Module: FanartTv::Client

Extended by:
Client
Includes:
FanartTv::ClientModules::TransparentProxy
Included in:
Client
Defined in:
lib/fanart_tv/client.rb

Instance Method Summary collapse

Methods included from FanartTv::ClientModules::TransparentProxy

#get_contents

Instance Method Details

#build_url(url, params) ⇒ Object



40
41
42
# 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) ⇒ Object



32
33
34
35
36
37
38
# 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

#httpObject



3
4
5
6
7
8
9
10
11
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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