lib/activite/client.rb in activite-0.1.0 vs lib/activite/client.rb in activite-0.2.0

- old
+ new

@@ -1,18 +1,20 @@ require 'activite/error' require 'activite/http/request' require 'activite/activity' require 'activite/measurement_group' +require 'activite/notification' require 'activite/sleep_series' require 'activite/sleep_summary' +require 'activite/response' module Activite class Client include Activite::HTTP::OAuthClient attr_writer :user_agent - + # 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 @@ -107,11 +109,11 @@ # 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. + # this inconsistency into this gem. # # @param user_id [Intger] # @param options [Hash] # # @return [Array<Activite::SleepSummary>] @@ -119,10 +121,59 @@ perform_request(:get, '/v2/sleep', Activite::SleepSummary, 'series', { action: 'getsummary' }.merge(options)) end + # Register a webhook / notification with the Withings API. This allows + # you to be notified when new data is available for a user. + # + # @param user_id [Integer] + # @param options [Hash] + # + # @return [Activite::Response] + def create_notification(user_id, options = {}) + perform_request(:post, '/notify', Activite::Response, nil, { + action: 'subscribe' + }.merge(options)) + end + + # Get information about a specific webhook / notification. + # + # @param user_id [Integer] + # @param options [Hash] + # + # @return [Activite::Notification] + def get_notification(user_id, options = {}) + perform_request(:get, '/notify', Activite::Notification, nil, { + action: 'get' + }.merge(options)) + end + + # Return a list of registered webhooks / notifications. + # + # @param user_id [Integer] + # @param options [Hash] + # + # @return [Array<Activite::Notification>] + def list_notifications(user_id, options = {}) + perform_request(:get, '/notify', Activite::Notification, 'profiles', { + action: 'list' + }.merge(options)) + end + + # Revoke previously subscribed webhook / notification. + # + # @param user_id [Integer] + # @param options [Hash] + # + # @return [Activite::Response] + def revoke_notification(user_id, options = {}) + perform_request(:get, '/notify', Activite::Response, nil, { + action: 'revoke' + }.merge(options)) + end + private # Helper function that handles all API requests # # @param http_method [Symbol] @@ -137,10 +188,12 @@ raise Activite::Error::ClientConfigurationError, "Missing consumer_key or consumer_secret" end options = Activite::Utils.normalize_date_params(options) request = Activite::HTTP::Request.new(@access_token, { 'User-Agent' => user_agent }) response = request.send(http_method, path, options) - if response.has_key? key + if key.nil? + klass.new(response) + elsif response.has_key? key response[key].collect do |element| klass.new(element) end else [klass.new(response)]