Class: HumanApi::Human

Inherits:
Nestful::Resource
  • Object
show all
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

Instance Method Summary collapse

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(options)
  @token   = options[:access_token]
  @success = true
  @results = []
  super
end

Instance Attribute Details

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

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/human_api/human.rb', line 4

def options
  @options
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/human_api/human.rb', line 4

def params
  @params
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/human_api/human.rb', line 4

def results
  @results
end

#successObject

Returns the value of attribute success.



4
5
6
# File 'lib/human_api/human.rb', line 4

def success
  @success
end

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'lib/human_api/human.rb', line 5

def token
  @token
end

#total_sizeObject

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(options = {})
  query :profile, options
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, options = {})
  options.symbolize_keys!
  @options = 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 options[:readings] == true
  else
    url += "/summary"   if options[:summary]   == true
    url +- "/summaries" if options[:summaries] == true
  end

  if options[:date].present?
    url += "/daily/#{options[:date]}"
  elsif options[:id].present?
    url += "/#{options[:id]}"
  end

  @params = {access_token: token}
  @params.merge!(start_date: options[:start_date]) if options[:start_date].present?
  @params.merge!(end_date:   options[:end_date])   if options[:end_date].present?

  if options[:fetch_all]
    fetch_page url
    while results.count < total_size
      fetch_page
    end

    options[:handle_data] ? @success : results
  else
    @params.merge!(limit: options[:limit])   if options[:limit].present?
    @params.merge!(offset: options[:offset]) if options[:offset].present?
    result = fetch_page url
    options[: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

#summaryObject

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