spot-gps 0.0.2 → 0.1.0

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: 13e4a0abb43ad3882974975b75c91103d6d0be42
4
- data.tar.gz: 164e08de3895f9d0f2dc8b666e6ba769f34941be
3
+ metadata.gz: ae8f932cea97f131527f1b2f24b693b21c78bfaa
4
+ data.tar.gz: 506fa0a9ce404eb350cd8e7847d15b8b0099a49a
5
5
  SHA512:
6
- metadata.gz: be02344cbfec73c32678b5d16b1829179ee9be435acdf5aa5957e1616152298fd69260f14783089b1b967f6b397af0ef2c528273854fba5d5068e421f049c7ac
7
- data.tar.gz: 5c0207c3806fb992c1e7ba0a046442539c906674c886d704d829b4dd2196fa8e82b9b89ea06310dfd378d37ba1065be92ea2f9101a9cc92cf860493903578a0c
6
+ metadata.gz: c81515b2b992f025958fbbd3de61d9fb06b6cab5d8b48c2e10bfde32d072beb2cb9808c6a6761cd4d2fb5e8f501689715f771d24d98e006872256d8113fac7f9
7
+ data.tar.gz: daad07e097b8decddb8c78adfd9c95b1804dabb0839108a9e5d0a3c5ca524d3768221b4c1975cc67deb963773f1cb3c196924a5b86f55b382c0b392889eabce4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.1.0, 10 July 2016
2
+
3
+ - Move old `all` functionality to `list`
4
+ - Introduce new `all` method, that lazily paginates through all resources
5
+
1
6
  ## v0.0.2, 10 July 2016
2
7
 
3
8
  - Wrap responses in SPOT::Resources::Message objects, making them easier to work
data/README.md CHANGED
@@ -41,18 +41,27 @@ Currently, the SPOT API only supports a single `messages` resource.
41
41
  Messages are communications sent from a SPOT device.
42
42
 
43
43
  ```ruby
44
- api.messages.all # => Returns a (paginated) list of all messages for a feed
44
+ api.messages.all # => Returns a lazily paginated list of messages for a feed
45
+ api.messages.list # => Returns a list of messages for a given page of a feed
45
46
  api.messages.latest # => Returns the most recent message for a feed
46
47
  ```
47
48
 
48
49
  ### Pagination
49
50
 
50
- All resources that support an `all` method also support pagination. By default,
51
- the first page of 50 records are fetched. To fetch subsequent pages (each of 50
52
- records), specify a `page`.
51
+ If you want to get all of the records for a given resource type, you can use the
52
+ `#all` method to get a lazily paginated list. `#all` will deal with making extra
53
+ API requests to paginate through all the data for you:
53
54
 
54
55
  ```ruby
55
- api.messages.all(page: 2)
56
+ api.messages.all.each { |message| puts message.created_at }
57
+ ```
58
+
59
+ Alternatively, you can use the `#list` method to get a specific page of entries.
60
+ By default, the first page of 50 records are fetched. To fetch subsequent pages
61
+ (each of 50 records), specify a `page`.
62
+
63
+ ```ruby
64
+ api.messages.list(page: 2)
56
65
  ```
57
66
 
58
67
  ### Filtering
@@ -60,7 +69,7 @@ api.messages.all(page: 2)
60
69
  The SPOT API supports filtering by date.
61
70
 
62
71
  ```ruby
63
- api.messages.all(start_at: '2014-06-01T00:00:00', end_at: '2015-06-01T00:00:00')
72
+ api.messages.list(start_at: '2014-06-01T00:00:00', end_at: '2015-06-01T00:00:00')
64
73
  ```
65
74
 
66
75
  ### Error Handling
@@ -0,0 +1,39 @@
1
+ module SPOT
2
+ # A class that can take an API LIST query and auto paginate through results
3
+ class Paginator
4
+ def initialize(service:, params: {})
5
+ @service = service
6
+ @params = (params || {}).dup
7
+ end
8
+
9
+ # Get a lazy enumerable for listing data from the API
10
+ def enumerator
11
+ Enumerator.new do |yielder|
12
+ response = get_initial_response
13
+
14
+ loop do
15
+ items = response.records
16
+
17
+ # If there are no records, we're done
18
+ break if items.empty?
19
+
20
+ # Otherwise, iterate through the records...
21
+ items.each { |item| yielder << item }
22
+
23
+ # ...and fetch the next page
24
+ @params ||= {}
25
+ @params[:page] ||= 1
26
+ @params[:page] += 1
27
+
28
+ response = @service.list(**@params)
29
+ end
30
+ end.lazy
31
+ end
32
+
33
+ private
34
+
35
+ def get_initial_response
36
+ @initial_response ||= @service.list(**@params)
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,18 @@
1
1
  module SPOT
2
2
  module Services
3
3
  class Messages < Base
4
- def all(page: nil, start_at: nil, end_at: nil)
4
+ def all(start_at: nil, end_at: nil)
5
+ query_params = {}
6
+ query_params[:startDate] = spot_formatted_time(start_at) if start_at
7
+ query_params[:endDate] = spot_formatted_time(end_at) if end_at
8
+
9
+ SPOT::Paginator.new(
10
+ service: self,
11
+ params: query_params
12
+ ).enumerator
13
+ end
14
+
15
+ def list(page: nil, start_at: nil, end_at: nil)
5
16
  query_params = {}
6
17
  query_params[:start] = start(page) if page
7
18
  query_params[:startDate] = spot_formatted_time(start_at) if start_at
@@ -1,3 +1,3 @@
1
1
  module SPOT
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
data/lib/spot.rb CHANGED
@@ -7,6 +7,7 @@ require 'spot-gps/client'
7
7
  require 'spot-gps/api_response'
8
8
  require 'spot-gps/api_service'
9
9
  require 'spot-gps/list_response'
10
+ require 'spot-gps/paginator'
10
11
  require 'spot-gps/resources/message'
11
12
  require 'spot-gps/services/base'
12
13
  require 'spot-gps/services/messages'
@@ -18,12 +18,12 @@
18
18
  {
19
19
  "@clientUnixTime": "0",
20
20
  "id": 585079373,
21
- "messengerId": "0-2855693",
22
- "messengerName": "Grey's SPOT",
21
+ "messengerId": "0-1234567",
22
+ "messengerName": "My SPOT",
23
23
  "unixTime": 1468164566,
24
24
  "messageType": "OK",
25
- "latitude": 51.54875,
26
- "longitude": -0.0697,
25
+ "latitude": 61.54875,
26
+ "longitude": -3.0697,
27
27
  "modelId": "SPOT3",
28
28
  "showCustomMsg": "Y",
29
29
  "dateTime": "2016-07-10T15:29:26+0000",
@@ -33,13 +33,13 @@
33
33
  },
34
34
  {
35
35
  "@clientUnixTime": "0",
36
- "id": 584982167,
37
- "messengerId": "0-2855693",
38
- "messengerName": "Grey's SPOT",
36
+ "id": 585079373,
37
+ "messengerId": "0-1234567",
38
+ "messengerName": "My SPOT",
39
39
  "unixTime": 1468149143,
40
40
  "messageType": "OK",
41
- "latitude": 51.5489,
42
- "longitude": -0.06988,
41
+ "latitude": 61.54875,
42
+ "longitude": -3.0697,
43
43
  "modelId": "SPOT3",
44
44
  "showCustomMsg": "Y",
45
45
  "dateTime": "2016-07-10T11:12:23+0000",
@@ -5,6 +5,55 @@ describe SPOT::Services::Messages do
5
5
 
6
6
  describe "#all" do
7
7
  subject(:all) { messages.all(args) }
8
+ let(:args) { {} }
9
+
10
+ it { is_expected.to be_a(Enumerator) }
11
+
12
+ context "with records" do
13
+ before do
14
+ stub_url = SPOT.endpoint + 'EXAMPLE_ID/message.json'
15
+ stub_request(:get, stub_url).to_return(
16
+ body: load_fixture('message.json'),
17
+ headers: { 'Content-Type' => 'application/json' }
18
+ )
19
+
20
+ stub_request(:get, stub_url + "?start=50").to_return(
21
+ body: load_fixture('message.json'),
22
+ headers: { 'Content-Type' => 'application/json' }
23
+ )
24
+
25
+ stub_request(:get, stub_url + "?start=100").to_return(
26
+ body: load_fixture('no_messages.json'),
27
+ headers: { 'Content-Type' => 'application/json' }
28
+ )
29
+ end
30
+
31
+ its(:first) { is_expected.to be_a(SPOT::Resources::Message) }
32
+ its("to_a.length") { is_expected.to eq(4) }
33
+
34
+ it "makes the correct number of requests" do
35
+ all.map(&:created_at).to_a
36
+ expect(a_request(:get, /#{Regexp.quote(SPOT.endpoint)}/)).
37
+ to have_been_made.times(3)
38
+ end
39
+ end
40
+
41
+ context "with no records" do
42
+ before do
43
+ stub_url = SPOT.endpoint + 'EXAMPLE_ID/message.json'
44
+ stub_request(:get, stub_url).to_return(
45
+ body: load_fixture('no_messages.json'),
46
+ headers: { 'Content-Type' => 'application/json' }
47
+ )
48
+ end
49
+
50
+ its(:first) { is_expected.to eq(nil) }
51
+ its("to_a.length") { is_expected.to eq(0) }
52
+ end
53
+ end
54
+
55
+ describe "#list" do
56
+ subject(:list) { messages.list(args) }
8
57
 
9
58
  before do
10
59
  stub_url = SPOT.endpoint + 'EXAMPLE_ID/message.json'
@@ -14,7 +63,6 @@ describe SPOT::Services::Messages do
14
63
  )
15
64
  end
16
65
 
17
-
18
66
  context "without any arguments" do
19
67
  let(:args) { {} }
20
68
 
@@ -24,7 +72,7 @@ describe SPOT::Services::Messages do
24
72
  with(path: 'message.json', params: {}).
25
73
  and_call_original
26
74
 
27
- messages.all
75
+ messages.list
28
76
  end
29
77
 
30
78
  it { is_expected.to be_a(SPOT::ListResponse) }
@@ -47,12 +95,12 @@ describe SPOT::Services::Messages do
47
95
  context "with a page parameter" do
48
96
  context "that is invalid" do
49
97
  let(:args) { { page: "invalid" } }
50
- specify { expect { all }.to raise_error(ArgumentError) }
98
+ specify { expect { list }.to raise_error(ArgumentError) }
51
99
  end
52
100
 
53
101
  context "that is zero" do
54
102
  let(:args) { { page: 0 } }
55
- specify { expect { all }.to raise_error(ArgumentError) }
103
+ specify { expect { list }.to raise_error(ArgumentError) }
56
104
  end
57
105
 
58
106
  context "that is greater than zero" do
@@ -64,7 +112,7 @@ describe SPOT::Services::Messages do
64
112
  with(path: 'message.json', params: { start: 0 }).
65
113
  and_call_original
66
114
 
67
- all
115
+ list
68
116
  end
69
117
 
70
118
  context "and greater than 1" do
@@ -76,7 +124,7 @@ describe SPOT::Services::Messages do
76
124
  with(path: 'message.json', params: { start: 50 }).
77
125
  and_call_original
78
126
 
79
- all
127
+ list
80
128
  end
81
129
  end
82
130
  end
@@ -85,7 +133,7 @@ describe SPOT::Services::Messages do
85
133
  context "with a start_at or end_at parameter" do
86
134
  context "that is invalid" do
87
135
  let(:args) { { start_at: "invalid" } }
88
- specify { expect { all }.to raise_error(ArgumentError) }
136
+ specify { expect { list }.to raise_error(ArgumentError) }
89
137
  end
90
138
 
91
139
  context "that is a valid string" do
@@ -99,7 +147,7 @@ describe SPOT::Services::Messages do
99
147
  endDate: '2016-02-01T00:00:00-0000' }).
100
148
  and_call_original
101
149
 
102
- all
150
+ list
103
151
  end
104
152
  end
105
153
 
@@ -113,7 +161,7 @@ describe SPOT::Services::Messages do
113
161
  params: { startDate: '2016-01-01T00:00:00-0000' }).
114
162
  and_call_original
115
163
 
116
- all
164
+ list
117
165
  end
118
166
  end
119
167
 
@@ -127,7 +175,7 @@ describe SPOT::Services::Messages do
127
175
  params: { startDate: '2016-01-01T00:00:00-0000' }).
128
176
  and_call_original
129
177
 
130
- all
178
+ list
131
179
  end
132
180
  end
133
181
 
@@ -141,7 +189,7 @@ describe SPOT::Services::Messages do
141
189
  params: { startDate: '2016-01-01T00:00:00-0000' }).
142
190
  and_call_original
143
191
 
144
- all
192
+ list
145
193
  end
146
194
  end
147
195
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spot-gps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Baker
@@ -89,6 +89,7 @@ files:
89
89
  - lib/spot-gps/client.rb
90
90
  - lib/spot-gps/configuration.rb
91
91
  - lib/spot-gps/list_response.rb
92
+ - lib/spot-gps/paginator.rb
92
93
  - lib/spot-gps/resources/message.rb
93
94
  - lib/spot-gps/services/base.rb
94
95
  - lib/spot-gps/services/messages.rb