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 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. He can do this either in the sharing overlay on my.withings.com or through the api (user.share = true) after authentication by email/password
28
- * As soon as the user sets sharing to false (user.share = false) the public_key is reset and upon next activation of sharing (user.share = true) a new public_key is assigned.
29
- * All the methods making remote calls can throw Withing::ApiError if something goes wrong (wrong public_key, missing parameters, ...). You can find more details on the error by looking at the status code in the error.
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
 
@@ -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
- response = self.class.get(path, :query => params.merge(:publickey => user.public_key, :userid => user.user_id))
18
- self.class.verify_response!(response)
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
- def self.verify_response!(response)
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
@@ -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 is missing, invalid, or does not match the provided email",
4
- 247 => "The userid is either absent or incorrect",
5
- 250 => "The userid and publickey provided do not match, or the user does not share its data",
6
- 264 => "The email address provided is either unknown or invalid",
7
- 293 => "The callback URL is either absent or incorrect",
8
- 294 => "No such subscription could be deleted",
9
- 304 => "The comment is either absent or incorrect",
10
- 2555 => "An unknown error occurred",
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(STATUS_CODES[status] || "Undefined status code. We do not have any description about the code #{status}. If you know more, then please let me know.")
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{simplificator-withings}
5
- s.version = "0.2.2"
5
+ s.version = "0.2.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["pascalbetz"]
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - pascalbetz