gun_broker 1.2.1.1 → 1.3.0

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: 320dc1e2ff0d54dbfebd167be835b5c0f9e6125f
4
- data.tar.gz: 7f5cc2cb8aa382175ff9dda7e9fe0f335b52d2ad
3
+ metadata.gz: d6a5514db1a0faf10391363483a843c4566e5d8a
4
+ data.tar.gz: a249bf76f5d92a7ad4fba9a1d87d798eec830512
5
5
  SHA512:
6
- metadata.gz: ba0685164c1e8cc0023a9cd57df64a0d636cd7571ca2ba768311cbc4a40dbb2f92313876eb871d50493e55ec9954e93e507ba0e43ebe2f18f0236080db645fe4
7
- data.tar.gz: 9d8236d50a5cb8f91709d1af63afa5bd6593942760de6082c2bce84cb171d350be5dced8505dbfad3c4c27fdb10c0fefc761d0d7690163e33f520076099e2307
6
+ metadata.gz: fa2b60ad095be332d187c3038fd344de3d3b269d8bea02bda64b83fa2cb925556ed850ed9f05b9e99ec3f8fbabdbe95ba3173572a297939710bbaf0f743597a4
7
+ data.tar.gz: c5ec2af3222e2b7239f52caff4da01c69c6e44777ee6a86d14910f5b391a4b2d4a5c47984542a37f1e6a474f2653316cb3159b6a4ad84920409f9d7e39989fb9
data/README.md CHANGED
@@ -114,6 +114,19 @@ GunBroker::Item.find(123)
114
114
  To raise a `GunBroker::Error::NotFound` exception if no Item can be found, use `Item.find!`.
115
115
 
116
116
 
117
+ ### GunBroker::ItemsAsPage
118
+
119
+ Represents a page of items (listings) on GunBroker, allowing for querying to be done in chunks to prevent memory leaks. The `ItemsAsPage#fetch_items` method fetches the associated items for the given page.
120
+
121
+ ```ruby
122
+ items_as_pages.each do |page_of_items|
123
+ page_of_items.fetch_items.each do |item|
124
+ puts item.id
125
+ end
126
+ end
127
+ ```
128
+
129
+
117
130
  ### GunBroker::Category
118
131
 
119
132
  Returns GunBroker category responses. To get an array of all categories, call `Category.all`.
@@ -5,6 +5,7 @@ require 'gun_broker/category'
5
5
  require 'gun_broker/error'
6
6
  require 'gun_broker/feedback'
7
7
  require 'gun_broker/item'
8
+ require 'gun_broker/items_as_page'
8
9
  require 'gun_broker/response'
9
10
  require 'gun_broker/user'
10
11
 
@@ -0,0 +1,22 @@
1
+ module GunBroker
2
+ # Represents a page of GunBroker items (listings).
3
+ class ItemsAsPage
4
+
5
+ # @param attrs [Hash] The attributes required to fetch items from the API.
6
+ def initialize(attributes = {})
7
+ @attributes = attributes
8
+ end
9
+
10
+ # @return [Array<Item>]
11
+ def fetch_items
12
+ @attributes[:params].merge!({
13
+ 'PageIndex' => @attributes[:page_index],
14
+ 'PageSize' => @attributes[:page_size]
15
+ })
16
+ response = GunBroker::API.get(@attributes[:endpoint], @attributes[:params], @attributes[:token_header])
17
+
18
+ response['results'].map { |result| GunBroker::Item.new(result) }
19
+ end
20
+
21
+ end
22
+ end
@@ -1,5 +1,6 @@
1
1
  require 'gun_broker/token_header'
2
2
  require 'gun_broker/user/items_delegate'
3
+ require 'gun_broker/user/items_as_pages_delegate'
3
4
 
4
5
  module GunBroker
5
6
  # Represents a GunBroker User.
@@ -86,6 +87,13 @@ module GunBroker
86
87
  @items_delegate ||= ItemsDelegate.new(self)
87
88
  end
88
89
 
90
+ # (see ItemsAsPagesDelegate)
91
+ # See the {ItemsAsPagesDelegate} docs.
92
+ # @return [ItemsAsPagesDelegate]
93
+ def items_as_pages(options = {})
94
+ @items_as_pages_delegate ||= ItemsAsPagesDelegate.new(self, options)
95
+ end
96
+
89
97
  private
90
98
 
91
99
  # @return [Boolean] `true` if `@username` is present and either `@password` *or* `@token` is present.
@@ -0,0 +1,98 @@
1
+ require 'gun_broker/token_header'
2
+
3
+ module GunBroker
4
+ class User
5
+ # Used to scope {ItemsAsPage} actions by {User}.
6
+ class ItemsAsPagesDelegate
7
+
8
+ include GunBroker::TokenHeader
9
+
10
+ # @param user [User] A {User} instance to scope item pages by.
11
+ # @param options [Hash] { items_per_page => <number of desired items per page> (Integer) }.
12
+ def initialize(user, options = {})
13
+ max_page_size = GunBroker::API::PAGE_SIZE
14
+ @user = user
15
+ @items_per_page = options.fetch(:items_per_page, max_page_size)
16
+
17
+ if @items_per_page > max_page_size
18
+ raise ArgumentError.new("`items_per_page` may not exceed #{max_page_size}")
19
+ end
20
+ end
21
+
22
+ # Returns pages for all the the User's items (both selling and not selling).
23
+ # @note {API#get! GET} /Items
24
+ # @return [Array<ItemsAsPage>]
25
+ def all
26
+ # NOTE: this endpoint will not return items that were sold
27
+ @all ||= build_pages_for(:Items, { 'SellerName' => @user.username })
28
+ end
29
+
30
+ # Returns pages for all items the User has bid on.
31
+ # @note {API#get! GET} /ItemsBidOn
32
+ # @return [Array<ItemsAsPage>]
33
+ def bid_on
34
+ @bid_on ||= build_pages_for(:ItemsBidOn)
35
+ end
36
+
37
+ # Returns pages for items the User has bid on, but not won.
38
+ # @note {API#get! GET} /ItemsNotWon
39
+ # @return [Array<ItemsAsPage>]
40
+ def not_won
41
+ @not_won ||= build_pages_for(:ItemsNotWon)
42
+ end
43
+
44
+ # Returns pages for items that are currently selling.
45
+ # @note {API#get! GET} /Items
46
+ # @return [Array<ItemsAsPage>]
47
+ def selling
48
+ @selling ||= build_pages_for(:Items, { 'SellerName' => @user.username })
49
+ end
50
+
51
+ # Returns pages for items the User has sold.
52
+ # @note {API#get! GET} /ItemsSold
53
+ # @return [Array<ItemsAsPage>]
54
+ def sold
55
+ @sold ||= build_pages_for(:ItemsSold)
56
+ end
57
+
58
+ # Returns pages for items that were listed, but not sold.
59
+ # @note {API#get! GET} /ItemsUnsold
60
+ # @return [Array<ItemsAsPage>]
61
+ def unsold
62
+ @unsold ||= build_pages_for(:ItemsUnsold)
63
+ end
64
+
65
+ # Returns pages for items the User has won.
66
+ # @note {API#get! GET} /ItemsWon
67
+ # @return [Array<ItemsAsPage>]
68
+ def won
69
+ @won ||= build_pages_for(:ItemsWon)
70
+ end
71
+
72
+ private
73
+
74
+ def build_pages_for(endpoint, params = {})
75
+ endpoint = ['/', endpoint.to_s].join
76
+ _token_header = token_header(@user.token)
77
+ response = GunBroker::API.get(endpoint, params.merge({ 'PageSize' => 1 }), _token_header)
78
+ number_of_pages = (response['count'] / @items_per_page.to_f).ceil
79
+ items_as_pages = []
80
+
81
+ number_of_pages.times do |page_number|
82
+ page_number += 1
83
+ attrs = {
84
+ page_size: @items_per_page,
85
+ page_index: page_number,
86
+ endpoint: endpoint,
87
+ params: params,
88
+ token_header: _token_header
89
+ }
90
+
91
+ items_as_pages << GunBroker::ItemsAsPage.new(attrs)
92
+ end
93
+
94
+ items_as_pages
95
+ end
96
+ end
97
+ end
98
+ end
@@ -19,20 +19,14 @@ module GunBroker
19
19
  # @return [Array<Item>]
20
20
  def all
21
21
  # NOTE: this endpoint will not return items that were sold
22
- endpoint = :Items
23
- params = { 'SellerName' => @user.username }
24
-
25
- @all ||= fetch_items(endpoint, params)
22
+ @all ||= fetch_items(:Items, { 'SellerName' => @user.username })
26
23
  end
27
24
 
28
25
  # Returns all the items the User has bid on.
29
26
  # @note {API#get! GET} /ItemsBidOn
30
- # @raise (see #all)
31
27
  # @return [Array<Item>]
32
28
  def bid_on
33
- endpoint = :ItemsBidOn
34
-
35
- @bid_on ||= fetch_items(endpoint)
29
+ @bid_on ||= fetch_items(:ItemsBidOn)
36
30
  end
37
31
 
38
32
  # Sends a multipart/form-data POST request to create an Item with the given `attributes`.
@@ -66,7 +60,6 @@ module GunBroker
66
60
  end
67
61
 
68
62
  # Same as {#find} but raises GunBroker::Error::NotFound if no item is found.
69
- # @raise (see #all)
70
63
  # @raise [GunBroker::Error::NotFound] If the User has no Item with `item_id`.
71
64
  # @return [Item] Returns the Item or `nil` if no Item found.
72
65
  def find!(item_id)
@@ -77,12 +70,9 @@ module GunBroker
77
70
 
78
71
  # Items the User has bid on, but not won.
79
72
  # @note {API#get! GET} /ItemsNotWon
80
- # @raise (see #all)
81
73
  # @return [Array<Item>]
82
74
  def not_won
83
- endpoint = :ItemsNotWon
84
-
85
- @not_won ||= fetch_items(endpoint)
75
+ @not_won ||= fetch_items(:ItemsNotWon)
86
76
  end
87
77
 
88
78
  # Returns Items that are currently selling.
@@ -92,40 +82,35 @@ module GunBroker
92
82
  # @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response).
93
83
  # @return [Array<Item>]
94
84
  def selling(options = {})
95
- endpoint = :Items
96
85
  params = {
97
- 'ItemID' => (options[:item_id] || options["ItemID"]),
98
- 'SellerName' => @user.username,
86
+ 'ItemID' => (options[:item_id] || options["ItemID"]),
87
+ 'SellerName' => @user.username,
99
88
  }.delete_if { |k, v| v.nil? }
100
89
 
101
- @selling ||= fetch_items(endpoint, params)
90
+ @selling ||= fetch_items(:Items, params)
102
91
  end
103
92
 
104
93
  # Items the User has sold.
105
94
  # @param options [Hash] {ItemID=>ItemID}.
106
95
  # @note {API#get! GET} /ItemsSold
107
- # @raise (see #all)
108
96
  # @return [Array<Item>]
109
97
  def sold(options = {})
110
- endpoint = :ItemsSold
111
98
  params = {
112
- 'ItemID' => (options[:item_id] || options["ItemID"])
99
+ 'ItemID' => (options[:item_id] || options["ItemID"])
113
100
  }.delete_if { |k, v| v.nil? }
114
101
 
115
- @sold ||= fetch_items(endpoint, params)
102
+ @sold ||= fetch_items(:ItemsSold, params)
116
103
  end
117
104
 
118
105
  # Items that were listed, but not sold.
119
106
  # @note {API#get! GET} /ItemsUnsold
120
- # @raise (see #all)
121
107
  # @return [Array<Item>]
122
108
  def unsold(options = {})
123
- endpoint = :ItemsUnsold
124
109
  params = {
125
- 'ItemID' => (options[:item_id] || options["ItemID"])
110
+ 'ItemID' => (options[:item_id] || options["ItemID"])
126
111
  }.delete_if { |k, v| v.nil? }
127
112
 
128
- @unsold ||= fetch_items(endpoint, params)
113
+ @unsold ||= fetch_items(:ItemsUnsold, params)
129
114
  end
130
115
 
131
116
  # Updates an {Item} with the given attributes.
@@ -150,12 +135,9 @@ module GunBroker
150
135
 
151
136
  # Items the User has won.
152
137
  # @note {API#get! GET} /ItemsWon
153
- # @raise (see #all)
154
138
  # @return [Array<Item>]
155
139
  def won
156
- endpoint = :ItemsWon
157
-
158
- @won ||= fetch_items(endpoint)
140
+ @won ||= fetch_items(:ItemsWon)
159
141
  end
160
142
 
161
143
  private
@@ -164,16 +146,16 @@ module GunBroker
164
146
  endpoint = ['/', endpoint.to_s].join
165
147
  params.merge!({ 'PageSize' => GunBroker::API::PAGE_SIZE })
166
148
  response = GunBroker::API.get(endpoint, params, token_header(@user.token))
167
- pages = (response['count'] / GunBroker::API::PAGE_SIZE.to_f).ceil
149
+ number_of_pages = (response['count'] / GunBroker::API::PAGE_SIZE.to_f).ceil
168
150
 
169
- if pages > 1
151
+ if number_of_pages > 1
170
152
  _items_from_results = items_from_results(response['results'])
171
153
 
172
- pages.times do |page|
173
- page += 1
174
- next if page == 1
154
+ number_of_pages.times do |page_number|
155
+ page_number += 1
156
+ next if page_number == 1
175
157
 
176
- params.merge!({ 'PageIndex' => page })
158
+ params.merge!({ 'PageIndex' => page_number })
177
159
  response = GunBroker::API.get(endpoint, params, token_header(@user.token))
178
160
  _items_from_results.concat(items_from_results(response['results']))
179
161
  end
@@ -1,3 +1,3 @@
1
1
  module GunBroker
2
- VERSION = "1.2.1.1"
2
+ VERSION = "1.3.0"
3
3
  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: 1.2.1.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-21 00:00:00.000000000 Z
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,9 +136,11 @@ files:
136
136
  - lib/gun_broker/feedback.rb
137
137
  - lib/gun_broker/item.rb
138
138
  - lib/gun_broker/item/constants.rb
139
+ - lib/gun_broker/items_as_page.rb
139
140
  - lib/gun_broker/response.rb
140
141
  - lib/gun_broker/token_header.rb
141
142
  - lib/gun_broker/user.rb
143
+ - lib/gun_broker/user/items_as_pages_delegate.rb
142
144
  - lib/gun_broker/user/items_delegate.rb
143
145
  - lib/gun_broker/version.rb
144
146
  - spec/fixtures/authenticate.json