Class: RescueTimeApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rescue_time_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @key = options.fetch(:key)
  @host = options.fetch(:host,"www.rescuetime.com")
  @base_path = options.fetch(:base_path,"/anapi/data")
end

Instance Attribute Details

#base_pathObject

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

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/rescue_time_api/client.rb', line 8

def host
  @host
end

#keyObject

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

#connectionObject



16
17
18
# File 'lib/rescue_time_api/client.rb', line 16

def connection
  @connection ||= get_connection
end

#current_user_nameObject



37
38
39
# File 'lib/rescue_time_api/client.rb', line 37

def current_user_name
  request({perspective: 'member'}).rows.first['person']
end

#default_paramsObject



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_connectionObject



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_keysObject



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