From 2be2d4419c34b961dd76c1c3db123d9bf5c29fb1 Mon Sep 17 00:00:00 2001 From: kenna-bmcdevitt Date: Wed, 22 Aug 2018 15:03:42 -0500 Subject: [PATCH] fixed naming conventions and set this up to begin working on command line calls --- .gitignore | 1 + Gemfile | 1 - bin/pastebinner | 2 ++ lib/pastebinner.rb | 40 +++++++++++++++++++++------------------- pastebinner.gemspec | 7 ++++--- 5 files changed, 28 insertions(+), 23 deletions(-) create mode 100755 bin/pastebinner diff --git a/.gitignore b/.gitignore index b04a8c8..a4be4c3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /pkg/ /spec/reports/ /tmp/ +Gemfile.lock # rspec failure tracking .rspec_status diff --git a/Gemfile b/Gemfile index 6f07931..7462a8e 100644 --- a/Gemfile +++ b/Gemfile @@ -3,5 +3,4 @@ source "https://rubygems.org" git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in pastebinner.gemspec -rest-client gemspec diff --git a/bin/pastebinner b/bin/pastebinner new file mode 100755 index 0000000..1c55357 --- /dev/null +++ b/bin/pastebinner @@ -0,0 +1,2 @@ +#!/usr/bin/env ruby +require 'pastebinner' diff --git a/lib/pastebinner.rb b/lib/pastebinner.rb index bae74c3..c248412 100755 --- a/lib/pastebinner.rb +++ b/lib/pastebinner.rb @@ -4,9 +4,8 @@ # official docs from pastebin on their api can be found at https://pastebin.com/api require 'rest-client' -module Pastebin - class Pastebinner - attr_reader :api_user_key +class Pastebinner + attr_accessor :api_dev_key, :username, :password def initialize(api_dev_key, username, password) @api_dev_key = api_dev_key @@ -14,7 +13,6 @@ module Pastebin @password = password @base_api_url = 'https://pastebin.com/api' @scraping_api_url = 'https://scrape.pastebin.com' - @api_user_key = self.get_api_user_key end # this should be a hash of { endpoint_name: '/url_endpoint.php'} @@ -45,9 +43,9 @@ module Pastebin execute_query(:api_post, params) end - def get_api_user_key + def api_user_key # returns a user session key that can be used as the api_user_key param - @response ||= RestClient::Request.execute({ + @api_user_key ||= RestClient::Request.execute({ method: :post, url: @base_api_url + ENDPOINTS[:login], payload: { 'api_dev_key': @api_dev_key, @@ -56,8 +54,8 @@ module Pastebin end def list_user_pastes - params = { 'api_dev_key': @api_dev_key, - 'api_user_key': @api_user_key, + params = { 'api_dev_key': api_dev_key, + 'api_user_key': api_user_key, 'api_results_limit': '100', 'api_option': 'list' } @@ -65,7 +63,7 @@ module Pastebin end def list_trending_pastes - params = { 'api_dev_key': @api_dev_key, + params = { 'api_dev_key': api_dev_key, 'api_option': 'trend' } execute_query(:api_post, params) @@ -73,14 +71,19 @@ module Pastebin # api_paste_key = this is the unique key of the paste data you want to delete. def delete_user_paste(api_paste_key) - params = { 'api_dev_key': @api_dev_key, - 'api_user_key': @api_user_key, + params = { 'api_dev_key': api_dev_key, + 'api_user_key': api_user_key, 'api_paste_key': api_paste_key, 'api_option': 'delete' } execute_query(:api_post, params) end + def get_user_info + params = { 'api_dev_key': api_dev_key, + } + end + def api_post(params) response = RestClient::Request.execute( method: :post, @@ -105,30 +108,29 @@ module Pastebin puts e.message end end - + # make my own exception class + # inherit ruby standard error class end -end - ######################## TESTING #################################################### ##################################################################################### #### INITIAL STEPS # setup our object and grab a session key -pb = Pastebin::Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password']) +pb = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password']) api_dev_key = ENV['pastebin_api_key'] #### CREATE PASTE # prepare some sample paste data to send -paste_data = 'this is a test paste two two two.' +#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 } -puts pb.create_paste(params) +#params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data } +#puts pb.create_paste(params) #### SCRAPE PUBLIC PASTES #public_pastes = pb.execute_query(:scrape_public_pastes) #puts public_pastes #### LIST USER PASTES -#puts pb.execute_query(:list_user_pastes) +puts pb.scrape_public_pastes diff --git a/pastebinner.gemspec b/pastebinner.gemspec index c469219..85622e0 100644 --- a/pastebinner.gemspec +++ b/pastebinner.gemspec @@ -26,12 +26,13 @@ Gem::Specification.new do |spec| 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.bindir = "bin" + spec.executables = ['pastebinner'] + 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" - spec.add_development_dependency "rest-client", "~> 2.0" + spec.add_runtime_dependency "rest-client", "~> 2.0" end