Class: Fleakr::Api::ParameterList
- Inherits:
-
Object
- Object
- Fleakr::Api::ParameterList
- Defined in:
- lib/fleakr/api/parameter_list.rb
Overview
ParameterList
Represents a list of parameters that get passed as part of a MethodRequest or UploadRequest. These can be transformed as necessary into query strings (using #to_query) or form data (using #to_form)
Instance Method Summary collapse
-
#<<(parameter) ⇒ Object
Add a new parameter (ValueParameter / FileParameter) to the list.
-
#[](key) ⇒ Object
Access an individual parameter by key (symbol or string).
-
#authenticate? ⇒ Boolean
Should we send the auth_token with the request?.
-
#boundary ⇒ Object
:nodoc:.
-
#initialize(options = {}) ⇒ ParameterList
constructor
Create a new parameter list with optional parameters: [:authenticate?] Request will automatically be authenticated if Fleakr.token is available set this to false to force it not to authenticate.
-
#sign? ⇒ Boolean
Should this parameter list be signed?.
-
#signature ⇒ Object
:nodoc:.
-
#to_form ⇒ Object
Generate the form representation of this parameter list including the boundary.
-
#to_query ⇒ Object
Generate the query string representation of this parameter list - e.g.
Constructor Details
#initialize(options = {}) ⇒ ParameterList
Create a new parameter list with optional parameters:
- :authenticate?
-
Request will automatically be authenticated if Fleakr.token is available set this to false to force it not to authenticate
Any additional name / value pairs will be created as individual ValueParameters as part of the list. Example:
>> list = Fleakr::Api::ParameterList.new(:foo => 'bar')
=> #<Fleakr::Api::ParameterList:0x1656e6c @list=... >
>> list[:foo]
=> #<Fleakr::Api::ValueParameter:0x1656da4 @include_in_signature=true, @name="foo", @value="bar">
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fleakr/api/parameter_list.rb', line 24 def initialize( = {}) # TODO: need to find a way to move the unexpected behavior in Fleakr.token elsewhere @api_options = .extract!(:authenticate?) @list = Hash.new .each {|k,v| self << ValueParameter.new(k.to_s, v) } self << ValueParameter.new('api_key', Fleakr.api_key) self << ValueParameter.new('auth_token', Fleakr.token.value) if authenticate? end |
Instance Method Details
#<<(parameter) ⇒ Object
Add a new parameter (ValueParameter / FileParameter) to the list
38 39 40 |
# File 'lib/fleakr/api/parameter_list.rb', line 38 def <<(parameter) @list.merge!(parameter.name => parameter) end |
#[](key) ⇒ Object
Access an individual parameter by key (symbol or string)
56 57 58 |
# File 'lib/fleakr/api/parameter_list.rb', line 56 def [](key) list[key.to_s] end |
#authenticate? ⇒ Boolean
Should we send the auth_token with the request?
50 51 52 |
# File 'lib/fleakr/api/parameter_list.rb', line 50 def authenticate? @api_options.has_key?(:authenticate?) ? @api_options[:authenticate?] : !Fleakr.token.blank? end |
#boundary ⇒ Object
:nodoc:
60 61 62 |
# File 'lib/fleakr/api/parameter_list.rb', line 60 def boundary # :nodoc: @boundary ||= Digest::MD5.hexdigest(rand.to_s) end |
#sign? ⇒ Boolean
Should this parameter list be signed?
44 45 46 |
# File 'lib/fleakr/api/parameter_list.rb', line 44 def sign? !Fleakr.shared_secret.blank? end |
#signature ⇒ Object
:nodoc:
81 82 83 84 85 86 |
# File 'lib/fleakr/api/parameter_list.rb', line 81 def signature # :nodoc: parameters_to_sign = @list.values.reject {|p| !p.include_in_signature? } signature_text = parameters_to_sign.sort.map {|p| "#{p.name}#{p.value}" }.join Digest::MD5.hexdigest("#{Fleakr.shared_secret}#{signature_text}") end |
#to_form ⇒ Object
Generate the form representation of this parameter list including the boundary
74 75 76 77 78 79 |
# File 'lib/fleakr/api/parameter_list.rb', line 74 def to_form form = list.values.map {|p| "--#{self.boundary}\r\n#{p.to_form}" }.join form << "--#{self.boundary}--" form end |
#to_query ⇒ Object
Generate the query string representation of this parameter list - e.g. foo=bar&blee=baz
67 68 69 |
# File 'lib/fleakr/api/parameter_list.rb', line 67 def to_query list.values.map {|element| element.to_query }.join('&') end |