Class: TimeDoctor::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/timedoctor/worker.rb

Constant Summary collapse

ENTRY =
'https://webapi.timedoctor.com/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Worker

Returns a new instance of Worker.



7
8
9
10
# File 'lib/timedoctor/worker.rb', line 7

def initialize(config)
  @config = config
  @conn   = Faraday.new(url: ENTRY)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/timedoctor/worker.rb', line 5

def config
  @config
end

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/timedoctor/worker.rb', line 5

def conn
  @conn
end

Instance Method Details

#exchange(method, url, params = {}) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/timedoctor/worker.rb', line 12

def exchange(method, url, params = {})
  raise EmptyAccessToken unless config[:access_token]

  params[:access_token] = config[:access_token]
  params[:_format]      = :json

  response = conn.public_send method, url, params

  case response.status
  when 200
    JSON.parse(response.body, symbolize_names: true)
  when 401
    exchange(method, url, params) if Token.new(config).refresh
  else
    raise UnknownError, response
  end
end