initial commit

This commit is contained in:
booboy 2018-08-16 14:54:08 -05:00
parent d667a0b2ce
commit da6c789a5c
13 changed files with 249 additions and 0 deletions

11
.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
# rspec failure tracking
.rspec_status

3
.rspec Normal file
View file

@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper

7
.travis.yml Normal file
View file

@ -0,0 +1,7 @@
---
sudo: false
language: ruby
cache: bundler
rvm:
- 2.5.1
before_install: gem install bundler -v 1.16.3

6
Gemfile Normal file
View file

@ -0,0 +1,6 @@
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in pastebinner.gemspec
gemspec

35
README.md Normal file
View file

@ -0,0 +1,35 @@
# Pastebinner
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pastebinner`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'pastebinner'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install pastebinner
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pastebinner.

6
Rakefile Normal file
View file

@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

14
bin/console Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require "bundler/setup"
require "pastebinner"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)

8
bin/setup Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

96
lib/pastebinner.rb Executable file
View file

@ -0,0 +1,96 @@
#!/usr/bin/env ruby
# author: brendan mcdevitt
# a ruby wrapper around all of the methods pastebin provides with its api
# official docs from pastebin on their api can be found at https://pastebin.com/api
require 'rest-client'
module PasteBin
class PasteBinner
# PasteBinner.new(api_dev_key)
def initialize(api_dev_key)
@api_dev_key = api_dev_key
@base_api_url = 'https://pastebin.com/api'
@scraping_api_url = 'https://scrape.pastebin.com'
end
# this should be a hash of { endpoint_name: '/url_endpoint.php'}
ENDPOINTS = { :login => '/api_login.php',
:post => '/api_post.php',
:raw => '/api_raw.php',
:scraping => '/api_scraping.php',
:scrape_item => '/api_scrape_item.php',
:srape_item_meta => '/api_scrape_item_meta.php' }
# basic example hash for creating a paste:
# params = { 'api_dev_key': @api_dev_key, 'api_option': 'paste'. 'api_paste_code': paste_data}
# required params:
# api_dev_key - your unique developer api key
# api_option - set as paste, this will indicate you want to create a new paste
# api_paste_code - this is the text that will be written inside of your paste
# optional params:
# api_user_key - this parameter is part of the login system, which is explained further down the page
# api_paste_name - this will be the name / title of your paste
# api_paste_format - this will be the syntax highlighting value, which is explained in detail further down the page
# api_paste_private - this makes a paste public, unlisted, or private, public = 0, unlisted = 1, private = 2
# api_paste_expire_date - this sets the expiration date of your paste, the values are explained further down the page
def create_paste(params)
response = RestClient::Request.execute(
method: :post,
url: @base_api_url + ENDPOINTS[:post],
payload: params )
end
def get_api_user_key(username, password)
# returns a user session key that can be used as the api_user_key param
@response ||= RestClient::Request.execute({
method: :post,
url: @base_api_url + ENDPOINTS[:login],
payload: { 'api_dev_key': @api_dev_key,
'api_user_name': username,
'api_user_password': password }})
end
# params is optional for now. to query specific language ?lang=ruby as an example
def scrape_public_pastes(params = nil)
response = RestClient::Request.execute(
method: :get,
url: @scraping_api_url + ENDPOINTS[:scraping])
end
# this will be the main way to execute any of these methods. this has the exception handling taken care of.
def execute_query(selector, *args)
begin
send(selector, *args)
rescue RestClient::ExceptionWithResponse => e
puts e.message
end
end
end
end
######################## TESTING ####################################################
#####################################################################################
#
# CREATE PASTE
#
# setup our api key
api_dev_key = ENV['pastebin_api_key']
# setup our object and grab a session key
pb = PasteBin::PasteBinner.new(api_dev_key)
#api_user_key = pb.get_api_user_key(ENV['pastebin_username'], ENV['pastebin_password'])
# here is some paste content
paste_data = 'this is a test paste two two two.'
# prepare our paste params
#params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data }
params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data }
#puts pb.create_paste(params)
#public_pastes = pb.execute_query(pb.scrape_public_pastes)
puts pb.execute_query(:create_paste, params)

View file

@ -0,0 +1,3 @@
module Pastebinner
VERSION = "0.1.0"
end

37
pastebinner.gemspec Normal file
View file

@ -0,0 +1,37 @@
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "pastebinner/version"
Gem::Specification.new do |spec|
spec.name = "pastebinner"
spec.version = Pastebinner::VERSION
spec.authors = ["booboy"]
spec.email = ["bpmcdevitt@thelinuxspace.com"]
spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end

9
spec/pastebinner_spec.rb Normal file
View file

@ -0,0 +1,9 @@
RSpec.describe Pastebinner do
it "has a version number" do
expect(Pastebinner::VERSION).not_to be nil
end
it "does something useful" do
expect(false).to eq(true)
end
end

14
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,14 @@
require "bundler/setup"
require "pastebinner"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
end