Class: HumanApi::Human
- Inherits:
-
Nestful::Resource
- Object
- Nestful::Resource
- HumanApi::Human
- Defined in:
- lib/human_api/human.rb
Constant Summary collapse
- AVAILABLE_METHODS =
The available methods for this api
%i{ profile activities blood_glucose blood_oxygen blood_pressure body_fat genetic_traits heart_rate height locations sleeps weight bmi sources }.freeze
Instance Attribute Summary collapse
-
#next_page_link ⇒ Object
Returns the value of attribute next_page_link.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#results ⇒ Object
Returns the value of attribute results.
-
#success ⇒ Object
Returns the value of attribute success.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#total_size ⇒ Object
Returns the value of attribute total_size.
Instance Method Summary collapse
-
#initialize(options) ⇒ Human
constructor
A new instance of Human.
- #profile(options = {}) ⇒ Object
-
#query(method, options = {}) ⇒ Object
.
-
#summary ⇒ Object
Profile =====================================.
Constructor Details
#initialize(options) ⇒ Human
Returns a new instance of Human.
16 17 18 19 20 21 |
# File 'lib/human_api/human.rb', line 16 def initialize() @token = [:access_token] @success = true @results = [] super end |
Instance Attribute Details
#next_page_link ⇒ Object
Returns the value of attribute next_page_link.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def next_page_link @next_page_link end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def @options end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def params @params end |
#results ⇒ Object
Returns the value of attribute results.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def results @results end |
#success ⇒ Object
Returns the value of attribute success.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def success @success end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
5 6 7 |
# File 'lib/human_api/human.rb', line 5 def token @token end |
#total_size ⇒ Object
Returns the value of attribute total_size.
4 5 6 |
# File 'lib/human_api/human.rb', line 4 def total_size @total_size end |
Instance Method Details
#profile(options = {}) ⇒ Object
36 37 38 |
# File 'lib/human_api/human.rb', line 36 def profile( = {}) query :profile, end |
#query(method, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/human_api/human.rb', line 42 def query(method, = {}) .symbolize_keys! @options = unless AVAILABLE_METHODS.include? method.to_sym raise ArgumentError, "The method '#{method}' does not exist!" end method = method.to_s url = "#{method}" if method.is_singular? url += "/readings" if [:readings] == true else url += "/summary" if [:summary] == true url +- "/summaries" if [:summaries] == true end if [:date].present? url += "/daily/#{[:date]}" elsif [:id].present? url += "/#{[:id]}" end @params = {access_token: token} @params.merge!(start_date: [:start_date]) if [:start_date].present? @params.merge!(end_date: [:end_date]) if [:end_date].present? if [:fetch_all] fetch_page url while results.count < total_size fetch_page end [:handle_data] ? @success : results else @params.merge!(limit: [:limit]) if [:limit].present? @params.merge!(offset: [:offset]) if [:offset].present? result = fetch_page url [:return_metadata] ? result : JSON.parse(result.body) end rescue Nestful::UnauthorizedAccess => e if HumanApi.config.handle_access_error HumanApi.config.handle_access_error.call e else raise if HumanApi.config.raise_access_errors false end end |
#summary ⇒ Object
Profile =====================================
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/human_api/human.rb', line 25 def summary get '', access_token: token rescue Nestful::UnauthorizedAccess => e if HumanApi.config.handle_access_error HumanApi.config.handle_access_error.call e else raise if HumanApi.config.raise_access_errors false end end |