class Troo::API::Client

Public Class Methods

perform(parameters) click to toggle source

@param [Hash] @return [Array]

# File lib/troo/api/client.rb, line 15
def perform(parameters)
  new(parameters).perform
end

Public Instance Methods

perform() click to toggle source

@return [Array]

# File lib/troo/api/client.rb, line 21
def perform
  return [] unless allow_remote?
  return [] if missing_parameters?
  return [] if error_response?
  return [] if empty_response?

  log

  if collection?
    model.with_collection(parsed_response)
  else
    [model.new(parsed_response)]
  end
end

Private Instance Methods

allow_remote?() click to toggle source
# File lib/troo/api/client.rb, line 89
def allow_remote?
  Troo.configuration.allow_remote
end
collection?() click to toggle source
# File lib/troo/api/client.rb, line 38
def collection?
  parsed_response.is_a?(Array)
end
empty_response?() click to toggle source
# File lib/troo/api/client.rb, line 42
def empty_response?
  return log(true) if parsed_response.empty?
  false
end
error_response?() click to toggle source
# File lib/troo/api/client.rb, line 47
def error_response?
  return log(true) if response.is_a?(ErrorResponse)
  false
end
external_id() click to toggle source
# File lib/troo/api/client.rb, line 137
def external_id
  interpolation[:external_id]
end
filename() click to toggle source
# File lib/troo/api/client.rb, line 130
def filename
  filename = [Troo.root_path + '/logs/api/', endpoint.to_s]
  filename << '_' << external_id if external_id
  filename << '.json'
  filename.join
end
formatted_messages() click to toggle source
# File lib/troo/api/client.rb, line 102
def formatted_messages
  messages.map do |label, value|
    [
      [Esc.red, label.rjust(8, ' '), ':', Esc.reset].join, value
    ].join(' ')
  end.join("\n")
end
log(retval = nil) click to toggle source
# File lib/troo/api/client.rb, line 93
def log(retval = nil)
  Troo.logger.debug("\n" + formatted_messages) if log?
  retval
end
log?() click to toggle source
# File lib/troo/api/client.rb, line 98
def log?
  Troo.configuration.logs
end
messages() click to toggle source
# File lib/troo/api/client.rb, line 110
def messages
  request_log.merge!(response_log)
end
missing_parameters?() click to toggle source
# File lib/troo/api/client.rb, line 85
def missing_parameters?
  verb.nil? || endpoint.nil? || model.nil?
end
parsed_response() click to toggle source
# File lib/troo/api/client.rb, line 52
def parsed_response
  @parsed ||= Yajl::Parser.parse(response_body)
end
query() click to toggle source
# File lib/troo/api/client.rb, line 68
def query
  Addressable::URI.new
    .tap { |u| u.query_values = @query }.query
end
request_log() click to toggle source
# File lib/troo/api/client.rb, line 114
def request_log
  {
    'Endpoint' => endpoint,
    'Verb'     => verb,
    'URI'      => uri,
    'Query'    => query
  }
end
response() click to toggle source
# File lib/troo/api/client.rb, line 64
def response
  @response ||= API::Request.make(verb, uri, query)
end
response_body() click to toggle source
# File lib/troo/api/client.rb, line 56
def response_body
  data = response.body
  File.open(filename, 'w') do |file_handle|
    file_handle.write(data)
  end
  data
end
response_log() click to toggle source
# File lib/troo/api/client.rb, line 123
def response_log
  {
    'Status'   => response.code,
    'Response' => response.body
  }
end
uri() click to toggle source
# File lib/troo/api/client.rb, line 73
def uri
  Addressable::URI.parse(url + urn).to_s
end
url() click to toggle source
# File lib/troo/api/client.rb, line 77
def url
  Troo.configuration.api_url
end
urn() click to toggle source
# File lib/troo/api/client.rb, line 81
def urn
  Troo::API::Endpoints.interpolate(endpoint, interpolation)
end