class DataGetter

Constants

API

Attributes

api_call[RW]
headers[R]
processor[RW]
url[RW]
values[RW]

Public Class Methods

new(url) click to toggle source
# File lib/contentar/data_getter.rb, line 7
def initialize(url)
  @headers = { x_access_key: ENV['PRICE_ACCESS_KEY'] }
end

Public Instance Methods

data() click to toggle source
# File lib/contentar/data_getter.rb, line 11
def data
  return error_process(response_data) if is_error?(response_data)
  processor.data(response_data)
end

Private Instance Methods

attempt_get() click to toggle source
# File lib/contentar/data_getter.rb, line 22
def attempt_get
  begin
    get
  rescue RestClient::RequestTimeout, RestClient::InternalServerError => error
    error_data(error.message)
  end
end
error_data(error) click to toggle source
# File lib/contentar/data_getter.rb, line 34
def error_data(error)
  { error: error }.to_json
end
error_process(data) click to toggle source
# File lib/contentar/data_getter.rb, line 43
def error_process(data)
  JSON.parse(data)
end
get() click to toggle source
# File lib/contentar/data_getter.rb, line 30
def get
  RestClient.post("#{ API }#{ api_call }", values, headers)
end
is_error?(response) click to toggle source
# File lib/contentar/data_getter.rb, line 38
def is_error?(response)
  parsed = JSON.parse(response)
  parsed.fetch('error') { false }
end
response_data() click to toggle source
# File lib/contentar/data_getter.rb, line 18
def response_data
  @response_data ||= attempt_get
end