Class: TVDB::Connection
- Inherits:
-
Object
- Object
- TVDB::Connection
- Defined in:
- lib/tvdb_client/connection.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#host_url ⇒ Object
readonly
Returns the value of attribute host_url.
-
#language ⇒ Object
Returns the value of attribute language.
-
#modified_since ⇒ Object
Returns the value of attribute modified_since.
-
#response_struct ⇒ Object
Returns the value of attribute response_struct.
-
#token ⇒ Object
Returns the value of attribute token.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #get(route, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #parse_options(options) ⇒ Object
- #parsed_response(response) ⇒ Object
- #post(route, options) ⇒ Object
- #set_convenience_headers(options) ⇒ Object
- #set_default_headers(options) ⇒ Object
- #set_params(options) ⇒ Object
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( = {} ) @token = "" @host_url = .fetch( :host_url ) { Settings.tvdb.host_url } @language = [:language] @version = [:version] @modified_since = [:modified_since] @connection = Faraday.new( :url => host_url, :ssl => { :verify => false } ) @response_struct = Struct.new( :request_url, :code, :body, :headers ) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/tvdb_client/connection.rb', line 7 def connection @connection end |
#host_url ⇒ Object (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 |
#language ⇒ Object
Returns the value of attribute language.
6 7 8 |
# File 'lib/tvdb_client/connection.rb', line 6 def language @language end |
#modified_since ⇒ Object
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_struct ⇒ Object
Returns the value of attribute response_struct.
6 7 8 |
# File 'lib/tvdb_client/connection.rb', line 6 def response_struct @response_struct end |
#token ⇒ Object
Returns the value of attribute token.
6 7 8 |
# File 'lib/tvdb_client/connection.rb', line 6 def token @token end |
#version ⇒ Object
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, = {} ) params, headers = ( ) 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 ( ) headers = set_default_headers( ) params = set_params( ) 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, ) request_body = .fetch( :body ) response = connection.post do |req| req.url( route ) req.headers = set_default_headers( ) 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( ) if @language = [:language] @version = [:version] @modified_since = [: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( ) set_convenience_headers( ) headers = [:headers] if 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( ) [:language, :version, :modified_since, :headers].each do |param| .delete_if { |key, value| key == param } end return end |