data-com-api 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NWY4NzZiOGVlYjg0MDJkM2ZjNjMyZDIxMzM5Zjg2ZWVlN2RiY2Y0ZA==
4
+ MzFkNTgyOTc3YjczMzc2MWZiYzE3ODliZGI2YzJkN2I4YWRiNmQxNA==
5
5
  data.tar.gz: !binary |-
6
- ZGE3NGQ3NGRmNWYwYjFlYjU0ODgwYjkwNDhkODY2NzJlNjgxMjIzZQ==
6
+ ZDI0ZGVjYmNkODgyNTgyYWFhYjY5Y2QyMGM1ZWIzNzdhMGFhOWE1Mw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGQ5YmQ0NTgyNDYxN2QwMDhjMTY1ZDE3MDdlNzJlNWIwMmY1YmNlZTMzZGY5
10
- YmQ3ZDQ5ZThjN2M1NjhjNDBiYzJiMzRiYmE1YjViMDZkNmUyMzkxMDY1MGVj
11
- ZmIwODAwNGQ1ZDUzMWZhNWEwMmYzMTNmODNiNDU3NjQxNzFkZDU=
9
+ M2YzY2M3NDUxNjhhYjg1MjAxNGZhZWI0N2U0MTg3NzgyZTAwMWM1ZjMyZDE4
10
+ YTcwYWNlYmVhZTg1MDc2ZjY3YzljZDdjOGU2MDY5YTRlMjBlYTg0YWI0MzQ4
11
+ ZTM2ZWEyNDdmYTM5Y2ZkNDc1OThkMjI3NmMxYTkyZjEzYzNhMWU=
12
12
  data.tar.gz: !binary |-
13
- MzU5NzYxYzAwZDkyMGQ3OGZhNTIxZTVjZmU4ZDAzMWNjZGU4YWIxZGVhYjIy
14
- MzJkNDU0NzE1NjAxZDhhMTE4MjY2ZjU1NDE0MzIwY2UxNmFmY2NjODRhOGRm
15
- YzVjMjQyMDcyYTgxZjliZGIzMGU5MzNhZWM0OTY3NjcyZWVjOWI=
13
+ NjlhNjgzMWQzYjUzODZjNmI2Mjg3Zjg2M2Q3NDE4MWMzZjNkMmFlMDcyNWIw
14
+ NTY0NzY4NWY5OTM4YmNhYTkwMDA0ZTQwOWRlZmIxNDk3NWE5NTAwMDBhMDEz
15
+ OTI1ZjdiOTcxM2M1NzFkY2EwOTNkYzdjZGYzMGRkNmQyYjEwY2M=
data/README.md CHANGED
@@ -6,7 +6,7 @@ Ruby bindings for Data.com API ( Salesforce, ex Jigsaw ).
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'data-com-api', '~> 0.1.3'
9
+ gem 'data-com-api', '~> 0.2.0'
10
10
 
11
11
  And then execute:
12
12
 
@@ -55,6 +55,9 @@ requesting `size`, `all`, `each` and such (keep reading).
55
55
  The parameters accepted by this method are the keys of the [DataComApi::QueryParameters](https://github.com/Fire-Dragon-DoL/data-com-api/blob/master/lib/data-com-api/query_parameters.rb). Notice that you can use the key specified as `:from` for a more ruby-like
56
56
  syntax.
57
57
 
58
+ **start_at_offset** and **end_at_offset**: This two new parameters allow your response to start fetching data at specified offset or end earlier (if
59
+ `end_at_offset` is bigger than max fetchable records, it will be ignored and code will handle things in standard way).
60
+
58
61
  The returned object is a `DataComApi::SearchContact` which is mostly a [DataComApi::SearchBase](https://github.com/Fire-Dragon-DoL/data-com-api/blob/master/lib/data-com-api/responses/search_base.rb) instance with the following main methods:
59
62
  - `size` which returns the `totalHits` field from the response
60
63
  (it will perform a request only if none performed)
@@ -5,9 +5,22 @@ module DataComApi
5
5
  # Abstract class
6
6
  class SearchBase < Base
7
7
 
8
+ OFFSET_KEYS = [:start_at_offset, :end_at_offset].freeze
8
9
 
10
+ # Options accept 2 special params: :start_at_offset and :end_at_offset
11
+ # which is where the records will be start being fetched from
9
12
  def initialize(api_client, received_options)
10
- @options = received_options
13
+ @start_at_offset = 0
14
+ @end_at_offset = nil
15
+
16
+ unless received_options[OFFSET_KEYS[0]].nil?
17
+ @start_at_offset = received_options[OFFSET_KEYS[0]].to_i
18
+ end
19
+ unless received_options[OFFSET_KEYS[1]].nil?
20
+ @end_at_offset = received_options[OFFSET_KEYS[1]].to_i
21
+ end
22
+
23
+ @options = received_options.reject { |key| OFFSET_KEYS.include? key.to_sym }
11
24
  super(api_client)
12
25
 
13
26
  # Cache pagesize, MUST NOT change between requests
@@ -56,9 +69,9 @@ module DataComApi
56
69
  end
57
70
 
58
71
  def each_with_index
59
- total_records = 0
60
72
  records_per_previous_page = page_size
61
- current_offset = 0
73
+ current_offset = @start_at_offset
74
+ total_records = current_offset
62
75
 
63
76
  loop do
64
77
  break if current_offset > self.real_max_offset
@@ -84,6 +97,11 @@ module DataComApi
84
97
 
85
98
  @real_max_offset = client.max_offset
86
99
  @real_max_offset = @real_max_offset - (@real_max_offset % page_size)
100
+ unless @end_at_offset.nil? || @real_max_offset < @end_at_offset
101
+ @real_max_offset = @end_at_offset
102
+ end
103
+
104
+ @real_max_offset
87
105
  end
88
106
 
89
107
  alias_method :to_a, :all
@@ -1,3 +1,3 @@
1
1
  module DataComApi
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -159,6 +159,68 @@ describe DataComApi::Client do
159
159
 
160
160
  end
161
161
 
162
+ context "when starting at different offset" do
163
+
164
+ describe "#each" do
165
+ before do
166
+ DataComApiStubRequests.stub_search_contact(
167
+ page_size: client.page_size,
168
+ total_hits: total_contacts_count
169
+ )
170
+ stub_request(
171
+ :get,
172
+ URI.join(
173
+ DataComApi::Client.base_uri, DataComApi::ApiURI.search_contact
174
+ ).to_s
175
+ ).with(
176
+ query: hash_including({
177
+ 'offset' => '10',
178
+ 'pageSize' => client.page_size.to_s
179
+ })
180
+ ).to_return(
181
+ body: FactoryGirl.build(
182
+ :data_com_search_contact_response,
183
+ page_size: client.page_size,
184
+ totalHits: total_contacts_count
185
+ ).to_json
186
+ )
187
+ end
188
+
189
+ let!(:total_contacts_count) { 10 }
190
+
191
+ it "is an array containing all records possible for request" do
192
+ start_at_offset = 2
193
+ response = client.search_contact(start_at_offset: start_at_offset)
194
+ contacts_count = 0
195
+
196
+ response.each { contacts_count += 1 }
197
+ expect(contacts_count).to eq(total_contacts_count - start_at_offset)
198
+ end
199
+
200
+ context "when ending at different offset" do
201
+
202
+ it "is an array containing all records possible for request" do
203
+ start_at_offset = 2
204
+ # XXX: BE EXTREMELY CAREFUL, use an odd number (0 counting as 1 record)
205
+ # otherwise test will fail because of the way I mocked web requests
206
+ # which are returning always 2 records, even if less are returned
207
+ end_at_offset = 7
208
+ contacts_count = 0
209
+ response = client.search_contact(
210
+ start_at_offset: start_at_offset,
211
+ end_at_offset: end_at_offset
212
+ )
213
+
214
+ response.each { contacts_count += 1 }
215
+ expect(contacts_count).to eq((end_at_offset + 1) - start_at_offset)
216
+ end
217
+
218
+ end
219
+
220
+ end
221
+
222
+ end
223
+
162
224
  end
163
225
 
164
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data-com-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fire-Dragon-DoL