Class: TaskHelper::API::Call

Inherits:
Object
  • Object
show all
Defined in:
lib/task_helper/api/call.rb

Constant Summary collapse

MissingAPIKey =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route:, params: {}, timeout: 314, time: Time.now) ⇒ Call

Returns a new instance of Call.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/task_helper/api/call.rb', line 9

def initialize(route:, params: {}, timeout: 314, time: Time.now)
  @params = { rest_api_key: API.rest_api_key }.merge(params)
  if @params[:rest_api_key].nil?
    raise MissingAPIKey, "Rest API key not provided. " \
      "Either pass it as a param or use " \
      "'TaskHelper::API.rest_api_key = key' " \
      "to set it for all future calls."
  end
  @route = "#{BASE_URL}/#{route}"
  @timeout = timeout
  @time = time
  set_end_time
end

Instance Attribute Details

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/task_helper/api/call.rb', line 6

def time
  @time
end

Instance Method Details

#==(other_call) ⇒ Object



37
38
39
# File 'lib/task_helper/api/call.rb', line 37

def ==(other_call)
  @route == other_call.route && @params == other_call.params
end

#expired?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/task_helper/api/call.rb', line 33

def expired?
  @end_time < Time.now
end

#runObject



23
24
25
26
27
28
29
30
31
# File 'lib/task_helper/api/call.rb', line 23

def run
  @time = Time.now
  if expired? || !@result
    set_end_time
    @result = HTTParty.get(@route, query: @params).parsed_response
  else
    @result
  end
end