gun_broker 0.4.1 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c615953eb407df3dfa91c322f4f903401ce37371
4
- data.tar.gz: 4eea32e2531e6517d755ec14fc3e2fb4b5b5d867
3
+ metadata.gz: 657189493f5cfb8faf699f3100fe60bd18190012
4
+ data.tar.gz: c1ccb2b44db024453638d5fa70204da06ce128fb
5
5
  SHA512:
6
- metadata.gz: f5dff34088eb750ff1dd38a36d9c629704bb0550abf1ec092faaba518e6abf82686cd6036d494c38ae85072c97ec87809902e7db4e457f785d550ce00554d231
7
- data.tar.gz: c92686ccc9e33528dbb4115a8b443230ac1d0c4f633525721882f5eaea22c80b7fc1343d102b52fd8f73745d9f8e21f50feaedbc1a18c0019d3a79da82fd4431
6
+ metadata.gz: 72ce3a15f96a354f2e93f378a6639b011386418123716304a6be15b839b54a5581a67d3f9bc92566126034166047dd485dc50c5616aea717df42b60c217462e6
7
+ data.tar.gz: 4c85034d8ebf42def28fb7aa2316326c2fb02f1eea25092ac52e5b85ab05abdc4fe6ff8b1db20cd8910d5de83f87193e3281b394bea7e4ddd453636d9c1d0171
data/README.md CHANGED
@@ -34,8 +34,10 @@ GunBroker.dev_key = 'your-sekret-dev-key'
34
34
 
35
35
  ### GunBroker::User
36
36
 
37
- Authentication requires a `username` and an 'auth options' hash that requires **at least** a `password`
38
- *or* `token`. If a password is given, you must call `User#authenticate!` to obtain an access token.
37
+ #### Authentication
38
+
39
+ Authentication requires a `username` and an 'auth options' hash that requires **at least** a `:password`
40
+ *or* `:token`. If a password is given, you must call `User#authenticate!` to obtain an access token.
39
41
 
40
42
  ```ruby
41
43
  # By username/password:
@@ -49,16 +51,6 @@ user = GunBroker::User.new('username', token: 'user-access-token')
49
51
  user.token # => 'user-access-token'
50
52
  ```
51
53
 
52
- Once the User has an access token, you can then grab all of their items (listings). All items methods
53
- return an array of `GunBroker::Item` instances.
54
-
55
- ```ruby
56
- user.items.all # => [GunBroker::Item, ...]
57
- user.items.unsold # => [GunBroker::Item, ...]
58
- user.items.sold # => [GunBroker::Item, ...]
59
- user.items.won # => [GunBroker::Item, ...]
60
- ```
61
-
62
54
  To revoke the access token, call `User#deauthenticate!`. This method is also aliased as `#revoke_access_token!`.
63
55
 
64
56
  ```ruby
@@ -67,6 +59,24 @@ user.deauthenticate!
67
59
  user.token # => nil
68
60
  ```
69
61
 
62
+ #### Items
63
+
64
+ You can access a User's Items through the `User#items` method, which returns an instance of `User::ItemsDelegate`.
65
+
66
+ ```ruby
67
+ user.items.all # => [GunBroker::Item, ...]
68
+ user.items.sold # => [GunBroker::Item, ...]
69
+ ```
70
+
71
+ To find a specific Item by its ID, use `#find`. This will return a `GunBroker::Item` instance or `nil` if no item found.
72
+
73
+ ```ruby
74
+ item = user.items.find(123)
75
+ ```
76
+
77
+ To raise a `GunBroker::Error::NotFound` exception if no Item can be found, use `#find!`.
78
+
79
+
70
80
  ### GunBroker::Item
71
81
 
72
82
  Represents an item (listing) on GunBroker. The `Item#id` method returns the value of the `itemID` attribute
@@ -74,12 +84,27 @@ from the response. All other attributes can be accessed through the `Item#[]` m
74
84
 
75
85
  ```ruby
76
86
  item.id # => '1234567'
77
- item['title'] # => 'Super Awesome Scope'
87
+ item.title # => 'Super Awesome Scope'
88
+ item.category # => GunBroker::Category
89
+ item['description'] # => 'This scope is really awesome.'
90
+ ```
91
+
92
+ You can find an Item belonging to the authenticated User with `user.items.find` or any Item with `Item.find`.
93
+
94
+ ```ruby
95
+ # Returns the Item or nil, if the User has no Item with that ID.
96
+ user.items.find(123)
97
+
98
+ # Find any Item by its ID.
99
+ GunBroker::Item.find(123)
78
100
  ```
79
101
 
102
+ To raise a `GunBroker::Error::NotFound` exception if no Item can be found, use `Item.find!`.
103
+
104
+
80
105
  ### GunBroker::Category
81
106
 
82
- Returns GunBroker category responses. To get an array of all categories, call `Category#all()`.
107
+ Returns GunBroker category responses. To get an array of all categories, call `Category.all`.
83
108
 
84
109
  ```ruby
85
110
  GunBroker::Category.all
@@ -95,12 +120,22 @@ GunBroker::Category.all(firearms)
95
120
  # => [GunBroker::Category, ...]
96
121
  ```
97
122
 
123
+ To find a Category by a specific ID, use either `Category.find` or `Category.find!`.
124
+
125
+ ```ruby
126
+ GunBroker::Category.find(123)
127
+ # => Returns the Category or nil
128
+ GunBroker::Category.find!(123)
129
+ # => Returns the Category or raises GunBroker::Error::NotFound
130
+ ```
131
+
98
132
  Much like GunBroker::Item, the `Category#id` method returns the `categoryID` attribute from the response.
99
133
  All other attributes can be accessed with `Category#[]`.
100
134
 
101
135
  ```ruby
102
136
  category.id # => '123'
103
- category['categoryName'] # => 'Firearms'
137
+ category.name # => 'Firearms'
138
+ category['description'] # => 'Modern Firearms are defined ...'
104
139
  ```
105
140
 
106
141
  ### GunBroker::API
@@ -133,8 +168,9 @@ GunBroker::API.delete('/some/resource', {}, { 'X-TestHeader' => 'FooBar' })
133
168
  ### Error Handling
134
169
 
135
170
  Methods that require authorization (with an access token) will raise a `GunBroker::Error::NotAuthorized`
136
- exception if the token isn't valid. Otherwise, if there is some other issue with the request (namely,
137
- the response status code is not in the `2xx` range), a `GunBroker::Error::RequestError` will be raised.
171
+ exception if the token isn't valid. If the response is an HTTP `404` status, a `GunBroker::Error::NotFound`
172
+ will be raised. Otherwise, if there is some other issue with the request (namely, the response status
173
+ code is not in the `2xx` range), a `GunBroker::Error::RequestError` will be raised.
138
174
 
139
175
  ## Contributing
140
176
 
@@ -4,7 +4,7 @@ require 'gun_broker/api'
4
4
  require 'gun_broker/category'
5
5
  require 'gun_broker/error'
6
6
  require 'gun_broker/item'
7
- require 'gun_broker/items_delegate'
7
+ require 'gun_broker/response'
8
8
  require 'gun_broker/user'
9
9
 
10
10
  module GunBroker
@@ -23,6 +23,21 @@ module GunBroker
23
23
  @@dev_key
24
24
  end
25
25
 
26
+ # Returns a hash containing the time on GunBroker's servers in UTC
27
+ # and the current version of the GunBroker API.
28
+ #
29
+ # For example:
30
+ #
31
+ # {
32
+ # "gunBrokerTime" => "2015-02-06T20:23:08Z",
33
+ # "gunBrokerVersion" => "6 4.4.2.12"
34
+ # }
35
+ #
36
+ # @return [Hash] Containing the time and API version.
37
+ def self.time
38
+ GunBroker::API.get('/GunBrokerTime')
39
+ end
40
+
26
41
  private
27
42
 
28
43
  def self.dev_key_present?
@@ -1,5 +1,5 @@
1
1
  require 'json'
2
- require 'net/https'
2
+ require 'net/http'
3
3
 
4
4
  module GunBroker
5
5
  # Generic REST adapter for the GunBroker API.
@@ -12,7 +12,7 @@ module GunBroker
12
12
  # @param params [Hash] (optional) URL params for GET requests; form params for POST request.
13
13
  # @param headers [Hash] (optional) Additional headers sent with the request.
14
14
  def initialize(path, params = {}, headers = {})
15
- raise "Path must start with '/': #{path}" unless path.start_with?('/')
15
+ raise GunBroker::Error.new("Path must start with '/': #{path}") unless path.start_with?('/')
16
16
 
17
17
  @path = path
18
18
  @params = params
@@ -41,7 +41,7 @@ module GunBroker
41
41
  def delete!
42
42
  request = Net::HTTP::Delete.new(uri)
43
43
  response = get_response(request)
44
- handle_response(response)
44
+ GunBroker::Response.new(response)
45
45
  end
46
46
 
47
47
  # Sends a GET request to the given `path`.
@@ -50,7 +50,7 @@ module GunBroker
50
50
 
51
51
  request = Net::HTTP::Get.new(uri)
52
52
  response = get_response(request)
53
- handle_response(response)
53
+ GunBroker::Response.new(response)
54
54
  end
55
55
 
56
56
  # Sends a POST request to the given `path`.
@@ -59,7 +59,7 @@ module GunBroker
59
59
  request.body = @params.to_json
60
60
 
61
61
  response = get_response(request)
62
- handle_response(response)
62
+ GunBroker::Response.new(response)
63
63
  end
64
64
 
65
65
  private
@@ -77,17 +77,6 @@ module GunBroker
77
77
  end
78
78
  end
79
79
 
80
- def handle_response(response)
81
- case response
82
- when Net::HTTPOK, Net::HTTPSuccess
83
- JSON.parse(response.body)
84
- when Net::HTTPUnauthorized
85
- raise GunBroker::Error::NotAuthorized.new(response)
86
- else
87
- raise GunBroker::Error::RequestError.new(response)
88
- end
89
- end
90
-
91
80
  def uri
92
81
  @uri ||= URI([GUNBROKER_API, @path].join)
93
82
  end
@@ -13,8 +13,18 @@ module GunBroker
13
13
  end
14
14
 
15
15
  # @param category_id [Integer, String] The ID of the Category to find.
16
- # @return [Category] A Category instance.
16
+ # @return [Category] A Category instance or `nil` if no Category with `category_id` exists.
17
17
  def self.find(category_id)
18
+ find!(category_id)
19
+ rescue GunBroker::Error::NotFound
20
+ nil
21
+ end
22
+
23
+ # Same as {.find} but raises GunBroker::Error::NotFound if no Category is found.
24
+ # @param (see .find)
25
+ # @raise [GunBroker::Error::NotFound] If no Category with `category_id` exists.
26
+ # @return (see .find)
27
+ def self.find!(category_id)
18
28
  new(GunBroker::API.get("/Categories/#{category_id}"))
19
29
  end
20
30
 
@@ -28,6 +38,11 @@ module GunBroker
28
38
  @attrs['categoryID']
29
39
  end
30
40
 
41
+ # @return [String] The Category name.
42
+ def name
43
+ @attrs['categoryName']
44
+ end
45
+
31
46
  # @param key [String] A Category attribute name (from the JSON response).
32
47
  # @return The value of the given `key` or `nil`.
33
48
  def [](key)
@@ -1,10 +1,27 @@
1
+ require 'gun_broker/item/constants'
2
+
1
3
  module GunBroker
2
4
  # Represents a GunBroker item (listing).
3
5
  class Item
4
6
 
7
+ include GunBroker::Item::Constants
8
+
9
+ # @return [Hash] Attributes parsed from the JSON response.
10
+ attr_reader :attrs
11
+
5
12
  # @param item_id [Integer, String] The ID of the Item to find.
6
- # @return [Item] An Item instance.
13
+ # @return [Item] An Item instance or `nil` if no Item with `item_id` exists.
7
14
  def self.find(item_id)
15
+ find!(item_id)
16
+ rescue GunBroker::Error::NotFound
17
+ nil
18
+ end
19
+
20
+ # Same as {.find} but raises GunBroker::Error::NotFound if no Item is found.
21
+ # @param (see .find)
22
+ # @raise [GunBroker::Error::NotFound] If no Item with `item_id` exists.
23
+ # @return (see .find)
24
+ def self.find!(item_id)
8
25
  new(GunBroker::API.get("/Items/#{item_id}"))
9
26
  end
10
27
 
@@ -18,6 +35,21 @@ module GunBroker
18
35
  @attrs['itemID']
19
36
  end
20
37
 
38
+ # @return [Hash] Attributes parsed from the JSON response.
39
+ def attributes
40
+ @attrs
41
+ end
42
+
43
+ # @return [Category] This Items Category.
44
+ def category
45
+ GunBroker::Category.find(@attrs['categoryID'])
46
+ end
47
+
48
+ # @return [String] Title of this Item.
49
+ def title
50
+ @attrs['title']
51
+ end
52
+
21
53
  # @param key [String] An Item attribute name (from the JSON response).
22
54
  # @return The value of the given `key` or `nil`.
23
55
  def [](key)
@@ -0,0 +1,94 @@
1
+ module GunBroker
2
+ class Item
3
+ # Holds constant values for Item.
4
+ module Constants
5
+
6
+ # Options for auto-relisting.
7
+ AUTO_RELIST = {
8
+ 1 => 'Do Not Relist',
9
+ 2 => 'Relist Until Sold',
10
+ 3 => 'Relist Fixed Count',
11
+ }
12
+
13
+ # Condition options.
14
+ CONDITION = {
15
+ 1 => 'Factory New',
16
+ 2 => 'New Old Stock',
17
+ 3 => 'Used',
18
+ }
19
+
20
+ # The return policy / inspection period for the item.
21
+ INSPECTION_PERIOD = {
22
+ 1 => 'AS IS - No refund or exchange',
23
+ 2 => 'No refund but item can be returned for exchange or store credit within fourteen days',
24
+ 3 => 'No refund but item can be returned for exchange or store credit within thirty days',
25
+ 4 => 'Three Days from the date the item is received',
26
+ 5 => 'Three Days from the date the item is received, including the cost of shipping',
27
+ 6 => 'Five Days from the date the item is received',
28
+ 7 => 'Five Days from the date the item is received, including the cost of shipping',
29
+ 8 => 'Seven Days from the date the item is received',
30
+ 9 => 'Seven Days from the date the item is received, including the cost of shipping',
31
+ 10 => 'Fourteen Days from the date the item is received',
32
+ 11 => 'Fourteen Days from the date the item is received, including the cost of shipping',
33
+ 12 => '30 day money back guarantee',
34
+ 13 => '30 day money back guarantee including the cost of shipping',
35
+ }
36
+
37
+ # How long before the listing ends.
38
+ LISTING_DURATION = {
39
+ 1 => 'One day',
40
+ 3 => 'Three days',
41
+ 5 => 'Five days',
42
+ 7 => 'Seven days',
43
+ 9 => 'Nine days',
44
+ 10 => 'Ten days',
45
+ 11 => 'Eleven days',
46
+ 12 => 'Twelve days',
47
+ 13 => 'Thirteen days',
48
+ 14 => 'Fourteen days',
49
+ 30 => 'Thirty days (Fixed price items only)',
50
+ 60 => 'Sixty days (Fixed price items only)',
51
+ 90 => 'Ninety days (Fixed price items only)',
52
+ }
53
+
54
+ # The payment methods accepted by the seller for this Item.
55
+ # The keys of this hash should be sent as `true` or `false` in the `paymentMethods` param.
56
+ PAYMENT_METHODS = {
57
+ 'SeeItemDesc' => 'See Item Description',
58
+ 'Amex' => 'American Express',
59
+ 'COD' => 'Cash on Delivery',
60
+ 'CertifiedCheck' => 'Certified Check',
61
+ 'Check' => 'Check',
62
+ 'Discover' => 'Discover Card',
63
+ 'Escrow' => 'Escrow',
64
+ 'MoneyOrder' => 'Money Order',
65
+ 'PayPal' => 'PayPal',
66
+ 'USPSMoneyOrder' => 'USPS Money Order',
67
+ 'VisaMastercard' => 'Visa / Mastercard',
68
+ }
69
+
70
+ # The type of shipping offered by the seller of this Item.
71
+ # The keys of this hash should be sent as `true` or `false` in the `shippingClassesSupported` param.
72
+ SHIPPING_CLASSES = {
73
+ 'Overnight' => 'Overnight',
74
+ 'TwoDay' => 'Two Day',
75
+ 'ThreeDay' => 'Three Day',
76
+ 'Ground' => 'Ground',
77
+ 'FirstClass' => 'First Class',
78
+ 'Priority' => 'Priority',
79
+ 'Other' => 'Other',
80
+ }
81
+
82
+ # Who pays for shipping.
83
+ # The key should be sent as the `whoPaysForShipping` param.
84
+ SHIPPING_PAYER = {
85
+ 1 => 'See item description',
86
+ 2 => 'Seller pays for shipping',
87
+ 4 => 'Buyer pays actual shipping cost',
88
+ 8 => 'Buyer pays fixed amount',
89
+ 16 => 'Use shipping profile',
90
+ }
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,28 @@
1
+ module GunBroker
2
+ # Wrapper class for the GunBroker API response JSON.
3
+ class Response
4
+
5
+ # @param response [Net::HTTPResponse] Response returned from the API.
6
+ def initialize(response)
7
+ @response = response
8
+
9
+ case @response
10
+ when Net::HTTPOK, Net::HTTPSuccess
11
+ @data = JSON.parse(@response.body)
12
+ when Net::HTTPUnauthorized
13
+ raise GunBroker::Error::NotAuthorized.new(@response)
14
+ when Net::HTTPNotFound
15
+ raise GunBroker::Error::NotFound.new(@response)
16
+ else
17
+ raise GunBroker::Error::RequestError.new(@response)
18
+ end
19
+ end
20
+
21
+ # @param key [String] Key from the response JSON to read.
22
+ # @return [String, Array, Hash] Whatever object is the value of `key` or `nil` if the key doesn't exist.
23
+ def [](key)
24
+ @data[key]
25
+ end
26
+
27
+ end
28
+ end
@@ -1,4 +1,5 @@
1
1
  require 'gun_broker/token_header'
2
+ require 'gun_broker/user/items_delegate'
2
3
 
3
4
  module GunBroker
4
5
  # Represents a GunBroker User.
@@ -0,0 +1,100 @@
1
+ require 'gun_broker/token_header'
2
+
3
+ module GunBroker
4
+ class User
5
+ # Used to scope {Item} lookup by {User}.
6
+ class ItemsDelegate
7
+
8
+ include GunBroker::TokenHeader
9
+
10
+ # @param user [User] A {User} instance to scope items by.
11
+ def initialize(user)
12
+ @user = user
13
+ end
14
+
15
+ # Returns all the User's items.
16
+ # @note {API#get! GET} /Items
17
+ # @raise [GunBroker::Error::NotAuthorized] If the {User#token} isn't valid.
18
+ # @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response).
19
+ # @return [Array<Item>]
20
+ def all
21
+ response = GunBroker::API.get('/Items', { 'SellerName' => @user.username }, token_header(@user.token))
22
+ items_from_results(response['results'])
23
+ end
24
+
25
+ # Returns all the items the User has bid on.
26
+ # @note {API#get! GET} /ItemsBidOn
27
+ # @raise (see #all)
28
+ # @return [Array<Item>]
29
+ def bid_on
30
+ response = GunBroker::API.get('/ItemsBidOn', {}, token_header(@user.token))
31
+ items_from_results(response['results'])
32
+ end
33
+
34
+ # Finds a specific User's Item by ID. Calls {Item.find} to get full Item details.
35
+ # @raise (see #all)
36
+ # @return [Item] Returns the Item or `nil` if no Item found.
37
+ def find(item_id)
38
+ # HACK: This has to filter through `#all`, since the GunBroker API currently has no way to scope the `/Items/{itemID}` endpoint by user.
39
+ if all.select { |item| item.id.to_s == item_id.to_s }.first
40
+ GunBroker::Item.find(item_id)
41
+ else
42
+ nil
43
+ end
44
+ end
45
+
46
+ # Same as {#find} but raises GunBroker::Error::NotFound if no item is found.
47
+ # @raise (see #all)
48
+ # @raise [GunBroker::Error::NotFound] If the User has no Item with `item_id`.
49
+ # @return [Item] Returns the Item or `nil` if no Item found.
50
+ def find!(item_id)
51
+ item = find(item_id)
52
+ raise GunBroker::Error::NotFound.new("Couldn't find item with ID '#{item_id}'") if item.nil?
53
+ item
54
+ end
55
+
56
+ # Items the User has bid on, but not won.
57
+ # @note {API#get! GET} /ItemsNotWon
58
+ # @raise (see #all)
59
+ # @return [Array<Item>]
60
+ def not_won
61
+ response = GunBroker::API.get('/ItemsNotWon', {}, token_header(@user.token))
62
+ items_from_results(response['results'])
63
+ end
64
+
65
+ # Items the User has sold.
66
+ # @note {API#get! GET} /ItemsSold
67
+ # @raise (see #all)
68
+ # @return [Array<Item>]
69
+ def sold
70
+ response = GunBroker::API.get('/ItemsSold', {}, token_header(@user.token))
71
+ items_from_results(response['results'])
72
+ end
73
+
74
+ # Items that were listed, but not sold.
75
+ # @note {API#get! GET} /ItemsUnsold
76
+ # @raise (see #all)
77
+ # @return [Array<Item>]
78
+ def unsold
79
+ response = GunBroker::API.get('/ItemsUnsold', {}, token_header(@user.token))
80
+ items_from_results(response['results'])
81
+ end
82
+
83
+ # Items the User has won.
84
+ # @note {API#get! GET} /ItemsWon
85
+ # @raise (see #all)
86
+ # @return [Array<Item>]
87
+ def won
88
+ response = GunBroker::API.get('/ItemsWon', {}, token_header(@user.token))
89
+ items_from_results(response['results'])
90
+ end
91
+
92
+ private
93
+
94
+ def items_from_results(results)
95
+ results.map { |result| GunBroker::Item.new(result) }
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -1,3 +1,3 @@
1
1
  module GunBroker
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.6"
3
3
  end
@@ -0,0 +1 @@
1
+ {"test":"value"}
@@ -0,0 +1 @@
1
+ {"gunBrokerTime":"2015-02-06T20:40:16Z","gunBrokerVersion":"6 4.4.2.12","links":[]}
@@ -7,6 +7,8 @@ describe GunBroker::API do
7
7
  let(:path) { '/some/resource' }
8
8
  let(:endpoint) { [GunBroker::API::GUNBROKER_API, path].join }
9
9
 
10
+ let(:test_response) { JSON.parse(response_fixture('test')) }
11
+
10
12
  before(:all) do
11
13
  GunBroker.dev_key = 'test-dev-key'
12
14
  end
@@ -15,15 +17,29 @@ describe GunBroker::API do
15
17
  expect(GunBroker::API::GUNBROKER_API).not_to be_nil
16
18
  end
17
19
 
20
+ it "raises GunBroker::Error if path does not start with '/'" do
21
+ expect { GunBroker::API.new('foo/bar') }.to raise_error(GunBroker::Error)
22
+ end
23
+
24
+ context 'response' do
25
+ it 'should return an instance of GunBroker::Response' do
26
+ stub_request(:get, endpoint)
27
+ .to_return(body: response_fixture('test'))
28
+
29
+ response = GunBroker::API.get(path)
30
+ expect(response).to be_a(GunBroker::Response)
31
+ end
32
+ end
33
+
18
34
  context '.delete' do
19
35
  context 'on success' do
20
36
  it 'returns JSON parsed response' do
21
37
  stub_request(:delete, endpoint)
22
38
  .with(headers: headers('X-AccessToken' => token))
23
- .to_return(body: response_fixture('deauthenticate'))
39
+ .to_return(body: response_fixture('test'))
24
40
 
25
41
  response = GunBroker::API.delete(path, {}, headers('X-AccessToken' => token))
26
- expect(response).to eq(JSON.parse(response_fixture('deauthenticate')))
42
+ expect(response['test']).to eq(test_response['test'])
27
43
  end
28
44
  end
29
45
 
@@ -44,10 +60,10 @@ describe GunBroker::API do
44
60
  it 'returns JSON parsed response' do
45
61
  stub_request(:get, endpoint)
46
62
  .with(query: { 'SellerName' => 'test-user' })
47
- .to_return(body: response_fixture('items'))
63
+ .to_return(body: response_fixture('test'))
48
64
 
49
65
  response = GunBroker::API.get(path, { 'SellerName' => 'test-user' })
50
- expect(response).to eq(JSON.parse(response_fixture('items')))
66
+ expect(response['test']).to eq(test_response['test'])
51
67
  end
52
68
  end
53
69
 
@@ -70,10 +86,12 @@ describe GunBroker::API do
70
86
  headers: headers,
71
87
  body: { username: 'test-user' }
72
88
  )
73
- .to_return(body: response_fixture('authenticate'))
89
+ .to_return(body: response_fixture('test'))
74
90
 
75
91
  response = GunBroker::API.post(path, { username: 'test-user' }, headers)
76
- expect(response).to eq(JSON.parse(response_fixture('authenticate')))
92
+
93
+ expect(response).to be_a(GunBroker::Response)
94
+ expect(response['test']).to eq(test_response['test'])
77
95
  end
78
96
  end
79
97
 
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe GunBroker::Category do
4
4
 
5
5
  let(:attrs) { JSON.parse(response_fixture('category')) }
6
+ let(:category) { GunBroker::Category.new(attrs) }
6
7
 
7
8
  before(:all) do
8
9
  GunBroker.dev_key = 'test-dev-key'
@@ -13,13 +14,15 @@ describe GunBroker::Category do
13
14
  end
14
15
 
15
16
  it 'should have an #id' do
16
- category = GunBroker::Category.new(attrs)
17
17
  expect(category.id).to eq(attrs['categoryID'])
18
18
  end
19
19
 
20
+ it 'should have a #name' do
21
+ expect(category.name).to eq(attrs['categoryName'])
22
+ end
23
+
20
24
  context '#[]' do
21
25
  it 'should return the value from @attrs' do
22
- category = GunBroker::Category.new(attrs)
23
26
  attrs.each { |k, v| expect(category[k]).to eq(v) }
24
27
  end
25
28
  end
@@ -43,7 +46,7 @@ describe GunBroker::Category do
43
46
  end
44
47
 
45
48
  context 'on failure' do
46
- it 'should raise a GunBroker::Error::RequestError exception' do
49
+ it 'should raise a GunBroker::Error::NotAuthorized exception' do
47
50
  stub_request(:get, endpoint)
48
51
  .with(
49
52
  headers: headers,
@@ -74,13 +77,42 @@ describe GunBroker::Category do
74
77
  end
75
78
 
76
79
  context 'on failure' do
77
- it 'should raise a GunBroker::Error::RequestError exception' do
80
+ it 'should return nil' do
78
81
  stub_request(:get, endpoint)
79
82
  .with(headers: headers)
80
- .to_return(body: response_fixture('not_authorized'), status: 401)
83
+ .to_return(body: response_fixture('not_authorized'), status: 404)
84
+
85
+ id = attrs['categoryID']
86
+ expect(GunBroker::Category.find(id)).to be_nil
87
+ end
88
+ end
89
+ end
90
+
91
+ context '.find!' do
92
+ let(:attrs) { JSON.parse(response_fixture('category')) }
93
+ let(:endpoint) { [GunBroker::API::GUNBROKER_API, "/Categories/#{attrs['categoryID']}"].join }
94
+
95
+ context 'on success' do
96
+ it 'returns the Category' do
97
+ stub_request(:get, endpoint)
98
+ .with(headers: headers)
99
+ .to_return(body: response_fixture('category'))
100
+
101
+ id = attrs['categoryID']
102
+ category = GunBroker::Category.find!(id)
103
+ expect(category).to be_a(GunBroker::Category)
104
+ expect(category.id).to eq(id)
105
+ end
106
+ end
107
+
108
+ context 'on failure' do
109
+ it 'should raise a GunBroker::Error::NotFound exception' do
110
+ stub_request(:get, endpoint)
111
+ .with(headers: headers)
112
+ .to_return(body: response_fixture('not_authorized'), status: 404)
81
113
 
82
114
  id = attrs['categoryID']
83
- expect { GunBroker::Category.find(id) }.to raise_error(GunBroker::Error::NotAuthorized)
115
+ expect { GunBroker::Category.find!(id) }.to raise_error(GunBroker::Error::NotFound)
84
116
  end
85
117
  end
86
118
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe GunBroker::Item::Constants do
4
+
5
+ it 'has a AUTO_RELIST hash' do
6
+ expect(GunBroker::Item::AUTO_RELIST).to be_a(Hash)
7
+ end
8
+
9
+ it 'has a CONDITION hash' do
10
+ expect(GunBroker::Item::CONDITION).to be_a(Hash)
11
+ end
12
+
13
+ it 'has a INSPECTION_PERIOD hash' do
14
+ expect(GunBroker::Item::INSPECTION_PERIOD).to be_a(Hash)
15
+ end
16
+
17
+ it 'has a LISTING_DURATION hash' do
18
+ expect(GunBroker::Item::LISTING_DURATION).to be_a(Hash)
19
+ end
20
+
21
+ it 'has a PAYMENT_METHODS hash' do
22
+ expect(GunBroker::Item::PAYMENT_METHODS).to be_a(Hash)
23
+ end
24
+
25
+ it 'has a SHIPPING_CLASSES hash' do
26
+ expect(GunBroker::Item::SHIPPING_CLASSES).to be_a(Hash)
27
+ end
28
+
29
+ it 'has a SHIPPING_PAYER hash' do
30
+ expect(GunBroker::Item::SHIPPING_PAYER).to be_a(Hash)
31
+ end
32
+
33
+ end
@@ -3,21 +3,44 @@ require 'spec_helper'
3
3
  describe GunBroker::Item do
4
4
 
5
5
  let(:attrs) { JSON.parse(response_fixture('item')) }
6
+ let(:item) { GunBroker::Item.new(attrs) }
6
7
 
7
8
  it 'should have an #id' do
8
- item = GunBroker::Item.new(attrs)
9
9
  expect(item.id).to eq(attrs['itemID'])
10
10
  end
11
11
 
12
12
  context '#[]' do
13
13
  it 'should return the value from @attrs' do
14
- item = GunBroker::Item.new(attrs)
15
14
  attrs.each { |k, v| expect(item[k]).to eq(v) }
16
15
  end
17
16
  end
18
17
 
18
+ context '#attributes' do
19
+ it 'should provide access to @attrs' do
20
+ expect(item.attributes['title']).to eq(attrs['title'])
21
+ end
22
+ end
23
+
24
+ context '#category' do
25
+ it 'should return the Category' do
26
+ # Mock up the Category.
27
+ category = GunBroker::Category.new({
28
+ 'categoryID' => attrs['categoryID'],
29
+ 'categoryName' => attrs['categoryName'],
30
+ })
31
+
32
+ expect(GunBroker::Category).to receive(:find).with(attrs['categoryID']).and_return(category)
33
+ expect(item.category).to eq(category)
34
+ end
35
+ end
36
+
37
+ context '#title' do
38
+ it 'should return the item title' do
39
+ expect(item.title).to eq(attrs['title'])
40
+ end
41
+ end
42
+
19
43
  context '.find' do
20
- let(:attrs) { JSON.parse(response_fixture('item')) }
21
44
  let(:endpoint) { [GunBroker::API::GUNBROKER_API, "/Items/#{attrs['itemID']}"].join }
22
45
 
23
46
  context 'on success' do
@@ -34,12 +57,39 @@ describe GunBroker::Item do
34
57
  end
35
58
 
36
59
  context 'on failure' do
37
- it 'should raise an exception' do
60
+ it 'should return nil' do
61
+ stub_request(:get, endpoint)
62
+ .with(headers: headers)
63
+ .to_return(body: response_fixture('empty'), status: 404)
64
+
65
+ expect(GunBroker::Item.find(attrs['itemID'])).to be_nil
66
+ end
67
+ end
68
+ end
69
+
70
+ context '.find!' do
71
+ let(:endpoint) { [GunBroker::API::GUNBROKER_API, "/Items/#{attrs['itemID']}"].join }
72
+
73
+ context 'on success' do
74
+ it 'returns an Item' do
75
+ stub_request(:get, endpoint)
76
+ .with(headers: headers)
77
+ .to_return(body: response_fixture('item'))
78
+
79
+ id = attrs['itemID']
80
+ item = GunBroker::Item.find!(id)
81
+ expect(item).to be_a(GunBroker::Item)
82
+ expect(item.id).to eq(id)
83
+ end
84
+ end
85
+
86
+ context 'on failure' do
87
+ it 'should raise GunBroker::Error::NotFound' do
38
88
  stub_request(:get, endpoint)
39
89
  .with(headers: headers)
40
90
  .to_return(body: response_fixture('empty'), status: 404)
41
91
 
42
- expect { GunBroker::Item.find(attrs['itemID']) }.to raise_error(GunBroker::Error::RequestError)
92
+ expect { GunBroker::Item.find!(attrs['itemID']) }.to raise_error(GunBroker::Error::NotFound)
43
93
  end
44
94
  end
45
95
  end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe GunBroker::ItemsDelegate do
3
+ describe GunBroker::User::ItemsDelegate do
4
4
  let(:username) { 'test-user' }
5
5
  let(:token) { 'test-user-access-token' }
6
6
 
7
7
  let(:user) { GunBroker::User.new(username, token: token) }
8
- let(:delegate) { GunBroker::ItemsDelegate.new(user) }
8
+ let(:delegate) { GunBroker::User::ItemsDelegate.new(user) }
9
9
 
10
10
  context '#all' do
11
11
  let(:endpoint) { [GunBroker::API::GUNBROKER_API, '/Items'].join }
@@ -117,7 +117,8 @@ describe GunBroker::User do
117
117
  )
118
118
  .to_return(body: response_fixture('contact_info'))
119
119
 
120
- expect(user.contact_info).to eq(JSON.parse(response_fixture('contact_info')))
120
+ contact_info = JSON.parse(response_fixture('contact_info'))
121
+ expect(user.contact_info['userID']).to eq(contact_info['userID'])
121
122
  end
122
123
  end
123
124
 
@@ -140,7 +141,7 @@ describe GunBroker::User do
140
141
  context '#items' do
141
142
  it 'should return an ItemsDelegate instance' do
142
143
  user = GunBroker::User.new(username, token: token)
143
- expect(user.items).to be_a(GunBroker::ItemsDelegate)
144
+ expect(user.items).to be_a(GunBroker::User::ItemsDelegate)
144
145
  end
145
146
  end
146
147
 
@@ -25,4 +25,23 @@ describe GunBroker do
25
25
  end
26
26
  end
27
27
 
28
+ context '.time' do
29
+ before(:all) do
30
+ GunBroker.dev_key = 'test-dev-key'
31
+ end
32
+
33
+ let(:endpoint) { [GunBroker::API::GUNBROKER_API, '/GunBrokerTime'].join }
34
+ let(:response) { JSON.parse(response_fixture('time')) }
35
+
36
+ it 'should return the GunBroker time' do
37
+ stub_request(:get, endpoint)
38
+ .with(headers: headers)
39
+ .to_return(body: response_fixture('time'))
40
+
41
+ time = GunBroker.time
42
+ expect(time['gunBrokerTime']).to eq(response['gunBrokerTime'])
43
+ expect(time['gunBrokerVersion']).to eq(response['gunBrokerVersion'])
44
+ end
45
+ end
46
+
28
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gun_broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,9 +102,11 @@ files:
102
102
  - lib/gun_broker/category.rb
103
103
  - lib/gun_broker/error.rb
104
104
  - lib/gun_broker/item.rb
105
- - lib/gun_broker/items_delegate.rb
105
+ - lib/gun_broker/item/constants.rb
106
+ - lib/gun_broker/response.rb
106
107
  - lib/gun_broker/token_header.rb
107
108
  - lib/gun_broker/user.rb
109
+ - lib/gun_broker/user/items_delegate.rb
108
110
  - lib/gun_broker/version.rb
109
111
  - spec/fixtures/authenticate.json
110
112
  - spec/fixtures/categories.json
@@ -115,11 +117,14 @@ files:
115
117
  - spec/fixtures/item.json
116
118
  - spec/fixtures/items.json
117
119
  - spec/fixtures/not_authorized.json
120
+ - spec/fixtures/test.json
121
+ - spec/fixtures/time.json
118
122
  - spec/gun_broker/api_spec.rb
119
123
  - spec/gun_broker/category_spec.rb
124
+ - spec/gun_broker/item/constants_spec.rb
120
125
  - spec/gun_broker/item_spec.rb
121
- - spec/gun_broker/items_delegate_spec.rb
122
126
  - spec/gun_broker/token_header_spec.rb
127
+ - spec/gun_broker/user/items_delegate_spec.rb
123
128
  - spec/gun_broker/user_spec.rb
124
129
  - spec/gun_broker_spec.rb
125
130
  - spec/spec_helper.rb
@@ -159,11 +164,14 @@ test_files:
159
164
  - spec/fixtures/item.json
160
165
  - spec/fixtures/items.json
161
166
  - spec/fixtures/not_authorized.json
167
+ - spec/fixtures/test.json
168
+ - spec/fixtures/time.json
162
169
  - spec/gun_broker/api_spec.rb
163
170
  - spec/gun_broker/category_spec.rb
171
+ - spec/gun_broker/item/constants_spec.rb
164
172
  - spec/gun_broker/item_spec.rb
165
- - spec/gun_broker/items_delegate_spec.rb
166
173
  - spec/gun_broker/token_header_spec.rb
174
+ - spec/gun_broker/user/items_delegate_spec.rb
167
175
  - spec/gun_broker/user_spec.rb
168
176
  - spec/gun_broker_spec.rb
169
177
  - spec/spec_helper.rb
@@ -1,98 +0,0 @@
1
- require 'gun_broker/token_header'
2
-
3
- module GunBroker
4
- # Used to scope {Item} lookup by {User}.
5
- class ItemsDelegate
6
-
7
- include GunBroker::TokenHeader
8
-
9
- # @param user [User] A {User} instance to scope items by.
10
- def initialize(user)
11
- @user = user
12
- end
13
-
14
- # Returns all the User's items.
15
- # @note {API#get! GET} /Items
16
- # @raise [GunBroker::Error::NotAuthorized] If the {User#token} isn't valid.
17
- # @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response).
18
- # @return [Array<Item>]
19
- def all
20
- response = GunBroker::API.get('/Items', { 'SellerName' => @user.username }, token_header(@user.token))
21
- items_from_results(response['results'])
22
- end
23
-
24
- # Returns all the items the User has bid on.
25
- # @note {API#get! GET} /ItemsBidOn
26
- # @raise (see #all)
27
- # @return [Array<Item>]
28
- def bid_on
29
- response = GunBroker::API.get('/ItemsBidOn', {}, token_header(@user.token))
30
- items_from_results(response['results'])
31
- end
32
-
33
- # Finds a specific User's Item by ID. Calls {Item.find} to get full Item details.
34
- # @raise (see #all)
35
- # @return [Item] Returns the Item or `nil` if no Item found.
36
- def find(item_id)
37
- # HACK: This has to filter through `#all`, since the GunBroker API currently has no way to scope the `/Items/{itemID}` endpoint by user.
38
- if all.select { |item| item.id.to_s == item_id.to_s }.first
39
- GunBroker::Item.find(item_id)
40
- else
41
- nil
42
- end
43
- end
44
-
45
- # Same as {#find} but raises GunBroker::Error::NotFound if no item is found.
46
- # @raise (see #all)
47
- # @raise [GunBroker::Error::NotFound] If the User has no Item with `item_id`.
48
- # @return [Item] Returns the Item or `nil` if no Item found.
49
- def find!(item_id)
50
- item = find(item_id)
51
- raise GunBroker::Error::NotFound.new("Couldn't find item with ID '#{item_id}'") if item.nil?
52
- item
53
- end
54
-
55
- # Items the User has bid on, but not won.
56
- # @note {API#get! GET} /ItemsNotWon
57
- # @raise (see #all)
58
- # @return [Array<Item>]
59
- def not_won
60
- response = GunBroker::API.get('/ItemsNotWon', {}, token_header(@user.token))
61
- items_from_results(response['results'])
62
- end
63
-
64
- # Items the User has sold.
65
- # @note {API#get! GET} /ItemsSold
66
- # @raise (see #all)
67
- # @return [Array<Item>]
68
- def sold
69
- response = GunBroker::API.get('/ItemsSold', {}, token_header(@user.token))
70
- items_from_results(response['results'])
71
- end
72
-
73
- # Items that were listed, but not sold.
74
- # @note {API#get! GET} /ItemsUnsold
75
- # @raise (see #all)
76
- # @return [Array<Item>]
77
- def unsold
78
- response = GunBroker::API.get('/ItemsUnsold', {}, token_header(@user.token))
79
- items_from_results(response['results'])
80
- end
81
-
82
- # Items the User has won.
83
- # @note {API#get! GET} /ItemsWon
84
- # @raise (see #all)
85
- # @return [Array<Item>]
86
- def won
87
- response = GunBroker::API.get('/ItemsWon', {}, token_header(@user.token))
88
- items_from_results(response['results'])
89
- end
90
-
91
- private
92
-
93
- def items_from_results(results)
94
- results.map { |result| GunBroker::Item.new(result) }
95
- end
96
-
97
- end
98
- end