Class: TVDB::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/tvdb_client/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tvdb_client/connection.rb', line 9

def initialize( options = {} )
  @token           = ""
  
  @host_url        = options.fetch( :host_url ) { Settings.tvdb.host_url }
  @language        = options[:language]
  @version         = options[:version]
  @modified_since  = options[:modified_since]

  @connection      = Faraday.new( :url => host_url, :ssl => { :verify => false } )
  @response_struct = Struct.new( :request_url, :code, :body, :headers )
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/tvdb_client/connection.rb', line 7

def connection
  @connection
end

#host_urlObject (readonly)

Returns the value of attribute host_url.



7
8
9
# File 'lib/tvdb_client/connection.rb', line 7

def host_url
  @host_url
end

#languageObject

Returns the value of attribute language.



6
7
8
# File 'lib/tvdb_client/connection.rb', line 6

def language
  @language
end

#modified_sinceObject

Returns the value of attribute modified_since.



6
7
8
# File 'lib/tvdb_client/connection.rb', line 6

def modified_since
  @modified_since
end

#response_structObject

Returns the value of attribute response_struct.



6
7
8
# File 'lib/tvdb_client/connection.rb', line 6

def response_struct
  @response_struct
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/tvdb_client/connection.rb', line 6

def token
  @token
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/tvdb_client/connection.rb', line 6

def version
  @version
end

Instance Method Details

#get(route, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tvdb_client/connection.rb', line 33

def get( route, options = {} )
  params, headers = parse_options( options )

  response = connection.get do |req|
    req.url( route )
    req.headers = headers
    req.params  = params
  end

  return parsed_response( response )
end

#parse_options(options) ⇒ Object



45
46
47
48
49
50
# File 'lib/tvdb_client/connection.rb', line 45

def parse_options( options )
  headers = set_default_headers( options )
  params  = set_params( options )

  return params, headers
end

#parsed_response(response) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tvdb_client/connection.rb', line 83

def parsed_response( response )
  unless response.body.empty?
    body = JSON.parse( response.body )
  else
    body = nil
  end

  response_struct.new(
    "#{host_url}#{response.env.url.request_uri}",
    response.status,
    body,
    response.env.response_headers
  )
end

#post(route, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tvdb_client/connection.rb', line 21

def post( route, options )
  request_body = options.fetch( :body )

  response = connection.post do |req|
    req.url( route )
    req.headers = set_default_headers( options )
    req.body    = request_body.to_json
  end

  return parsed_response( response )
end

#set_convenience_headers(options) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/tvdb_client/connection.rb', line 75

def set_convenience_headers( options )
  if options
    @language       = options[:language]
    @version        = options[:version]
    @modified_since = options[:modified_since]
  end
end

#set_default_headers(options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tvdb_client/connection.rb', line 60

def set_default_headers( options )
  set_convenience_headers( options )

  headers = options[:headers] if options
  headers ||= {}

  headers["Authorization"]     ||= "Bearer #{token}"
  headers["Content-Type"]      ||= "application/json"
  headers["Accept-Language"]   ||= "#{language}"
  headers["Accept"]            ||= "application/vnd.thetvdb.v#{version}"
  headers["If-Modified-Since"] ||= "#{modified_since}"

  return headers
end

#set_params(options) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/tvdb_client/connection.rb', line 52

def set_params( options )
  [:language, :version, :modified_since, :headers].each do |param|
    options.delete_if { |key, value| key == param }
  end

  return options
end