Class: NetFlix::Request

Inherits:
Valuable show all
Defined in:
lib/net_flix/request.rb

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

.encode(value) ⇒ Object



39
40
41
# File 'lib/net_flix/request.rb', line 39

def Request.encode(value)
  URI.escape( value, RESERVED_CHARACTERS ) if value
end

Instance Method Details

#authenticatorObject



21
22
23
# File 'lib/net_flix/request.rb', line 21

def authenticator
  @auth = NetFlix::Authenticator.new(request: self, credentials: NetFlix.credentials)
end

#logObject



29
30
31
# File 'lib/net_flix/request.rb', line 29

def log
  NetFlix.log(target.to_s)
end

#ordered_keysObject



10
11
12
# File 'lib/net_flix/request.rb', line 10

def ordered_keys
  parameters.keys.sort
end

#parameter_stringObject



14
15
16
17
18
19
# File 'lib/net_flix/request.rb', line 14

def parameter_string
  string = ordered_keys.map do |key|
    value = (key == "term") ? URI.escape(parameters[key]) : parameters[key]
    "#{key}=#{value}"
  end.join('&')
end

#sendObject



33
34
35
36
37
# File 'lib/net_flix/request.rb', line 33

def send
  authenticator.sign!
  log
  Net::HTTP.get(target)
end

#targetObject



25
26
27
# File 'lib/net_flix/request.rb', line 25

def target
  URI.parse "#{url}?#{parameter_string}"
end

#valid?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/net_flix/request.rb', line 46

def valid?
  errors.clear
  validate_http_method
  errors.empty?
end

#validate_http_methodObject



52
53
54
# File 'lib/net_flix/request.rb', line 52

def validate_http_method
  errors << "HTTP method must be POST or GET, but I got #{http_method}" unless ['POST', 'GET'].include? http_method
end