Class: NetFlix::Request
Constant Summary
collapse
- RESERVED_CHARACTERS =
/[^A-Za-z0-9\-\._~]/
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Valuable
#attributes, attributes, create_accessor_for, create_setter_for, #deep_duplicate_of, defaults, has_collection, has_value, #initialize
Constructor Details
This class inherits a constructor from Valuable
Class Method Details
.default_cache_options ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/net_flix/request.rb', line 11
def self.default_cache_options
{
:cache => 600,
:valid => 86400,
:period => 60,
:timeout => 5
}
end
|
.encode(value) ⇒ Object
65
66
67
|
# File 'lib/net_flix/request.rb', line 65
def self.encode(value)
URI.escape( value.to_s, RESERVED_CHARACTERS ) if value
end
|
Instance Method Details
#authenticator ⇒ Object
31
32
33
|
# File 'lib/net_flix/request.rb', line 31
def authenticator
@auth ||= NetFlix::Authenticator.new(:request => self, :credentials => NetFlix.credentials)
end
|
#log ⇒ Object
39
40
41
|
# File 'lib/net_flix/request.rb', line 39
def log
NetFlix.log(target.to_s)
end
|
#ordered_keys ⇒ Object
22
23
24
|
# File 'lib/net_flix/request.rb', line 22
def ordered_keys
parameters.keys.sort
end
|
#parameter_string ⇒ Object
25
26
27
28
29
|
# File 'lib/net_flix/request.rb', line 25
def parameter_string
ordered_keys.map do |key, value|
"#{key}=#{ ( key == 'term' )? URI.escape( parameters[key] ) : parameters[key]}"
end.join('&')
end
|
#send ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/net_flix/request.rb', line 43
def send
merged_cache_options = self.class.default_cache_options.merge(cache_options)
APICache.get(target.to_s, merged_cache_options ) do
authenticator.sign!
log
Net::HTTP.get(target)
end
end
|
#target ⇒ Object
35
36
37
|
# File 'lib/net_flix/request.rb', line 35
def target
URI.parse "#{url}?#{parameter_string}"
end
|
#valid? ⇒ Boolean
72
73
74
75
76
|
# File 'lib/net_flix/request.rb', line 72
def valid?
errors.clear
validate_http_method
errors.empty?
end
|
#validate_http_method ⇒ Object
78
79
80
|
# File 'lib/net_flix/request.rb', line 78
def validate_http_method
errors << "HTTP method must be POST or GET, but I got #{http_method}" unless ['POST', 'GET'].include? http_method
end
|
#write_to_file(file_name) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/net_flix/request.rb', line 52
def write_to_file(file_name)
authenticator.sign!
Net::HTTP.start(target.host, target.port) do |http|
File.open( file_name, 'w') do |file|
http.request_get(target.request_uri) do |response|
response.read_body do |body|
file.write(body)
end
end
end
end
end
|