module MarvelCrawler::ApiRequest

Constants

MARVEL_API_ROUTES

Public Instance Methods

auth(ts_value = timestamp) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 54
def auth(ts_value = timestamp)
  {
    ts: ts_value,
    apikey: api_key,
    hash: generate_hash(ts_value)
  }
end
check_for_credentials() click to toggle source
# File lib/marvel_crawler/api_request.rb, line 114
def check_for_credentials
  [:api_key, :private_key].each do |key|
    raise InvalidClientError, "You need to provide :#{key} param." if self.send(key).nil?
  end
end
generate_hash(ts_value) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 62
def generate_hash(ts_value)
  Digest::MD5.hexdigest(ts_value + private_key + api_key)
end
get_entity_valid_params(entity, entity_id, entity_property) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 79
def get_entity_valid_params(entity, entity_id, entity_property)
  valid_params = Hash.new

  case entity
    when 'characters'
      if entity_id.nil?
        valid_params = %w(name nameStartsWith modifiedSince comics series events stories orderBy limit offset).map { |e| e.to_sym}
      elsif !entity_id.nil? && entity_property.nil?
        valid_params = %w(characterId).map { |e| e.to_sym}
      else
        # valid_params for other methods
      end
    when 'comics'
      # comics params
    when 'creators'
      # creators params
    when 'events'
      # events params
    when 'series'
      # series params
    when 'stories'
      # stories params
  end

  valid_params
end
make_request(path, options = {}) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 46
def make_request(path, options = {})
  check_for_credentials
  response = Connection.new.connect.get do |request|
    request.url(path, options.merge(auth))
  end
  ApiResponse.new(response).format_response
end
pagination_params(page_num) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 106
def pagination_params(page_num)
  return {} if page_num.nil?
  {
    limit: record_per_page,
    offset: ((page_num - 1) * record_per_page)
  }
end
sanitize_params(args, entity, entity_id, entity_property) click to toggle source
# File lib/marvel_crawler/api_request.rb, line 70
def sanitize_params(args, entity, entity_id, entity_property)
  options = Hash.new.merge(pagination_params(args[:page_num]))

  valid_params = get_entity_valid_params(entity, entity_id, entity_property)

  options.merge(args.keep_if { |key, value| valid_params.include?(key) }) unless args.empty?
  options
end
timestamp() click to toggle source
# File lib/marvel_crawler/api_request.rb, line 66
def timestamp
  Time.now.to_s
end