health_hero-human_api 0.2.1 → 0.3.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: 1b3794f4fdc16f8ff0488af021bec476e514a607
4
- data.tar.gz: 85df138268da3635d8740e36145e0f832d92dae2
3
+ metadata.gz: 3d50a9ef5b31d0b1c21333d5d798e5e3417c1bf8
4
+ data.tar.gz: fe81ba970d44246934e1b6eb0c4eebe80b72ec46
5
5
  SHA512:
6
- metadata.gz: 9ac5c037fbcc722d438c990e27ae82bf6caa242ccf4c68651b01c37fa5615bb519d19baec4db9eebdd0b55db66e85bd23d2aced6b1e9a0c94661ee6f0dad95ca
7
- data.tar.gz: 252c37a5edec1642f79b8a51d23e55f9f14236ccdee188bbba2858b737f1b92590a1fadf47632cd91f754a4e9c14b1b23199a77346d0cd78965540e8234f4862
6
+ metadata.gz: e1648e75d59ee2627096946e8425d9bb9f47d01bcc86e72bd4863d736f020feb982ae02082a5ec133500e3005100d54ebb7378ee25372b1e4854a491dcc68ff3
7
+ data.tar.gz: 9eb63fb6bd3ede1c83adf38177bbe8c841573d1bf3b9c95b7914021f3f08bfed7057b31dab200392ff6b08d59d32abecc9c86c836a62245c2459f6c3828099dd
File without changes
data/lib/human_api/app.rb CHANGED
@@ -36,5 +36,19 @@ module HumanApi
36
36
  false
37
37
  end
38
38
  end
39
+
40
+ # Delete a new human:
41
+ def self.delete_human(id)
42
+ response = delete "users/#{id}"
43
+
44
+ response.status >= 200 && response.status < 300
45
+ rescue Nestful::UnauthorizedAccess => e
46
+ if HumanApi.config.handle_access_error
47
+ HumanApi.config.handle_access_error.call e
48
+ else
49
+ raise if HumanApi.config.raise_access_errors
50
+ false
51
+ end
52
+ end
39
53
  end
40
54
  end
@@ -1,6 +1,7 @@
1
1
  module HumanApi
2
2
  class Human < Nestful::Resource
3
3
 
4
+ attr_accessor :params, :success, :results, :options, :total_size, :next_page_link
4
5
  attr_reader :token
5
6
 
6
7
  endpoint 'https://api.humanapi.co'
@@ -13,7 +14,9 @@ module HumanApi
13
14
  }.freeze
14
15
 
15
16
  def initialize(options)
16
- @token = options[:access_token]
17
+ @token = options[:access_token]
18
+ @success = true
19
+ @results = []
17
20
  super
18
21
  end
19
22
 
@@ -37,71 +40,76 @@ module HumanApi
37
40
  # =============================================
38
41
 
39
42
  def query(method, options = {})
43
+ options.symbolize_keys!
44
+ @options = options
45
+
40
46
  unless AVAILABLE_METHODS.include? method.to_sym
41
47
  raise ArgumentError, "The method '#{method}' does not exist!"
42
48
  end
43
49
 
44
- # From sym to string
45
50
  method = method.to_s
51
+ url = "#{method}"
46
52
 
47
- # The base of the url
48
- url = "#{method}"
49
-
50
- # If it is a singular word prepare for readings
51
53
  if method.is_singular?
52
- if options[:readings] == true
53
- url += "/readings"
54
- end
54
+ url += "/readings" if options[:readings] == true
55
55
  else
56
- if options[:summary] == true
57
- url += "/summary"
58
- end
56
+ url += "/summary" if options[:summary] == true
57
+ url +- "/summaries" if options[:summaries] == true
59
58
  end
60
59
 
61
- # You passed a date
62
60
  if options[:date].present?
63
- # Make a request for a specific date
64
61
  url += "/daily/#{options[:date]}"
65
- # If you passed an id
66
62
  elsif options[:id].present?
67
- # Make a request for a single
68
63
  url += "/#{options[:id]}"
69
64
  end
70
65
 
71
- params = {access_token: token}
66
+ @params = {access_token: token}
67
+ @params.merge!(start_date: options[:start_date]) if options[:start_date].present?
68
+ @params.merge!(end_date: options[:end_date]) if options[:end_date].present?
72
69
 
73
70
  if options[:fetch_all]
74
- results = []
75
- first_request = get url, params
76
- total_size = first_request.headers['x-total-count'].to_i
77
- results = results + JSON.parse(first_request.body)
78
- next_page_link = first_request.headers['link'].match(/<(https[^>]*)>/)[1]
79
-
71
+ fetch_page url
80
72
  while results.count < total_size
81
- next_page = Nestful.get next_page_link
82
- next_page_link = next_page.headers['link'].match(/<(https[^>]*)>/)[1]
83
- results = results + JSON.parse(next_page.body)
73
+ fetch_page
84
74
  end
85
75
 
86
- results
76
+ options[:handle_data] ? @success : results
87
77
  else
88
- params.merge!(limit: options[:limit]) if options[:limit].present?
89
- params.merge!(offset: options[:offset]) if options[:offset].present?
90
-
91
- result = get url, params
92
- if options[:return_metadata]
93
- result
94
- else
95
- JSON.parse result.body
96
- end
78
+ @params.merge!(limit: options[:limit]) if options[:limit].present?
79
+ @params.merge!(offset: options[:offset]) if options[:offset].present?
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
97
89
  end
98
90
  end
99
- rescue Nestful::UnauthorizedAccess => e
100
- if HumanApi.config.handle_access_error
101
- HumanApi.config.handle_access_error.call e
102
- else
103
- raise if HumanApi.config.raise_access_errors
104
- false
91
+
92
+ private
93
+
94
+ def fetch_page(url=nil)
95
+ if url
96
+ page = get url, params
97
+ @total_size = page.headers['x-total-count'].to_i
98
+ else
99
+ page = Nestful.get next_page_link
100
+ end
101
+
102
+ @next_page_link = page.headers['link'].match(/<(https[^>]*)>/)[1] if page.headers['link']
103
+
104
+ if options[:handle_data]
105
+ JSON.parse(page.body).each do |data|
106
+ @success = false unless options[:handle_data].call data
107
+ @results << '*'
108
+ end
109
+ else
110
+ @results = @results + JSON.parse(page.body) if options[:fetch_all]
111
+ page
112
+ end
105
113
  end
106
114
  end
107
115
  end
@@ -1,3 +1,3 @@
1
1
  module HumanApi
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: delete
5
+ uri: https://APP_ID:@api.humanapi.co/v1/apps/CLIENT_ID/users/test_user
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 204
19
+ message: No Content
20
+ headers:
21
+ Date:
22
+ - Wed, 02 Sep 2015 17:11:30 GMT
23
+ Server:
24
+ - nginx/1.4.6 (Ubuntu)
25
+ X-Powered-By:
26
+ - Express
27
+ Connection:
28
+ - keep-alive
29
+ body:
30
+ encoding: UTF-8
31
+ string: ''
32
+ http_version:
33
+ recorded_at: Wed, 02 Sep 2015 17:07:27 GMT
34
+ recorded_with: VCR 2.9.3
@@ -63,4 +63,12 @@ describe HumanApi::App do
63
63
  xit '.. need to think of a way to make this fail :p'
64
64
  end
65
65
  end
66
+
67
+ describe ".delete_human" do
68
+ it "returns true when all is well" do
69
+ VCR.use_cassette :delete_human_success do
70
+ expect(app.delete_human 'test_user').to eq true
71
+ end
72
+ end
73
+ end
66
74
  end
@@ -102,6 +102,23 @@ describe HumanApi::Human do
102
102
  it 'goes crazy and gets them all' do
103
103
  expect(response.count).to eq 234
104
104
  end
105
+
106
+ context "with a callback" do
107
+ let(:context) { '_fetch_all' }
108
+ let(:foo) { double 'Nothing' }
109
+ let(:options) { {fetch_all: true, handle_data: ->data { foo.do_stuff data['id'] }} }
110
+
111
+ it 'calls the callback on them all' do
112
+ expect(foo).to receive(:do_stuff).exactly(234).times.and_return true
113
+ expect(response).to be true
114
+ end
115
+
116
+ it 'returns false if one errors' do
117
+ expect(foo).to receive(:do_stuff).exactly(:once).and_return false
118
+ expect(foo).to receive(:do_stuff).exactly(233).times.and_return true
119
+ expect(response).to be false
120
+ end
121
+ end
105
122
  end
106
123
  end
107
124
 
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'human_api'
1
+ require 'health_hero-human_api'
2
2
  require 'webmock'
3
3
  require 'vcr'
4
4
  require 'pry'
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.2.1
4
+ version: 0.3.0
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-01 00:00:00.000000000 Z
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nestful
@@ -165,13 +165,14 @@ files:
165
165
  - Rakefile
166
166
  - human_api.gemspec
167
167
  - lib/config/initializers/core_ext.rb
168
- - lib/human_api.rb
168
+ - lib/health_hero-human_api.rb
169
169
  - lib/human_api/app.rb
170
170
  - lib/human_api/config.rb
171
171
  - lib/human_api/human.rb
172
172
  - lib/human_api/user.rb
173
173
  - lib/human_api/version.rb
174
174
  - spec/cassettes/create_human_success.yml
175
+ - spec/cassettes/delete_human_success.yml
175
176
  - spec/cassettes/get_activities.yml
176
177
  - spec/cassettes/get_activities_a_few.yml
177
178
  - spec/cassettes/get_activities_fetch_all.yml
@@ -210,6 +211,7 @@ specification_version: 4
210
211
  summary: API client for HumanAPI
211
212
  test_files:
212
213
  - spec/cassettes/create_human_success.yml
214
+ - spec/cassettes/delete_human_success.yml
213
215
  - spec/cassettes/get_activities.yml
214
216
  - spec/cassettes/get_activities_a_few.yml
215
217
  - spec/cassettes/get_activities_fetch_all.yml