module FanartTv::Client

Public Instance Methods

build_url(url, params) click to toggle source
# File lib/fanart_tv/client.rb, line 40
def build_url(url, params)
  url % params.merge({api_key: FanartTv.config.api_key})
end
contents_of(url) click to toggle source
# File lib/fanart_tv/client.rb, line 32
def contents_of(url)
  if method_defined? :get_contents
    get_contents url
  else
    http.get url
  end
end
http() click to toggle source
# File lib/fanart_tv/client.rb, line 3
def http
  @faraday ||= Faraday.new do |f|
    f.request :json
    #f.use :instrumentation
    f.adapter Faraday.default_adapter # make requests with Net::HTTP
    f.response :json
    #f.use     FanartTv::Middleware # run requests with correct headers
  end
end
load(resource, query, params) click to toggle source
# File lib/fanart_tv/client.rb, line 13
def load(resource, query, params)
  raise Exception.new("You need to run FanartTv.configure before querying") if FanartTv.config.nil?

  response = contents_of(build_url(params[:url], query))
  data = params[:binding].parse(response.body)

  if params[:create_model]
    params[:create_model].new(data)
  elsif params[:create_models]
    models = data.map{ |item| params[:create_models].new(item) }
    models.sort!{ |a, b| a.send(params[:sort]) <=> b.send(params[:sort]) } if params[:sort]
    models
  else
    data
  end
rescue Faraday::Error::ParsingError
  nil
end