class Withings::User

Attributes

birthdate[R]
fat_method[R]
first_name[R]
gender[R]
last_name[R]
oauth_token[R]
oauth_token_secret[R]
short_name[R]
user_id[R]

Public Class Methods

authenticate(user_id, oauth_token, oauth_token_secret) click to toggle source
# File lib/withings/user.rb, line 4
def self.authenticate(user_id, oauth_token, oauth_token_secret)
  response = Withings::Connection.get_request('/user', oauth_token, oauth_token_secret, :action => :getbyuserid, :userid => user_id)
  user_data = response['users'].detect { |item| item['id'] == user_id.to_i }
  raise Withings::ApiError.new(2555, 'No user found', '') unless user_data
  Withings::User.new(user_data.merge({:oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret}))
end
new(params) click to toggle source

If you create a user yourself, then the only attributes of interest (required for calls to the API) are ‘user_id’ and ‘oauth_token’ and ‘oauth_token_secret’

# File lib/withings/user.rb, line 12
def initialize(params)
  params = params.stringify_keys
  @short_name = params['shortname']
  @first_name = params['firstname']
  @last_name = params['lastname']
  @user_id = params['id']                         || params['user_id']
  @birthdate = Time.at(params['birthdate']) if params['birthdate']
  @gender = params['gender'] == 0 ? :male : params['gender'] == 1 ? :female : nil
  @fat_method = params['fatmethod']
  @oauth_token = params['oauth_token']
  @oauth_token_secret = params['oauth_token_secret']
end

Public Instance Methods

describe_notification(callback_url, device = Withings::SCALE) click to toggle source
# File lib/withings/user.rb, line 33
def describe_notification(callback_url, device = Withings::SCALE)
  response = connection.get_request('/notify', :action => :get, :callbackurl => callback_url, :appli => device)
  Withings::NotificationDescription.new(response.merge(:callbackurl => callback_url))
end
get_activities(params = {}) click to toggle source

List the activity metrics. Params:

  • :date

  • :startdateymd

  • :enddateymd

Either date OR startdateymd AND enddate need to be supplied.

# File lib/withings/user.rb, line 54
def get_activities(params = {})
  connection.get_request('/v2/measure', params.merge(:action => :getactivity))
end
get_sleeps(params = {}) click to toggle source

List the sleep metrics Params:

  • :startdate

  • :enddate

# File lib/withings/user.rb, line 62
def get_sleeps(params = {})
  connection.get_request('/v2/sleep', params.merge(:action => :get))
end
list_notifications(device = Withings::SCALE) click to toggle source

List the notifications for a device (defaults to Withings::SCALE), pass nil to list all devices.

# File lib/withings/user.rb, line 39
def list_notifications(device = Withings::SCALE)
  options = (device.nil? ? {} : {:appli => device})
  response = connection.get_request('/notify', options.merge({:action => :list}))
  response['profiles'].map do |item|
    Withings::NotificationDescription.new(item)
  end
end
measurement_groups(params = {}) click to toggle source

list measurement groups The limit and offset parameters are converted to will_paginate style parameters (:per_page, :page)

  • :per_page (default: 100)

  • :page (default: 1)

  • :category (default: empty)

  • :measurement_type (default: empty)

  • :start_at (default: empty)

  • :end_at (default: empty)

  • :last_udpated_at (default: empty)

  • :device (default: empty)

Parameters are described in WBS api

# File lib/withings/user.rb, line 78
def measurement_groups(params = {})
  params = params.stringify_keys
  options = {:limit => 100, :offset => 0}
  options[:limit] =  params['per_page'] if params.has_key?('per_page')
  options[:offset] = ((params['page'] || 1) - 1) * options[:limit]
  options[:category] = params['category'] if params.has_key?('category')
  options[:meastype] = params['measurement_type'] if params.has_key?('measurement_type')
  options[:startdate] = params['start_at'].to_i if params['start_at']
  options[:enddate] = params['end_at'].to_i if params['end_at']
  options[:lastupdate] = params['last_updated_at'].to_i if params['last_updated_at']
  options[:devtype] = params['device'] if params['device']
  response = connection.get_request('/measure', options.merge(:action => :getmeas))
  response['measuregrps'].map do |group|
    Withings::MeasurementGroup.new(group)
  end
end
revoke_notification(callback_url, device = Withings::SCALE) click to toggle source
# File lib/withings/user.rb, line 29
def revoke_notification(callback_url, device = Withings::SCALE)
  connection.get_request('/notify', :action => :revoke, :callbackurl => callback_url, :appli => device)
end
subscribe_notification(callback_url, description, device = Withings::SCALE) click to toggle source
# File lib/withings/user.rb, line 25
def subscribe_notification(callback_url, description, device = Withings::SCALE)
  connection.get_request('/notify', :action => :subscribe, :callbackurl => callback_url, :comment => description, :appli => device)
end
to_s() click to toggle source
# File lib/withings/user.rb, line 95
def to_s
  "[User #{short_name} / #{:user_id}]"
end

Protected Instance Methods

connection() click to toggle source
# File lib/withings/user.rb, line 107
def connection
  @connection ||= Withings::Connection.new(self)
end
devices_bitmask(*devices) click to toggle source
# File lib/withings/user.rb, line 102
def devices_bitmask(*devices)
  devices = [Withings::SCALE, Withings::BLOOD_PRESSURE_MONITOR] if Array(devices).empty?
  devices.inject('|'.to_sym)
end