spot-gps 0.1.0 → 0.2.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: ae8f932cea97f131527f1b2f24b693b21c78bfaa
4
- data.tar.gz: 506fa0a9ce404eb350cd8e7847d15b8b0099a49a
3
+ metadata.gz: dd30beee45ef852e2ef63c83c1671600b1aef085
4
+ data.tar.gz: fe3e6c3dc00cd6f8ccf2fe00955c121be5919009
5
5
  SHA512:
6
- metadata.gz: c81515b2b992f025958fbbd3de61d9fb06b6cab5d8b48c2e10bfde32d072beb2cb9808c6a6761cd4d2fb5e8f501689715f771d24d98e006872256d8113fac7f9
7
- data.tar.gz: daad07e097b8decddb8c78adfd9c95b1804dabb0839108a9e5d0a3c5ca524d3768221b4c1975cc67deb963773f1cb3c196924a5b86f55b382c0b392889eabce4
6
+ metadata.gz: b9807c61a995b52a9b6ec8cf36ffd10d43b87b222a6b87096216ed0e7e3e90b0abf6e2564ac03c0cc28372b13bf34cbff12d8a894a270527a3220be426677f83
7
+ data.tar.gz: 3794197c9ca65049abefb6483409f5b627fd83d697cb525d7c0a8728145778994794971347ffd6cce1ed8ba67f388e8f18cd6cc1c7b21c22b061bbf47f997fbe
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.2.0, 11 July 2016
2
+
3
+ - `#to_h` now returns a resources cleaned attributes, not its initial input
4
+ - Add `#to_raw_h` method to SPOT::Resources::Message, which replaces `#to_h`
5
+ - Make `#response` available on ListResponse, and document it
6
+
1
7
  ## v0.1.0, 10 July 2016
2
8
 
3
9
  - Move old `all` functionality to `list`
data/README.md CHANGED
@@ -15,7 +15,8 @@ gem 'spot-gps'
15
15
 
16
16
  ## Configuration
17
17
 
18
- There are 3 configuration options:
18
+ Timeout options can be configured globally, or you can rely on the default
19
+ values below:
19
20
 
20
21
  ```ruby
21
22
  SPOT.configure do |config|
@@ -26,7 +27,7 @@ end
26
27
 
27
28
  ## Usage
28
29
 
29
- API calls are made using an instance of the `API` class:
30
+ API calls are made using an instance of the `SPOT::Client` class:
30
31
 
31
32
  ```ruby
32
33
  api = SPOT::Client.new(feed_id: 'FEED_GIID', feed_password: 'OPTIONAL_PASSWORD')
@@ -34,7 +35,7 @@ api = SPOT::Client.new(feed_id: 'FEED_GIID', feed_password: 'OPTIONAL_PASSWORD')
34
35
 
35
36
  ### Resources
36
37
 
37
- Currently, the SPOT API only supports a single `messages` resource.
38
+ Currently, the SPOT API only supports the `messages` resource.
38
39
 
39
40
  #### Messages
40
41
 
@@ -46,6 +47,28 @@ api.messages.list # => Returns a list of messages for a given page of a feed
46
47
  api.messages.latest # => Returns the most recent message for a feed
47
48
  ```
48
49
 
50
+ Each message has the following details available:
51
+
52
+ ```ruby
53
+ message = api.messages.latest
54
+
55
+ message.id # => Integer. ID of the SPOT message
56
+ message.created_at # => Time
57
+ message.type # => String. Message type (e.g., "OK", or "HELP")
58
+ message.latitude # => Float
59
+ message.longitude # => Float
60
+ message.battery_state # => String. Battery state at time of sending (e.g., "GOOD")
61
+ message.hidden # => Boolean. I'm not sure what this is for...
62
+ message.show_custom_message # => Boolean. I'm not sure what this is for...
63
+ message.content # => String
64
+ message.messenger_id # => String. As used when registering your SPOT
65
+ message.messenger_name # => String. As specified when registering your SPOT
66
+ message.messenger_model # => String. E.g., "SPOT3"
67
+ ```
68
+
69
+ You can also call `#to_h` on a message to get its (cleaned) attributes as a
70
+ hash, or `#to_raw_h` to get the attributes as returned by SPOT.
71
+
49
72
  ### Pagination
50
73
 
51
74
  If you want to get all of the records for a given resource type, you can use the
@@ -66,17 +89,37 @@ api.messages.list(page: 2)
66
89
 
67
90
  ### Filtering
68
91
 
69
- The SPOT API supports filtering by date.
92
+ The SPOT API only supports filtering by date. You can do so using either the
93
+ `#list` or `#all` methods.
70
94
 
71
95
  ```ruby
96
+ api.messages.all(start_at: '2014-06-01T00:00:00', end_at: '2015-06-01T00:00:00')
72
97
  api.messages.list(start_at: '2014-06-01T00:00:00', end_at: '2015-06-01T00:00:00')
73
98
  ```
74
99
 
100
+ You can pass the `start_at` and `end_at` parameters as a `String`, `Time`,
101
+ `DateTime` or `Date`, and the gem will handle formatting it correctly for SPOT.
102
+
103
+ ### Raw responses
104
+
105
+ If you'd like to query the un-wrapped response data from SPOT, you can use the
106
+ `#response` method to do so.
107
+
108
+ ```ruby
109
+ message = api.messages.latest
110
+ response = message.response
111
+
112
+ response.body # => Hash. JSON body
113
+ response.headers # => Hash. Headers returned by SPOT
114
+ response.status # => Integer. Status code returned by SPOT
115
+ ```
116
+
75
117
  ### Error Handling
76
118
 
77
119
  TODO: Currently the gem will just raise Faraday errors if anything goes wrong
78
120
  with a request.
79
121
 
122
+
80
123
  ## Contributing
81
124
 
82
125
  1. Fork it ( https://github.com/greysteil/spot-gps/fork )
@@ -16,5 +16,9 @@ module SPOT
16
16
  @resource_class.new(item)
17
17
  end
18
18
  end
19
+
20
+ def response
21
+ @response
22
+ end
19
23
  end
20
24
  end
@@ -14,33 +14,48 @@ module SPOT
14
14
  attr_reader :messenger_name
15
15
  attr_reader :messenger_model
16
16
 
17
- def initialize(object, response = nil)
18
- @object = object
17
+ attr_reader :response
19
18
 
19
+ def initialize(object, response = nil)
20
20
  @id = object.fetch('id')
21
21
  @created_at = Time.at(object.fetch('unixTime'))
22
22
  @type = object.fetch('messageType')
23
23
  @latitude = object.fetch('latitude')
24
24
  @longitude = object.fetch('longitude')
25
25
  @battery_state = object.fetch('batteryState')
26
+ @hidden = object.fetch('hidden') == 1
27
+ @show_custom_message = object.fetch('showCustomMsg') == "Y"
28
+ @content = object.fetch('messageContent')
29
+ @messenger_id = object.fetch('messengerId')
30
+ @messenger_name = object.fetch('messengerName')
31
+ @messenger_model = object.fetch('modelId')
26
32
 
27
- @hidden = object['hidden'] == 1
28
- @show_custom_message = object['showCustomMsg'] == "Y"
29
- @content = object['messageContent']
30
- @messenger_id = object['messengerId']
31
- @messenger_name = object['messengerName']
32
- @messenger_model = object['modelId']
33
-
33
+ @object = object
34
34
  @response = response
35
35
  end
36
36
 
37
- def response
38
- @response
37
+ def to_h
38
+ @hash ||=
39
+ begin
40
+ attribute_ivars =
41
+ (instance_variables - [:@response, :@object, :@hash])
42
+
43
+ attribute_ivars.each_with_object({}) do |ivar, hash|
44
+ hash[ivar.to_s.delete('@').to_sym] = instance_variable_get(ivar)
45
+ end
46
+ end
39
47
  end
40
48
 
41
- def to_h
49
+ def to_raw_h
42
50
  @object
43
51
  end
52
+
53
+ def inspect
54
+ attr_list =
55
+ to_h.map { |key, value| "#{key}: #{value.inspect}" }.join(', ')
56
+
57
+ "#<#{self.class} #{attr_list}>"
58
+ end
44
59
  end
45
60
  end
46
61
  end
@@ -1,3 +1,3 @@
1
1
  module SPOT
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -15,6 +15,7 @@ describe SPOT::ApiResponse do
15
15
  end
16
16
 
17
17
  its(:status) { is_expected.to eq(200) }
18
+ its(:headers) { is_expected.to be_a(Hash) }
18
19
 
19
20
  describe "#body" do
20
21
  context "when the response is JSON" do
@@ -3,7 +3,7 @@
3
3
  "feedMessageResponse": {
4
4
  "count": 2,
5
5
  "feed": {
6
- "id": "1HXUWlonx0AInTM7LAnBoAFEBQ9GWLJ5e",
6
+ "id": "FEED_ID",
7
7
  "name": "Testing",
8
8
  "description": "Testing",
9
9
  "status": "ACTIVE",
@@ -22,7 +22,7 @@ describe SPOT::Resources::Message do
22
22
  end
23
23
 
24
24
  it 'can be initialized from an unenveloped response' do
25
- resource = described_class.new(data)
25
+ resource = described_class.new(data, nil)
26
26
 
27
27
  expect(resource.id).to eq(585079373)
28
28
  expect(resource.created_at).to eq(Time.at(1468164566))
@@ -40,13 +40,39 @@ describe SPOT::Resources::Message do
40
40
 
41
41
  it 'can handle new attributes without erroring' do
42
42
  data['foo'] = 'bar'
43
- expect { described_class.new(data) }.to_not raise_error
43
+ expect { described_class.new(data, nil) }.to_not raise_error
44
44
  end
45
45
 
46
46
  describe '#to_h' do
47
- it 'returns a hash representing the resource' do
48
- expect(described_class.new(data).to_h).to eq(data)
47
+ subject { described_class.new(data, nil).to_h }
48
+ let(:expected_hash) do
49
+ {
50
+ id: 585079373,
51
+ created_at: Time.at(1468164566),
52
+ type: 'OK',
53
+ latitude: 61.54875,
54
+ longitude: -3.0697,
55
+ battery_state: 'GOOD',
56
+ hidden: false,
57
+ show_custom_message: true,
58
+ content: 'Example SPOT message.',
59
+ messenger_id: '0-1234567',
60
+ messenger_name: 'My SPOT',
61
+ messenger_model: 'SPOT3'
62
+ }
49
63
  end
64
+
65
+ it { is_expected.to eq(expected_hash) }
66
+ end
67
+
68
+ describe '#to_aw_h' do
69
+ subject { described_class.new(data, nil).to_raw_h }
70
+ it { is_expected.to eq(data) }
71
+ end
72
+
73
+ describe '#response' do
74
+ subject { described_class.new(data, "response").response }
75
+ it { is_expected.to eq("response") }
50
76
  end
51
77
  end
52
78
  end
@@ -76,6 +76,7 @@ describe SPOT::Services::Messages do
76
76
  end
77
77
 
78
78
  it { is_expected.to be_a(SPOT::ListResponse) }
79
+ its(:response) { is_expected.to be_a(SPOT::ApiResponse) }
79
80
  its("records.first") { is_expected.to be_a(SPOT::Resources::Message) }
80
81
 
81
82
  context "when there are no displayable messages" do
@@ -196,6 +197,7 @@ describe SPOT::Services::Messages do
196
197
  end
197
198
 
198
199
  describe "#latest" do
200
+ subject { messages.latest }
199
201
  before do
200
202
  stub_url = SPOT.endpoint + 'EXAMPLE_ID/latest.json'
201
203
  stub_request(:get, stub_url).to_return(
@@ -213,8 +215,7 @@ describe SPOT::Services::Messages do
213
215
  messages.latest
214
216
  end
215
217
 
216
- it 'wraps the response in a resource' do
217
- expect(messages.latest).to be_a(SPOT::Resources::Message)
218
- end
218
+ it { is_expected.to be_a(SPOT::Resources::Message) }
219
+ its(:response) { is_expected.to be_a(SPOT::ApiResponse) }
219
220
  end
220
221
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spot-gps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Baker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-10 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock