simplificator-withings 0.2.2 → 0.2.3
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.
- data/README.rdoc +8 -4
- data/lib/withings/connection.rb +10 -8
- data/lib/withings/error.rb +20 -10
- data/simplificator-withings.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -18,15 +18,19 @@ will make an API call to populate the attributes, User.new just requires user_id
|
|
18
18
|
|
19
19
|
== TODO
|
20
20
|
|
21
|
-
* Need to test parameter generation for get measurement
|
22
21
|
* Integration Test? But i don't want to share my credentials... Perhaps with a test account on withings?
|
23
22
|
|
24
23
|
== Remarks
|
25
24
|
|
26
25
|
* The gem is still under development and the API might change a bit.
|
27
|
-
* Authentication by user_id/public_key is only supported when the user has activated sharing.
|
28
|
-
|
29
|
-
|
26
|
+
* Authentication by user_id/public_key is only supported when the user has activated sharing.
|
27
|
+
He can do this either in the sharing overlay on my.withings.com or
|
28
|
+
through the api (user.share = true) after authentication by email/password
|
29
|
+
* As soon as the user sets sharing to false (user.share = false) the public_key is reset
|
30
|
+
and upon next activation of sharing (user.share = true) a new public_key is assigned.
|
31
|
+
* All the methods making remote calls can throw Withing::ApiError if something goes wrong
|
32
|
+
(wrong public_key, missing parameters, ...).
|
33
|
+
You can find more details on the error by looking at the status code in the error.
|
30
34
|
|
31
35
|
== How To
|
32
36
|
|
data/lib/withings/connection.rb
CHANGED
@@ -1,31 +1,33 @@
|
|
1
|
+
# A convenience class for making get requests to WBS API.
|
2
|
+
# It verifies the response and raises ApiError if a call failed.
|
1
3
|
class Withings::Connection
|
2
4
|
include HTTParty
|
3
5
|
base_uri 'wbsapi.withings.net'
|
4
6
|
format :json
|
5
7
|
|
6
|
-
attr_reader :user
|
7
8
|
def initialize(user)
|
8
9
|
@user = user
|
9
10
|
end
|
10
11
|
|
11
12
|
def self.get_request(path, params)
|
12
13
|
response = self.get(path, :query => params)
|
13
|
-
verify_response!(response)
|
14
|
+
verify_response!(response, path, params)
|
14
15
|
end
|
15
16
|
|
17
|
+
# Merges the params with public_key and user_id for authentication.
|
16
18
|
def get_request(path, params)
|
17
|
-
|
18
|
-
self.class.
|
19
|
+
params = params.merge(:publickey => @user.public_key, :userid => @user.user_id)
|
20
|
+
response = self.class.get(path, :query => params)
|
21
|
+
self.class.verify_response!(response, path, params)
|
19
22
|
end
|
20
23
|
|
21
24
|
protected
|
22
|
-
|
25
|
+
# Verifies the status code in the JSON response and returns either the body element or raises ApiError
|
26
|
+
def self.verify_response!(response, path, params)
|
23
27
|
if response['status'] == 0
|
24
28
|
response['body'] || response['status']
|
25
29
|
else
|
26
|
-
raise ApiError.new(response['status'])
|
30
|
+
raise ApiError.new(response['status'], path, params)
|
27
31
|
end
|
28
|
-
|
29
32
|
end
|
30
|
-
|
31
33
|
end
|
data/lib/withings/error.rb
CHANGED
@@ -1,22 +1,32 @@
|
|
1
1
|
class Withings::ApiError < StandardError
|
2
|
+
UNKNOWN_STATUS_CODE = lambda() {|status, path, params| "Unknown status code '#{status}'"}
|
2
3
|
STATUS_CODES = {
|
3
|
-
100 => "The hash
|
4
|
-
247 => "The userid is
|
5
|
-
250 => "The userid and publickey
|
6
|
-
264 => "The email address
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
100 => lambda() {|status, path, params| "The hash '#{params[:hash]}' does not match the email '#{params[:email]}'"},
|
5
|
+
247 => lambda() {|status, path, params| "The '#{params[:userid]}' is invalid"},
|
6
|
+
250 => lambda() {|status, path, params| "The userid '#{params[:userid]}' and publickey '#{params[:publickey]}' do not match, or the user does not share its data"},
|
7
|
+
264 => lambda() {|status, path, params| "The email address '#{params[:email]}' is either unknown or invalid"},
|
8
|
+
286 => lambda() {|status, path, params| "No subscription for '#{params[:callbackurl]}' was found" },
|
9
|
+
293 => lambda() {|status, path, params| "The callback URL '#{params[:callbackurl]}' is either unknown or invalid"},
|
10
|
+
294 => lambda() {|status, path, params| "Could not delete subscription for '#{params[:callbackurl]}'"},
|
11
|
+
304 => lambda() {|status, path, params| "The comment '#{params[:comment]}' is invalid"},
|
12
|
+
2554 => lambda() {|status, path, params| "Unknown action '#{params[:action]}' for '#{path}'"},
|
13
|
+
2555 => lambda() {|status, path, params| "An unknown error occurred"},
|
11
14
|
}
|
12
15
|
|
13
16
|
attr_reader :status
|
14
|
-
def initialize(status)
|
15
|
-
super(
|
17
|
+
def initialize(status, path, params)
|
18
|
+
super(build_message(status, path, params))
|
16
19
|
@status = status
|
17
20
|
end
|
18
21
|
|
19
22
|
def to_s
|
20
23
|
super + " - Status code: #{self.status}"
|
21
24
|
end
|
25
|
+
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def build_message(status, path, params)
|
30
|
+
(STATUS_CODES[status] || UNKNOWN_STATUS_CODE).call(status, path, params)
|
31
|
+
end
|
22
32
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplificator-withings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- pascalbetz
|