diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f6b4de9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +--- +sudo: false +language: ruby +cache: bundler +rvm: + - 2.5.1 +before_install: gem install bundler -v 1.16.3 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7462a8e --- /dev/null +++ b/Gemfile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d4d0ec --- /dev/null +++ b/README.md @@ -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. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..b7e9ed5 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..fbf28a8 --- /dev/null +++ b/bin/console @@ -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__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -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 diff --git a/lib/pastebinner.rb b/lib/pastebinner.rb new file mode 100755 index 0000000..0f279ee --- /dev/null +++ b/lib/pastebinner.rb @@ -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) diff --git a/lib/pastebinner/version.rb b/lib/pastebinner/version.rb new file mode 100644 index 0000000..3849134 --- /dev/null +++ b/lib/pastebinner/version.rb @@ -0,0 +1,3 @@ +module Pastebinner + VERSION = "0.1.0" +end diff --git a/pastebinner.gemspec b/pastebinner.gemspec new file mode 100644 index 0000000..a1a3e34 --- /dev/null +++ b/pastebinner.gemspec @@ -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 diff --git a/spec/pastebinner_spec.rb b/spec/pastebinner_spec.rb new file mode 100644 index 0000000..7e30863 --- /dev/null +++ b/spec/pastebinner_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d409a8f --- /dev/null +++ b/spec/spec_helper.rb @@ -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