class RescueTimeApi::Client

Attributes

base_path[RW]
host[RW]
key[RW]

Public Class Methods

new(options) click to toggle source
# File lib/rescue_time_api/client.rb, line 10
def initialize(options)
  @key = options.fetch(:key)
  @host = options.fetch(:host,"www.rescuetime.com")
  @base_path = options.fetch(:base_path,"/anapi/data")
end

Public Instance Methods

connection() click to toggle source
# File lib/rescue_time_api/client.rb, line 16
def connection
  @connection ||= get_connection
end
current_user_name() click to toggle source
# File lib/rescue_time_api/client.rb, line 37
def current_user_name
  request({perspective: 'member'}).rows.first['person']
end
default_params() click to toggle source
# File lib/rescue_time_api/client.rb, line 41
def default_params
  {key: key, format: 'json'}
end
format_params(params) click to toggle source
# File lib/rescue_time_api/client.rb, line 45
def format_params(params)
  time_keys.each do |key|
    if params[key] && params[key].respond_to?(:to_date)
      params[key] = params[key].to_date.to_s
    end
  end
  params
end
get_connection() click to toggle source
# File lib/rescue_time_api/client.rb, line 20
def get_connection
  Faraday.new(:url => "https://#{host}") do |conn|
    conn.request    :json
    conn.response   :json, :content_type => /\bjson$/
    conn.adapter    Faraday.default_adapter
  end
end
request(params) click to toggle source
# File lib/rescue_time_api/client.rb, line 28
def request(params)
  params = format_params(params)
  Response.new(run_request(params))
end
run_request(params) click to toggle source
# File lib/rescue_time_api/client.rb, line 33
def run_request(params)
  connection.get(base_path,params.merge(default_params))
end
time_keys() click to toggle source
# File lib/rescue_time_api/client.rb, line 54
def time_keys
  keys = ['re','restrict_end','rb','restrict_begin']
  return keys + keys.map(&:to_sym)
end