Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 97 lines (80 sloc) 2.439 kB
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
1 require 'faraday_middleware'
2 require 'nibbler/json'
3 require 'addressable/uri'
4 require 'addressable/template'
5
6 module NibbleEndpoints
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
7 def get(endpoint, params = {}, &block)
8 process_request(:get, endpoint, params, &block)
9 end
10
11 def get_raw(endpoint, params = {}, &block)
12 make_request(:get, endpoint, params, &block)
13 end
b785bbd switch to TMDB API v3
Ivana & Mislav authored
14
15 def path_for(endpoint, params = {})
16 template, = endpoints.fetch(endpoint.to_sym) {
17 raise ArgumentError, "unknown endpoint #{endpoint.inspect}"
18 }
19 template.expand(default_params.merge(params))
20 end
21
22 def url_for(endpoint, params = {})
23 path = path_for(endpoint, params)
24 # TODO: figure out why + vs %20
25 connection.build_url(path).to_s.gsub('+', '%20')
26 end
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
27
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
28 protected
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
29
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
30 attr_reader :connection
31
32 def define_connection(url_prefix, &block)
33 @connection = Faraday::Connection.new(Addressable::URI.parse(url_prefix), &block)
34 end
35
36 def endpoint(name, url, parser = nil)
37 template = Addressable::Template.new url.to_s
38 parser ||= block_given? && Class.new(Nibbler, &Proc.new)
39 endpoints[name.to_sym] = [template, parser]
40 end
41
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
42 def default_params(params = nil)
43 if params or block_given?
44 @default_params = params || Proc.new
45 else
46 @default_params = {} unless defined? @default_params
47 @default_params = @default_params.call if @default_params.is_a? Proc
48 @default_params
49 end
50 end
51
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
52 def make_request(method, endpoint, params = {})
b785bbd switch to TMDB API v3
Ivana & Mislav authored
53 url = path_for(endpoint, params)
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
54 connection.run_request(method, url, nil, nil) do |request|
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
55 yield request if block_given?
56 end
7a7fd8e @mislav admin: provide link to TMDB entry, show raw movie data on demand
authored
57 end
58
59 def process_request(method, endpoint, params = {}, &block)
60 _, parser = endpoints.fetch(endpoint.to_sym) {
61 raise ArgumentError, "unknown endpoint #{endpoint.inspect}"
62 }
63 response = make_request(method, endpoint, params, &block)
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
64
65 if parser
b00d0eb @mislav add Rotten Tomatoes integration
authored
66 process_response(response, parser)
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
67 else
b00d0eb @mislav add Rotten Tomatoes integration
authored
68 response
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
69 end
70 end
71
b00d0eb @mislav add Rotten Tomatoes integration
authored
72 private
73
74 def endpoints
75 @endpoints ||= {}
76 end
77
78 def process_response(response, parser)
79 case response.status
80 when 200
81 data = response.body
82 if content_type(response) =~ /\b(json|javascript)$/
83 data = Nibbler::JsonDocument.new(data)
84 end
85 parser.parse(data)
86 when 404
87 nil
88 else
89 raise "got status #{response.status.inspect}"
90 end
91 end
92
93 def content_type(response)
94 response.headers['content-type'].to_s.split(';').first
95 end
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
96 end
Something went wrong with that request. Please try again.