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 Attribute Summary collapse
-
#upload_options ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#add_option(name, value) ⇒ Object
Add an option to the list that should be sent with a request.
-
#add_upload_option(name, value) ⇒ Object
Add an option that should be sent with an upload request.
-
#authentication_token ⇒ Object
Retrieve the authentication token from either the list of parameters or the global value (e.g. Fleakr.auth_token).
-
#boundary ⇒ Object
:nodoc:.
-
#default_options ⇒ Object
The default options to send as part of the parameter list, defaults to sending the API key.
-
#initialize(options = {}, send_authentication_token = true) ⇒ ParameterList
constructor
Create a new parameter list with optional parameters:.
-
#list ⇒ Object
:nodoc:.
-
#options ⇒ Object
:nodoc:.
-
#options_with_signature ⇒ Object
:nodoc:.
-
#options_without_signature ⇒ Object
:nodoc:.
-
#send_authentication_token? ⇒ Boolean
Should we send an authentication token as part of this list of parameters? By default this is true if the token is available as a global value or if the :auth_token key/value is part of the initial list.
-
#sign? ⇒ Boolean
Should this parameter list be signed? This will be true if Fleakr.shared_secret is set, false if not.
-
#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 = {}, send_authentication_token = true) ⇒ ParameterList
Create a new parameter list with optional parameters:
list = Fleakr::Api::ParameterList.new(:username => 'reagent')
You can also disable the sending of the authentication token using the second parameter:
list = Fleakr::Api::ParameterList.new({}, false)
23 24 25 26 27 |
# File 'lib/fleakr/api/parameter_list.rb', line 23 def initialize( = {}, send_authentication_token = true) @send_authentication_token = send_authentication_token = = {} end |
Instance Attribute Details
#upload_options ⇒ Object (readonly)
:nodoc:
12 13 14 |
# File 'lib/fleakr/api/parameter_list.rb', line 12 def end |
Instance Method Details
#add_option(name, value) ⇒ Object
Add an option to the list that should be sent with a request.
78 79 80 |
# File 'lib/fleakr/api/parameter_list.rb', line 78 def add_option(name, value) .merge!(name => value) end |
#add_upload_option(name, value) ⇒ Object
Add an option that should be sent with an upload request.
84 85 86 |
# File 'lib/fleakr/api/parameter_list.rb', line 84 def add_upload_option(name, value) .merge!(name => value) end |
#authentication_token ⇒ Object
Retrieve the authentication token from either the list of parameters or the global value (e.g. Fleakr.auth_token)
72 73 74 |
# File 'lib/fleakr/api/parameter_list.rb', line 72 def authentication_token Fleakr.auth_token.nil? ? [:auth_token] : Fleakr.auth_token end |
#boundary ⇒ Object
:nodoc:
95 96 97 |
# File 'lib/fleakr/api/parameter_list.rb', line 95 def boundary # :nodoc: @boundary ||= Digest::MD5.hexdigest(rand.to_s) end |
#default_options ⇒ Object
The default options to send as part of the parameter list, defaults to sending the API key
41 42 43 |
# File 'lib/fleakr/api/parameter_list.rb', line 41 def {:api_key => Fleakr.api_key} end |
#list ⇒ Object
:nodoc:
114 115 116 117 118 119 120 |
# File 'lib/fleakr/api/parameter_list.rb', line 114 def list # :nodoc: = sign? ? : value_parameters = .map {|k,v| ValueParameter.new(k, v) } file_parameters = .map {|k,v| FileParameter.new(k, v) } value_parameters + file_parameters end |
#options ⇒ Object
:nodoc:
88 89 90 91 92 93 |
# File 'lib/fleakr/api/parameter_list.rb', line 88 def # :nodoc: = .merge() .merge!(:auth_token => authentication_token) if send_authentication_token? end |
#options_with_signature ⇒ Object
:nodoc:
110 111 112 |
# File 'lib/fleakr/api/parameter_list.rb', line 110 def # :nodoc: .merge(:api_sig => signature) end |
#options_without_signature ⇒ Object
:nodoc:
106 107 108 |
# File 'lib/fleakr/api/parameter_list.rb', line 106 def # :nodoc: .reject {|k,v| k.to_s == 'api_sig'} end |
#send_authentication_token? ⇒ Boolean
Should we send an authentication token as part of this list of parameters? By default this is true if the token is available as a global value or if the :auth_token key/value is part of the initial list. You can override this in the constructor.
34 35 36 |
# File 'lib/fleakr/api/parameter_list.rb', line 34 def send_authentication_token? @send_authentication_token && !authentication_token.nil? end |
#sign? ⇒ Boolean
Should this parameter list be signed? This will be true if Fleakr.shared_secret is set, false if not.
48 49 50 |
# File 'lib/fleakr/api/parameter_list.rb', line 48 def sign? !Fleakr::Support::Utility.blank?(Fleakr.shared_secret) end |
#signature ⇒ Object
:nodoc:
99 100 101 102 103 104 |
# File 'lib/fleakr/api/parameter_list.rb', line 99 def signature # :nodoc: = .sort {|a,b| a[0].to_s <=> b[0].to_s } signature_text = .map {|o| "#{o[0]}#{o[1]}" }.join Digest::MD5.hexdigest("#{Fleakr.shared_secret}#{signature_text}") end |
#to_form ⇒ Object
Generate the form representation of this parameter list including the boundary
62 63 64 65 66 67 |
# File 'lib/fleakr/api/parameter_list.rb', line 62 def to_form form = list.map {|p| "--#{boundary}\r\n#{p.to_form}" }.join form << "--#{boundary}--" form end |
#to_query ⇒ Object
Generate the query string representation of this parameter list - e.g. foo=bar&blee=baz
55 56 57 |
# File 'lib/fleakr/api/parameter_list.rb', line 55 def to_query list.map {|element| element.to_query }.join('&') end |