Class: OMDBGateway::Gateway
- Inherits:
-
Object
- Object
- OMDBGateway::Gateway
- Defined in:
- lib/omdbgateway/gateway.rb
Overview
Faraday Based Gateway
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
Instance Method Summary collapse
-
#find_by_id(imdb_id, full_plot = false, tomatoes = true) ⇒ OMDBGateway::ResponseWrapper
Retrieves a single movie or show based on its IMDb ID.
-
#free_search(q) ⇒ OMDBGateway::ResponseWrapper
Retrieves a movie or show based on its title.
- #get(url, &block) ⇒ Object
-
#initialize(base_uri) ⇒ Gateway
constructor
A new instance of Gateway.
-
#title_search(title, year = nil, full_plot = false, tomatoes = false) ⇒ OMDBGateway::ResponseWrapper
Retrieves a single movie or show based on its title.
Constructor Details
#initialize(base_uri) ⇒ Gateway
Returns a new instance of Gateway.
9 10 11 12 13 14 15 16 17 |
# File 'lib/omdbgateway/gateway.rb', line 9 def initialize base_uri @base_uri = base_uri @conn = Faraday.new(:url => @base_uri) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :omdb_gateway faraday.response :json faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
7 8 9 |
# File 'lib/omdbgateway/gateway.rb', line 7 def base_uri @base_uri end |
Instance Method Details
#find_by_id(imdb_id, full_plot = false, tomatoes = true) ⇒ OMDBGateway::ResponseWrapper
Retrieves a single movie or show based on its IMDb ID.
72 73 74 75 76 77 78 79 |
# File 'lib/omdbgateway/gateway.rb', line 72 def find_by_id(imdb_id, full_plot = false, tomatoes = true) response = get '/' do |req| req.params = {:i => imdb_id} req.params[:plot] = 'full' if (full_plot == true || 'full' == full_plot) req.params[:tomatoes] = 'true' if tomatoes end response end |
#free_search(q) ⇒ OMDBGateway::ResponseWrapper
Retrieves a movie or show based on its title.
57 58 59 60 61 62 |
# File 'lib/omdbgateway/gateway.rb', line 57 def free_search(q) response = get '/' do |req| req.params = {:s => q} end response.prune_hash('Search', []) end |
#get(url, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/omdbgateway/gateway.rb', line 19 def get url, &block begin response = @conn.get do |req| req.url url yield req if block_given? end rescue URI::BadURIError => e response = ResponseWrapper.new(nil, 500, "BadURIError: #{e}") end response end |
#title_search(title, year = nil, full_plot = false, tomatoes = false) ⇒ OMDBGateway::ResponseWrapper
Retrieves a single movie or show based on its title.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/omdbgateway/gateway.rb', line 40 def title_search(title, year = nil, full_plot = false, tomatoes = false) response = get '/' do |req| req.params = {:t => title} req.params[:plot] = 'full' if (full_plot == true || 'full' == full_plot) req.params[:tomatoes] = 'true' if tomatoes req.params[:y] = year unless year.nil? end # Middleware creates the Hash response end |