Class: Activite::Client

Inherits:
Object
  • Object
show all
Includes:
HTTP::OAuthClient
Defined in:
lib/activite/client.rb

Constant Summary

Constants included from HTTP::OAuthClient

HTTP::OAuthClient::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Attributes included from HTTP::OAuthClient

#consumer_key, #consumer_secret, #secret, #token

Instance Method Summary collapse

Methods included from HTTP::OAuthClient

#access_token, #authorize_url, #connected?, #existing_access_token, #request_token

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Activite::Client

Initializes a new Client object used to communicate with the Withings API.

An authenticated Client can be created with an access token and access token secret if the user has previously authorized access to their Withings account and you've stored their access credentials. An unauthenticated Client can be created that will allow you to initiate the OAuth authorization flow, directing the user to Withings to authorize access to their account.

Examples:

User has not yet authorized access to their Withings account

client = Activite::Client.new({ consumer_key: your_key, consumer_secret: your_secret })

User has authorized access to their Withings account

client = Activite::Client.new({
  consumer_key: your_key,
  consumer_secret: your_secret,
  token: your_access_token,
  secret: your_access_token_secret
})

You can also pass parameters as a block

client = Activite::Client.new do |config|
  config.consumer_key = your_key
  config.consumer_secret = your_secret
  config.token = token
  config.secret = secret
end

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :consumer_key (String)

    The consumer key (required)

  • :consumer_secret (String)

    The consumer secret (required)

  • :token (String)

    The access token (if you've stored it)

  • :secret (String)

    The access token secret (if you've stored it)

Yields:

  • (_self)

Yield Parameters:


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/activite/client.rb', line 48

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  yield(self) if block_given?

  unless @token.nil? || @secret.nil?
    @access_token = existing_access_token(@token, @secret)
  end
end

Instance Attribute Details

#user_agentString

Return the User-Agent string

Returns:

  • (String)

63
64
65
# File 'lib/activite/client.rb', line 63

def user_agent
  @user_agent ||= "WithingsRubyGem/#{Activite::VERSION}"
end

Instance Method Details

#activities(user_id, options = {}) ⇒ Array<Activite::Activity>

Get a list of activity measures for the specified user

Parameters:

  • user_id (Integer)
  • options (Hash) (defaults to: {})

Returns:


73
74
75
76
77
78
# File 'lib/activite/client.rb', line 73

def activities(user_id, options = {})
  perform_request(:get, '/v2/measure', Activite::Activity, 'activities', {
    action: 'getactivity',
    userid: user_id
  }.merge(options))
end

#body_measurements(user_id, options = {}) ⇒ Array<Activite::MeasurementGroup>

Get a list of body measurements taken by Withings devices

Parameters:

  • user_id (Integer)
  • options (Hash) (defaults to: {})

Returns:


86
87
88
89
90
91
# File 'lib/activite/client.rb', line 86

def body_measurements(user_id, options = {})
  perform_request(:get, '/measure', Activite::MeasurementGroup, 'measuregrps', {
    action: 'getmeas',
    userid: user_id
  }.merge(options))
end

#sleep_series(user_id, options = {}) ⇒ Array<Activite::Sleep>

Get details about a user's sleep

Parameters:

  • user_id (Integer)
  • options (Hash) (defaults to: {})

Returns:

  • (Array<Activite::Sleep>)

99
100
101
102
103
104
# File 'lib/activite/client.rb', line 99

def sleep_series(user_id, options = {})
  perform_request(:get, '/v2/sleep', Activite::SleepSeries, 'series', {
    action: 'get',
    userid: user_id
  }.merge(options))
end

#sleep_summary(user_id, options = {}) ⇒ Array<Activite::SleepSummary>

Get a summary of a user's night. Includes the total time they slept, how long it took them to fall asleep, how long it took them to fall asleep, etc.

NOTE: user_id isn't actually used in this API call (so I assume it is derived from the OAuth credentials) but I was uncomfortable introducing this inconsitency into this gem.

Parameters:

  • user_id (Intger)
  • options (Hash) (defaults to: {})

Returns:


118
119
120
121
122
# File 'lib/activite/client.rb', line 118

def sleep_summary(user_id, options = {})
  perform_request(:get, '/v2/sleep', Activite::SleepSummary, 'series', {
    action: 'getsummary'
  }.merge(options))
end