stub_hub_api 0.0.2 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f87144cbbc215abaaa8630e6bdb64777eab7789
4
- data.tar.gz: 3e5fc4976034571c5235dd21cdd28baa067bc025
3
+ metadata.gz: c102374323a739fcdd8e693469b81de49329d7de
4
+ data.tar.gz: d74203b2d9ecda50c0417165573ed551fa5f906b
5
5
  SHA512:
6
- metadata.gz: b8302a1e10fd72d0f3916379af73f5e33eb49e9384d8e703468ac2c23178a937807435b67a8714b8c4b6e69364baa1ef97c87f59c1811507e8b6c56c5c113fe5
7
- data.tar.gz: 32e5094aef82501b444b25cc6c921d96963112a85cfbb7a698d65df2695ed0a2ac9fb421501b36180927602413a95d0671e797bb01ed7183c989627089a13ce0
6
+ metadata.gz: 5ed73ff6affc950ac2e1dfc0b20fe10db43adcd154c515aab431c7373053c06d4bd4066d9d30bad8914ef2f067ab776bcfc4521458ca673ff5516ab368538b77
7
+ data.tar.gz: bc5a8e4e4c555c4b02cefcfe3627b4b4fe7d82c09b2ab5325ce867a78f8cddc60726ae2e848ba93c60c2fa2516b75db2ffab06f27feefc5f95efb93c61cfe636
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # StubHubApi [![Code Climate](https://codeclimate.com/github/hashdog/stub_hub_api/badges/gpa.svg)](https://codeclimate.com/github/hashdog/stub_hub_api)
1
+ # StubHubApi [![Code Climate](https://codeclimate.com/github/hashdog/stub_hub_api/badges/gpa.svg)](https://codeclimate.com/github/hashdog/stub_hub_api) [![Gem Version](https://badge.fury.io/rb/stub_hub_api.svg)](http://badge.fury.io/rb/stub_hub_api)
2
2
 
3
3
  This gem provides access to add, edit and delete listings appearing on StubHub.
4
4
 
data/lib/stub_hub_api.rb CHANGED
@@ -1,16 +1,95 @@
1
1
  require 'json'
2
+ require 'nokogiri'
2
3
  require 'faraday'
4
+ require 'open-uri'
5
+ require 'base64'
6
+ require 'logger'
3
7
 
4
8
  require_relative 'stub_hub_api/version'
5
9
  require_relative 'stub_hub_api/response'
10
+ require_relative 'stub_hub_api/logger'
6
11
  require_relative 'stub_hub_api/base'
7
12
  require_relative 'stub_hub_api/login'
13
+ require_relative 'stub_hub_api/login_sandbox'
8
14
  require_relative 'stub_hub_api/account_management'
9
15
  require_relative 'stub_hub_api/account_management_sale'
10
16
  require_relative 'stub_hub_api/event'
11
17
  require_relative 'stub_hub_api/listing'
12
18
  require_relative 'stub_hub_api/search'
19
+ require_relative 'stub_hub_api/fulfillment'
13
20
  require_relative 'stub_hub_api/alert'
14
21
 
15
22
 
16
23
  module StubHubApi;end
24
+ #
25
+ # data = {:eventId=>"103765980",
26
+ # :isElectronicDelivery=>true,
27
+ # :pricePerProduct=>{:amount=>"559.98", :currency=>"USD"},
28
+ # :inhandDate=>"2018-11-07T09:59:56Z",
29
+ # :section=>"Parterre Center",
30
+ # :products=>
31
+ # [{:fullfillmentArtifact=>"99000002000071965129",
32
+ # :productType=>"ticket",
33
+ # :row=>"U ",
34
+ # :seat=>117,
35
+ # :operation=>"ADD"},
36
+ # {:fullfillmentArtifact=>"99000002000071965128",
37
+ # :productType=>"ticket",
38
+ # :row=>"U ",
39
+ # :seat=>118,
40
+ # :operation=>"ADD"},
41
+ # {:fullfillmentArtifact=>"99000002000071965127",
42
+ # :productType=>"ticket",
43
+ # :row=>"U ",
44
+ # :seat=>119,
45
+ # :operation=>"ADD"},
46
+ # {:fullfillmentArtifact=>"99000002000071965126",
47
+ # :productType=>"ticket",
48
+ # :row=>"U ",
49
+ # :seat=>120,
50
+ # :operation=>"ADD"}],
51
+ # :splitOption=>"MULTIPLES",
52
+ # :splitQuantity=>"1",
53
+ # :externalListingId=>359710}
54
+ #
55
+ # # c = StubHubApi::Listing.new(token: '0d85c364-10fb-3ee8-a41f-c5bf0c2ad7eb', sandbox: false)
56
+ # # r = c.create(data)
57
+ # # puts r.inspect
58
+ # # exit
59
+ #
60
+ # #1380812233
61
+ #
62
+ # # sleep(5)
63
+ # # puts "uploading...."
64
+ # cc = StubHubApi::Fulfillment.new(token: '0d85c364-10fb-3ee8-a41f-c5bf0c2ad7eb', sandbox: false)
65
+ #
66
+ # options = {
67
+ # :listing=>{
68
+ # :tickets=>[
69
+ # {:row=>"U", :seat=>"117", :name=>"17"},
70
+ # {:row=>"U", :seat=>"118", :name=>"18"},
71
+ # {:row=>"U", :seat=>"119", :name=>"19"},
72
+ # {:row=>"U", :seat=>"120", :name=>"20"}
73
+ # ]
74
+ # }
75
+ # }
76
+ # files = [
77
+ # {file_name: '17', file: '/Users/chebyte/rails/neuro/apis/stub_hub_api/lib/17.pdf'},
78
+ # {file_name: '18', file: '/Users/chebyte/rails/neuro/apis/stub_hub_api/lib/18.pdf'},
79
+ # {file_name: '19', file: '/Users/chebyte/rails/neuro/apis/stub_hub_api/lib/19.pdf'},
80
+ # {file_name: '20', file: '/Users/chebyte/rails/neuro/apis/stub_hub_api/lib/20.pdf'}
81
+ # ]
82
+ #
83
+ #
84
+ # r = cc.preload_pdf_json("1382617308", options, files)
85
+ #
86
+ # puts r.inspect
87
+ # exit
88
+ #
89
+ # #
90
+ # c = StubHubApi::Listing.new(token: '0d85c364-10fb-3ee8-a41f-c5bf0c2ad7eb', sandbox: false)
91
+ #
92
+ #
93
+ # r = c.delete('1382617308')
94
+ #
95
+ # puts r.inspect
@@ -23,8 +23,27 @@ module StubHubApi
23
23
  end
24
24
  end
25
25
 
26
+ def post_multi_pdfs_json_query_api url, body
27
+ session.post do |req|
28
+ req.url url
29
+ req.headers["Content-Type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
30
+ req.body = body
31
+ req.options.timeout = 2000
32
+ end.body
33
+ end
34
+
35
+ def post_multi_part_query_api url, options, file
36
+ session.post do |req|
37
+ req.url make_request_url(url, options)
38
+ req.headers['Content-Type'] = 'multipart/form-data'
39
+ req.body = {}
40
+ req.body[:file] = Faraday::UploadIO.new(file, 'application/pdf', "#{Time.now.to_i * rand(1000)}.pdf")
41
+ req.options.timeout = 2000
42
+ end.body
43
+ end
44
+
26
45
  def delete_query_api url
27
- session.delete(make_request_url(url, {}))
46
+ session.delete(make_request_url(url, {})).body
28
47
  end
29
48
 
30
49
  def put_query_api url, payload = false, options
@@ -41,6 +60,19 @@ module StubHubApi
41
60
 
42
61
  private
43
62
 
63
+ def folder_log
64
+ folder_path = "#{ENV['HOME']}/loggers/sh"
65
+ unless File.exists?(folder_path)
66
+ system "mkdir -p #{folder_path}"
67
+ end
68
+ folder_path
69
+ end
70
+
71
+ def logger
72
+ log_file = File.open("#{folder_log}/#{Time.now.strftime('%Y-%m-%d')}.log",'a+')
73
+ @logger = ::Logger.new(log_file)
74
+ end
75
+
44
76
  def sandbox_base_url
45
77
  'https://api.stubhubsandbox.com'
46
78
  end
@@ -65,15 +97,23 @@ module StubHubApi
65
97
  Faraday::Utils.build_nested_query(options)
66
98
  end
67
99
 
100
+ def user_agents
101
+ ["Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (FM Scene 4.6.1) ",
102
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5) ",
103
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36"]
104
+ end
105
+
68
106
  def session
69
107
  return @connection if @connection
70
108
 
71
109
  @connection = ::Faraday.new current_base_url, ssl: {verify: false} do |conn|
72
110
  conn.use Faraday::Response::StubHub
111
+ conn.use Faraday::Response::StubHubCustomLogger, logger
73
112
  conn.request :url_encoded
74
- conn.response :logger
113
+ conn.request :multipart
75
114
  conn.adapter Faraday.default_adapter
76
115
  end
116
+ @connection.headers[:user_agent] = user_agents.shuffle.first
77
117
  @connection.authorization :Bearer, token
78
118
  @connection
79
119
  end
@@ -0,0 +1,68 @@
1
+ module StubHubApi
2
+ class Fulfillment < Base
3
+ def upload_pdf(file, sale_id, options)
4
+ doc = post_multi_part_query_api("/fulfillment/pdf/v1/sale/#{sale_id}", options, StringIO.new(Base64.decode64(file)))
5
+ fields = %w(orderId row seatNo uploadStatus delivered)
6
+ response = {}
7
+ doc.xpath("uploadPDFTicketToOrderResponse").each do |xml|
8
+ fields.each do |field|
9
+ response[field] = xml.at_xpath(field).content if xml.at_xpath(field).content
10
+ end
11
+ end
12
+ response
13
+ end
14
+
15
+ def preload_pdf(file, listing_id, options)
16
+ doc = post_multi_part_query_api("/fulfillment/pdf/v1/listing/#{listing_id}", options, StringIO.new(Base64.decode64(file)))
17
+ fields = %w(listingId row seatNo uploadStatus preDelivered)
18
+ response = {}
19
+ doc.xpath("uploadPDFTicketToListingResponse").each do |xml|
20
+ fields.each do |field|
21
+ response[field] = xml.at_xpath(field).content if xml.at_xpath(field).content
22
+ end
23
+ end
24
+ response
25
+ end
26
+
27
+ def preload_pdf_json(listing_id, options, files)
28
+ boundary = "------WebKitFormBoundary7MA4YWxkTrZu0gW"
29
+ post_body = ""
30
+ post_body << "#{boundary}\r\n"
31
+ post_body << "Content-Disposition: form-data; name=\"listing\"\r\n\r\n"
32
+ post_body << "#{options.to_json}\r\n"
33
+ files.each do |file|
34
+ post_body << "#{boundary}\r\n"
35
+ post_body << "Content-Disposition: form-data; name=\"#{file[:file_name]}\"; filename=\"#{file[:file_name]}.pdf\"\r\n"
36
+ post_body << "Content-Type: application/pdf\r\n\r\n"
37
+ post_body << ""
38
+ post_body << ""
39
+ post_body << File.read(file[:file])
40
+ post_body << ""
41
+ post_body << "\r\n"
42
+ end
43
+ post_body << "\r\n"
44
+ post_body << "#{boundary}--\r\n"
45
+ post_multi_pdfs_json_query_api("/inventory/listings/v1/#{listing_id}/pdfs", post_body)
46
+ end
47
+
48
+
49
+ def label_for(options)
50
+ post_query_api("/fulfillment/shipping/v1/labels", true, options)
51
+ end
52
+
53
+
54
+ def get_label_for(options)
55
+ get_query_api("/fulfillment/shipping/v1/labels", options)
56
+ end
57
+
58
+ def confirm_transfer_sales(sale_id)
59
+ post_query_api("/fulfillment/delivery/v1/sale/#{sale_id}/confirmation", false)
60
+ end
61
+
62
+ def upload_barcode(options)
63
+ post_query_api("/fulfillment/barcode/v1/order", true, options)
64
+ end
65
+
66
+
67
+ end
68
+ end
@@ -1,11 +1,11 @@
1
1
  module StubHubApi
2
2
  class Listing < Base
3
3
  def create(options = {})
4
- post_query_api("/inventory/listings/v1", true, options)
4
+ post_query_api("/inventory/listings/v2", true, options)
5
5
  end
6
6
 
7
7
  def create_with_barcode(options = {})
8
- post_query_api("/inventory/listings/v1/barcodes", true, options)
8
+ post_query_api("/inventory/listings/v2/barcodes", true, options)
9
9
  end
10
10
 
11
11
  def delete(listing_id)
@@ -13,11 +13,11 @@ module StubHubApi
13
13
  end
14
14
 
15
15
  def update(listing_id, options = {})
16
- put_query_api("/inventory/listings/v1/#{listing_id}", true, options)
16
+ put_query_api("/inventory/listings/v2/#{listing_id}", true, options)
17
17
  end
18
18
 
19
19
  def show(listing_id, options = {})
20
- get_query_api("/inventory/listings/v1/#{listing_id}", options)
20
+ get_query_api("/inventory/listings/v2/#{listing_id}", options)
21
21
  end
22
22
 
23
23
  end
@@ -0,0 +1,54 @@
1
+ require 'forwardable'
2
+
3
+ module Faraday
4
+ class Response::StubHubCustomLogger < Response::Middleware
5
+ extend Forwardable
6
+
7
+ def initialize(app, logger = nil, options = {})
8
+ super(app)
9
+ @logger = logger || begin
10
+ require 'logger'
11
+ ::Logger.new(STDOUT)
12
+ end
13
+ @options = options
14
+ end
15
+
16
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
17
+
18
+ def call(env)
19
+ if env.method != :get
20
+ info "#{env.method} #{env.url.to_s}"
21
+ debug('request') { dump_headers env.request_headers }
22
+ debug('request') { dump_body(env[:body]) } if env[:body]
23
+ end
24
+ super
25
+ end
26
+
27
+ def on_complete(env)
28
+ if env.method != :get
29
+ info('Status') { env.status.to_s }
30
+ debug('response') { dump_headers env.response_headers }
31
+ debug('response') { dump_body env[:body] } if env[:body]
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def dump_headers(headers)
38
+ headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
39
+ end
40
+
41
+ def dump_body(body)
42
+ if body.respond_to?(:to_str)
43
+ body.to_str
44
+ else
45
+ pretty_inspect(body)
46
+ end
47
+ end
48
+
49
+ def pretty_inspect(body)
50
+ require 'pp' unless body.respond_to?(:pretty_inspect)
51
+ body.pretty_inspect
52
+ end
53
+ end
54
+ end
@@ -28,6 +28,7 @@ module StubHubApi
28
28
 
29
29
  @conn = Faraday.new(:url => base_url, :ssl => {:verify => false}) do |faraday|
30
30
  faraday.request :url_encoded
31
+ faraday.use Faraday::Response::StubHubCustomLogger, logger, bodies: true
31
32
  faraday.adapter Faraday.default_adapter
32
33
  end
33
34
  @conn.basic_auth(@consumer_key, @consumer_secret)
@@ -35,7 +36,7 @@ module StubHubApi
35
36
  end
36
37
 
37
38
  def init
38
- @response = session.post '/login', { grant_type: 'password', username: @username, password: @password, scope: 'SANDBOX' }
39
+ @response = session.post '/login', { grant_type: 'password', username: @username, password: @password}
39
40
  end
40
41
  end
41
42
  end
@@ -0,0 +1,42 @@
1
+ module StubHubApi
2
+ class LoginSandbox < Base
3
+ attr_reader :response
4
+
5
+ def initialize(consumer_key:, consumer_secret:, username:, password:)
6
+ @consumer_key = consumer_key
7
+ @consumer_secret = consumer_secret
8
+ @username = username
9
+ @password = password
10
+ init
11
+ end
12
+
13
+ def full_response
14
+ response
15
+ end
16
+
17
+ def user_id
18
+ response.headers['x-stubhub-user-guid']
19
+ end
20
+
21
+ def account
22
+ response.body
23
+ end
24
+
25
+ private
26
+ def session
27
+ return @conn if @conn
28
+
29
+ @conn = Faraday.new(:url => sandbox_base_url, :ssl => {:verify => false}) do |faraday|
30
+ faraday.request :url_encoded
31
+ faraday.use Faraday::Response::StubHubCustomLogger, logger, bodies: true
32
+ faraday.adapter Faraday.default_adapter
33
+ end
34
+ @conn.basic_auth(@consumer_key, @consumer_secret)
35
+ @conn
36
+ end
37
+
38
+ def init
39
+ @response = session.post '/login', { grant_type: 'password', username: @username, password: @password, scope: 'SANDBOX'}
40
+ end
41
+ end
42
+ end
@@ -1,10 +1,27 @@
1
1
  #api exception
2
2
  class ApiException < StandardError;end
3
3
 
4
+ #monkey patch for json
5
+ module JSON
6
+ def self.is_json?(foo)
7
+ begin
8
+ return false unless foo.is_a?(String)
9
+ JSON.parse(foo).all?
10
+ rescue JSON::ParserError
11
+ false
12
+ end
13
+ end
14
+ end
15
+
16
+
4
17
  module Faraday
5
18
  class Response::StubHub < Response::Middleware
6
19
  def parse_body(body)
7
- JSON.load(body)
20
+ if JSON.is_json?(body)
21
+ JSON.parse(body)
22
+ else
23
+ Nokogiri::XML(body)
24
+ end
8
25
  end
9
26
 
10
27
  def check_status(env)
@@ -9,7 +9,7 @@ module StubHubApi
9
9
  end
10
10
 
11
11
  def event(options = {})
12
- get_query_api("/search/catalog/events/v2", options)
12
+ get_query_api("/search/catalog/events/v3", options)
13
13
  end
14
14
  end
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module StubHubApi
2
- VERSION = "0.0.2"
2
+ VERSION = "1.1.4"
3
3
  end
data/stub_hub_api.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency 'rake', '~> 0'
23
- spec.add_runtime_dependency 'faraday', '~> 0.9', '>= 0.9.0'
23
+ spec.add_runtime_dependency 'nokogiri', '1.6.3.1', '>= 1.6.3.1'
24
+ spec.add_runtime_dependency 'faraday', '~> 0.9', '>= 0.9.2'
24
25
  spec.add_runtime_dependency 'json', '~> 1.8.1', '>= 1.8.1'
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stub_hub_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - chebyte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-25 00:00:00.000000000 Z
11
+ date: 2018-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,26 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.3.1
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.6.3.1
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '='
56
+ - !ruby/object:Gem::Version
57
+ version: 1.6.3.1
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.6.3.1
41
61
  - !ruby/object:Gem::Dependency
42
62
  name: faraday
43
63
  requirement: !ruby/object:Gem::Requirement
@@ -47,7 +67,7 @@ dependencies:
47
67
  version: '0.9'
48
68
  - - ">="
49
69
  - !ruby/object:Gem::Version
50
- version: 0.9.0
70
+ version: 0.9.2
51
71
  type: :runtime
52
72
  prerelease: false
53
73
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,7 +77,7 @@ dependencies:
57
77
  version: '0.9'
58
78
  - - ">="
59
79
  - !ruby/object:Gem::Version
60
- version: 0.9.0
80
+ version: 0.9.2
61
81
  - !ruby/object:Gem::Dependency
62
82
  name: json
63
83
  requirement: !ruby/object:Gem::Requirement
@@ -97,8 +117,11 @@ files:
97
117
  - lib/stub_hub_api/alert.rb
98
118
  - lib/stub_hub_api/base.rb
99
119
  - lib/stub_hub_api/event.rb
120
+ - lib/stub_hub_api/fulfillment.rb
100
121
  - lib/stub_hub_api/listing.rb
122
+ - lib/stub_hub_api/logger.rb
101
123
  - lib/stub_hub_api/login.rb
124
+ - lib/stub_hub_api/login_sandbox.rb
102
125
  - lib/stub_hub_api/response.rb
103
126
  - lib/stub_hub_api/search.rb
104
127
  - lib/stub_hub_api/version.rb
@@ -123,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
146
  version: '0'
124
147
  requirements: []
125
148
  rubyforge_project:
126
- rubygems_version: 2.2.2
149
+ rubygems_version: 2.2.5
127
150
  signing_key:
128
151
  specification_version: 4
129
152
  summary: Gem for StubHub