Class: RescueTimeApi::Client
- Inherits:
-
Object
- Object
- RescueTimeApi::Client
- Defined in:
- lib/rescue_time_api/client.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#host ⇒ Object
Returns the value of attribute host.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #connection ⇒ Object
- #current_user_name ⇒ Object
- #default_params ⇒ Object
- #format_params(params) ⇒ Object
- #get_connection ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #request(params) ⇒ Object
- #run_request(params) ⇒ Object
- #time_keys ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 |
# File 'lib/rescue_time_api/client.rb', line 10 def initialize() @key = .fetch(:key) @host = .fetch(:host,"www.rescuetime.com") @base_path = .fetch(:base_path,"/anapi/data") end |
Instance Attribute Details
#base_path ⇒ Object
Returns the value of attribute base_path.
8 9 10 |
# File 'lib/rescue_time_api/client.rb', line 8 def base_path @base_path end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/rescue_time_api/client.rb', line 8 def host @host end |
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/rescue_time_api/client.rb', line 8 def key @key end |
Instance Method Details
#connection ⇒ Object
16 17 18 |
# File 'lib/rescue_time_api/client.rb', line 16 def connection @connection ||= get_connection end |
#current_user_name ⇒ Object
37 38 39 |
# File 'lib/rescue_time_api/client.rb', line 37 def current_user_name request({perspective: 'member'}).rows.first['person'] end |
#default_params ⇒ Object
41 42 43 |
# File 'lib/rescue_time_api/client.rb', line 41 def default_params {key: key, format: 'json'} end |
#format_params(params) ⇒ Object
45 46 47 48 49 50 51 52 |
# 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 ⇒ Object
20 21 22 23 24 25 26 |
# 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) ⇒ Object
28 29 30 31 |
# 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) ⇒ Object
33 34 35 |
# File 'lib/rescue_time_api/client.rb', line 33 def run_request(params) connection.get(base_path,params.merge(default_params)) end |
#time_keys ⇒ Object
54 55 56 57 |
# 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 |