class Uatu::Connection

Constants

BASE_URL

Attributes

options[R]
resource[R]

Public Class Methods

new(resource, options) click to toggle source
# File lib/uatu/connection.rb, line 8
def initialize resource, options
  @resource = resource
  @options  = options
end

Public Instance Methods

request() click to toggle source
# File lib/uatu/connection.rb, line 13
def request
  connection.get path, params
end

Private Instance Methods

connection() click to toggle source
# File lib/uatu/connection.rb, line 19
def connection
  @connection ||= Faraday.new(url: BASE_URL) do |faraday|
    faraday.use Uatu::Response::RaiseMarvelError
    faraday.request  :url_encoded
    faraday.adapter  Faraday.default_adapter
  end
end
current_timestamp() click to toggle source
# File lib/uatu/connection.rb, line 73
def current_timestamp
  @ts ||= DateTime.now.to_s
end
hash(timestamp) click to toggle source
# File lib/uatu/connection.rb, line 77
def hash timestamp
  Digest::MD5.hexdigest("#{timestamp}#{Uatu.private_key}#{Uatu.public_key}")
end
mandatory_params() click to toggle source
# File lib/uatu/connection.rb, line 65
def mandatory_params
  {
    :apikey => Uatu.public_key,
    :ts     => current_timestamp,
    :hash   => hash(current_timestamp)
  }
end
params() click to toggle source
# File lib/uatu/connection.rb, line 41
def params
  request_params = {}

  # We remove unnecessary keys that should go on the route
  temp_params = options.reject{|key, value| key.to_s.match(/.*_id/)}

  # We change the names, so 'format_type' becomes 'formatType'
  temp_params.each{|key, value| request_params[key.to_s.camelize(:lower).to_sym] = value }

  # An array should become a string with comma separated values
  request_params.each{|key, value| request_params[key] = value.join(',') if value.is_a?(Array) }

  request_params.merge(mandatory_params)
end
path() click to toggle source
# File lib/uatu/connection.rb, line 27
def path
  route = "/v1/public/#{resource_path}"
  if resource_id = options["#{resource_path.singularize}_id".to_sym]
    route += "/#{resource_id}"
  end

  # If it is combined, it comes after the '_'
  if resource.split('_').count>1 && combined_path = resource.split('_').last
    route += "/#{combined_path}"
  end

  route
end
resource_path() click to toggle source

character => characters characters => characters character_comics => characters

# File lib/uatu/connection.rb, line 59
def resource_path
  @resource_path ||= resource.split('_').first.pluralize.tap do |path|
    raise Uatu::Error.new('InvalidMethod') unless Uatu::RESOURCES.map(&:pluralize).include?(path)
  end
end