class OmdbGateway::Request

Constants

BASE_URI

Attributes

api_version[R]
plot[R]
return_type[R]
year[R]

Public Class Methods

new(params) click to toggle source
# File lib/omdb_gateway/request.rb, line 8
def initialize(params)
  @year = params[:year]
  @plot = params[:plot] || :short
  @format = params[:format]
  @api_version = params[:api_version] || 1
end

Public Instance Methods

fetch(response_klass) click to toggle source
# File lib/omdb_gateway/request.rb, line 15
def fetch(response_klass)
  @response ||= RestClient.get(url)
  if @format.present? && @format.to_sym == :json
    JSON.parse(@response.body).with_indifferent_access
  elsif @format.present? && @format.to_sym == :xml
    @response.body
  else
    response_klass.send(:new, JSON.parse(@response.body))
  end
end

Protected Instance Methods

url() click to toggle source
# File lib/omdb_gateway/request.rb, line 28
def url
  url_string = "#{BASE_URI}/?apikey=#{api_key}"
  url_string += "&y=#{@year}" if @year.present?
  url_string += "&plot=#{@plot}" if @plot.present?
  url_string += "&r=#{@format}" if @format.present?
  url_string += "&v=#{@api_version}"
  url_string
end

Private Instance Methods

api_key() click to toggle source
# File lib/omdb_gateway/request.rb, line 39
def api_key
  raise InvalidConfiguration.new('omdb.yml file not present') unless File.exists?('config/initializers/omdb.yml')
  omdb_config = YAML::load(File.open('config/initializers/omdb.yml'))
  raise Unauthorized.new('Unauthorized - Provide a valid API key') unless omdb_config['apikey'].present?

  omdb_config['apikey']
end