health_hero-human_api 0.3.1 → 0.3.2

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: c217c0cffdebec2223cde421f673e1f2d1d09f5a
4
- data.tar.gz: fe4ff9bc5a2acd4eec99aa834308492d6d95ada3
3
+ metadata.gz: 0ed77a15aa9a4f4d52a1ad6b147bb4d5e08c84cc
4
+ data.tar.gz: ea9e5d872a2af3485f6485d0684f303bb5a5ac65
5
5
  SHA512:
6
- metadata.gz: 3179c496a64c1ff76be72a1c3eebdd36f5ee33ae80af1cf28d8b5f7822e49fe031aebd075143c2be141c9eea68610ddfe53d4ceb973f88f83d77bec6db926b49
7
- data.tar.gz: 25020943baadec2724ff7be0ecad68db82fbee7ab2e451560ed9d171b060639205a8cf268a1c76a7baf1e1e088324dd2d6f4513f56ecab386c11c5a80f2b2311
6
+ metadata.gz: 7ffeb1730dedef35efb285c1ddd034a2ad485df7a46f591b6db049c5b3c0ab448f7a683f7a9c15721aef8f7551affbfaa508970f69cb670f83be77872975cd95
7
+ data.tar.gz: ac9a87898529735646086ded69d9393dc5552769d382aaeb286796ac3bc08d40403030db06c4dcc5d83cf578533ee98789f0f0b426ba534c1c9620af8c4e640a
data/README.md CHANGED
@@ -21,7 +21,25 @@ gem 'health_hero-human_api'
21
21
 
22
22
  ## Configuration
23
23
 
24
- Let's say you have an User model as follows:
24
+ ### Initializer
25
+
26
+ ```ruby
27
+ HumanApi.config do |c|
28
+ c.app_id = ENV['HUMANAPI_KEY']
29
+ c.query_key = ENV['HUMANAPI_SECRET']
30
+ c.client_secret = ENV['HUMANAPI_CLIENT_SECRET']
31
+
32
+ # Optional:
33
+ # If a Nestful::UnauthorizedAccess error occurs, this proc will handle it:
34
+ c.handle_access_error = ->e,context { Airbrake.notify e; do_something_with(context) }
35
+
36
+ # If you don't want to handle it, and want it raised:
37
+ # (Note - if you set a proc above, this setting is ignored)
38
+ c.raise_access_errors = true # Default is false
39
+ end
40
+ ```
41
+
42
+ ### User model
25
43
 
26
44
  ```ruby
27
45
  class User < ActiveRecord::Base
@@ -35,34 +53,18 @@ class User < ActiveRecord::Base
35
53
  end
36
54
  ```
37
55
 
38
- This configuration is really simple. I suggest it over the second one.
39
-
40
- Always remember to configure the initializer with the access keys:
41
- ```ruby
42
- HumanApi.config do |c|
43
- c.app_id = ENV['HUMANAPI_KEY']
44
- c.query_key = ENV['HUMANAPI_SECRET']
45
- c.client_secret = ENV['HUMANAPI_CLIENT_SECRET']
46
- end
47
- ```
48
-
49
- ### The alternative
50
-
51
- If you don't like that configuration, you can use a different one, writing right into the initializer:
56
+ You can also configure it the initializer:
52
57
 
53
58
  ```ruby
54
59
  HumanApi.config do |c|
55
- c.app_id = "<YOUR_APP_ID>"
56
- c.query_key = "<YOUR_QUERY_KEY>"
57
- c.client_secret = "<YOUR__CLIENT_SECRET>"
58
-
60
+ ...
59
61
  # This is the part where the magics happen
60
62
  c.human_model = User # Tell me what is the model you want to use
61
63
  c.token_method_name = :human_token # Tell me the method you use to retrieve the token (Inside the human_model)
62
64
  end
63
65
  ```
64
66
 
65
- It should work in both ways, the choice is yours.
67
+ It should work both ways, the choice is yours.
66
68
 
67
69
  ## Usage
68
70
  Once you did the configuration, the usage of the gem is quite easy:
@@ -116,6 +118,9 @@ u.human.query(:activities, limit: 3, offset: 50).count #=> 3
116
118
  # Return all of a user's data jammed together, despite being across multiple pages:
117
119
  u.human.query(:activities).count #=> 50
118
120
  u.human.query(:activities, fetch_all: true).count #=> 321
121
+
122
+ # Return all of a user's data jammed together, and handle unauthorized errors:
123
+ u.human.query(:activities, fetch_all: true, handle_access_error: ->error, context { do_something_with(error, context)})
119
124
  ```
120
125
 
121
126
  Lastly, as a common rule, I've identified a pattern in humanapis.
@@ -124,7 +129,7 @@ Lastly, as a common rule, I've identified a pattern in humanapis.
124
129
 
125
130
  ## Common errors and troubleshooting
126
131
 
127
- ### 'rewrite_human_model': Could not find 'token' in User
128
- - Causes: it does mean that the method you suggested does not exist!
129
- - What to check: Check if you misspelled the method name or the attribute does not exist.
130
- - Solving: If this does not solve, try using the humanizable function passing a method you can create in your model to retrieve manually just the token.
132
+ - `rewrite_human_model`: Could not find `token` in `User`
133
+ - Causes: It means that the method you suggested as `:humanizable` does not exist!
134
+ - What to check: Check if you misspelled the method name or the attribute does not exist.
135
+ - Solving: If this does not solve, try using the `:humanizable` function passing a method you can create in your model to retrieve manually just the token.
data/lib/human_api/app.rb CHANGED
@@ -10,7 +10,7 @@ module HumanApi
10
10
  get 'users'
11
11
  rescue Nestful::UnauthorizedAccess => e
12
12
  if HumanApi.config.handle_access_error
13
- HumanApi.config.handle_access_error.call e
13
+ HumanApi.config.handle_access_error.call e, self
14
14
  else
15
15
  raise if HumanApi.config.raise_access_errors
16
16
  []
@@ -30,7 +30,7 @@ module HumanApi
30
30
  end
31
31
  rescue Nestful::UnauthorizedAccess => e
32
32
  if HumanApi.config.handle_access_error
33
- HumanApi.config.handle_access_error.call e
33
+ HumanApi.config.handle_access_error.call e, self
34
34
  else
35
35
  raise if HumanApi.config.raise_access_errors
36
36
  false
@@ -44,7 +44,7 @@ module HumanApi
44
44
  response.status >= 200 && response.status < 300
45
45
  rescue Nestful::UnauthorizedAccess => e
46
46
  if HumanApi.config.handle_access_error
47
- HumanApi.config.handle_access_error.call e
47
+ HumanApi.config.handle_access_error.call e, self
48
48
  else
49
49
  raise if HumanApi.config.raise_access_errors
50
50
  false
@@ -13,8 +13,8 @@ module HumanApi
13
13
  heart_rate height locations sleeps weight bmi sources
14
14
  }.freeze
15
15
 
16
- def initialize(options)
17
- @token = options[:access_token]
16
+ def initialize(initial_options)
17
+ @token = initial_options[:access_token]
18
18
  @success = true
19
19
  @results = []
20
20
  super
@@ -78,14 +78,13 @@ module HumanApi
78
78
  @params.merge!(limit: options[:limit]) if options[:limit].present?
79
79
  @params.merge!(offset: options[:offset]) if options[:offset].present?
80
80
  result = fetch_page url
81
- options[:return_metadata] ? result : JSON.parse(result.body)
82
- end
83
- rescue Nestful::UnauthorizedAccess => e
84
- if HumanApi.config.handle_access_error
85
- HumanApi.config.handle_access_error.call e
86
- else
87
- raise if HumanApi.config.raise_access_errors
88
- false
81
+ if options[:return_metadata]
82
+ result
83
+ elsif result.present? && result.respond_to?(:body)
84
+ JSON.parse result.body
85
+ else
86
+ Hash.new
87
+ end
89
88
  end
90
89
  end
91
90
 
@@ -110,6 +109,17 @@ module HumanApi
110
109
  @results = @results + JSON.parse(page.body) if options[:fetch_all]
111
110
  page
112
111
  end
112
+ rescue Nestful::UnauthorizedAccess => e
113
+ @success = false
114
+ if options[:handle_access_error]
115
+ options[:handle_access_error].call e, self
116
+ elsif HumanApi.config.handle_access_error
117
+ HumanApi.config.handle_access_error.call e, self
118
+ else
119
+ raise if HumanApi.config.raise_access_errors
120
+ end
121
+
122
+ false
113
123
  end
114
124
  end
115
125
  end
@@ -11,7 +11,7 @@ module HumanApi
11
11
  JSON.parse(response.body)['publicToken']
12
12
  rescue Nestful::UnauthorizedAccess => e
13
13
  if HumanApi.config.handle_access_error
14
- HumanApi.config.handle_access_error.call e
14
+ HumanApi.config.handle_access_error.call e, self
15
15
  else
16
16
  raise if HumanApi.config.raise_access_errors
17
17
  nil
@@ -1,3 +1,3 @@
1
1
  module HumanApi
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -35,7 +35,7 @@ describe HumanApi::App do
35
35
 
36
36
  context "when unauthorized" do
37
37
  context "with a proc set" do
38
- before { expect(HumanApi.config).to receive(:handle_access_error).exactly(:twice).and_return ->e { e.class.to_s } }
38
+ before { expect(HumanApi.config).to receive(:handle_access_error).exactly(:twice).and_return ->e,_ { e.class.to_s } }
39
39
 
40
40
  it 'calls the proc' do
41
41
  expect(app.create_human 'joe').to eq 'Nestful::UnauthorizedAccess'
@@ -38,6 +38,16 @@ describe HumanApi::Human do
38
38
  expect(profile['defaultTimeZone']).to eq 'name' => "UTC"
39
39
  end
40
40
  end
41
+
42
+ context "with an error handler" do
43
+ let(:profile) { human.profile handle_access_error: ->e,h { @error = e; @human = h} }
44
+
45
+ it "calls it" do
46
+ expect(profile).to eq Hash.new
47
+ expect(@error).to be_an_instance_of Nestful::UnauthorizedAccess
48
+ expect(@human.attributes).to eq 'access_token' => 'token'
49
+ end
50
+ end
41
51
  end
42
52
 
43
53
  describe "#activities" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: health_hero-human_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Aiken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-02 00:00:00.000000000 Z
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nestful