fixed naming conventions and set this up to begin working on command line calls

This commit is contained in:
kenna-bmcdevitt 2018-08-22 15:03:42 -05:00
parent eaf6207f22
commit 2be2d4419c
5 changed files with 28 additions and 23 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@
/pkg/ /pkg/
/spec/reports/ /spec/reports/
/tmp/ /tmp/
Gemfile.lock
# rspec failure tracking # rspec failure tracking
.rspec_status .rspec_status

View file

@ -3,5 +3,4 @@ source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in pastebinner.gemspec # Specify your gem's dependencies in pastebinner.gemspec
rest-client
gemspec gemspec

2
bin/pastebinner Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env ruby
require 'pastebinner'

View file

@ -4,9 +4,8 @@
# official docs from pastebin on their api can be found at https://pastebin.com/api # official docs from pastebin on their api can be found at https://pastebin.com/api
require 'rest-client' require 'rest-client'
module Pastebin class Pastebinner
class Pastebinner attr_accessor :api_dev_key, :username, :password
attr_reader :api_user_key
def initialize(api_dev_key, username, password) def initialize(api_dev_key, username, password)
@api_dev_key = api_dev_key @api_dev_key = api_dev_key
@ -14,7 +13,6 @@ module Pastebin
@password = password @password = password
@base_api_url = 'https://pastebin.com/api' @base_api_url = 'https://pastebin.com/api'
@scraping_api_url = 'https://scrape.pastebin.com' @scraping_api_url = 'https://scrape.pastebin.com'
@api_user_key = self.get_api_user_key
end end
# this should be a hash of { endpoint_name: '/url_endpoint.php'} # this should be a hash of { endpoint_name: '/url_endpoint.php'}
@ -45,9 +43,9 @@ module Pastebin
execute_query(:api_post, params) execute_query(:api_post, params)
end 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 # 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, method: :post,
url: @base_api_url + ENDPOINTS[:login], url: @base_api_url + ENDPOINTS[:login],
payload: { 'api_dev_key': @api_dev_key, payload: { 'api_dev_key': @api_dev_key,
@ -56,8 +54,8 @@ module Pastebin
end end
def list_user_pastes def list_user_pastes
params = { 'api_dev_key': @api_dev_key, params = { 'api_dev_key': api_dev_key,
'api_user_key': @api_user_key, 'api_user_key': api_user_key,
'api_results_limit': '100', 'api_results_limit': '100',
'api_option': 'list' 'api_option': 'list'
} }
@ -65,7 +63,7 @@ module Pastebin
end end
def list_trending_pastes def list_trending_pastes
params = { 'api_dev_key': @api_dev_key, params = { 'api_dev_key': api_dev_key,
'api_option': 'trend' 'api_option': 'trend'
} }
execute_query(:api_post, params) 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. # api_paste_key = this is the unique key of the paste data you want to delete.
def delete_user_paste(api_paste_key) def delete_user_paste(api_paste_key)
params = { 'api_dev_key': @api_dev_key, params = { 'api_dev_key': api_dev_key,
'api_user_key': @api_user_key, 'api_user_key': api_user_key,
'api_paste_key': api_paste_key, 'api_paste_key': api_paste_key,
'api_option': 'delete' 'api_option': 'delete'
} }
execute_query(:api_post, params) execute_query(:api_post, params)
end end
def get_user_info
params = { 'api_dev_key': api_dev_key,
}
end
def api_post(params) def api_post(params)
response = RestClient::Request.execute( response = RestClient::Request.execute(
method: :post, method: :post,
@ -105,30 +108,29 @@ module Pastebin
puts e.message puts e.message
end end
end end
# make my own exception class
# inherit ruby standard error class
end end
end
######################## TESTING #################################################### ######################## TESTING ####################################################
##################################################################################### #####################################################################################
#### INITIAL STEPS #### INITIAL STEPS
# setup our object and grab a session key # 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'] api_dev_key = ENV['pastebin_api_key']
#### CREATE PASTE #### CREATE PASTE
# prepare some sample paste data to send # 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 # 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) #puts pb.create_paste(params)
#### SCRAPE PUBLIC PASTES #### SCRAPE PUBLIC PASTES
#public_pastes = pb.execute_query(:scrape_public_pastes) #public_pastes = pb.execute_query(:scrape_public_pastes)
#puts public_pastes #puts public_pastes
#### LIST USER PASTES #### LIST USER PASTES
#puts pb.execute_query(:list_user_pastes) puts pb.scrape_public_pastes

View file

@ -26,12 +26,13 @@ Gem::Specification.new do |spec|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end end
spec.bindir = "exe" spec.bindir = "bin"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = ['pastebinner']
spec.require_paths = ["lib"] spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.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 end