Class: Fleakr::Api::ParameterList

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {}, send_authentication_token = true)
  @send_authentication_token = send_authentication_token
  @options                   = options
  @upload_options            = {}
end

Instance Attribute Details

#upload_optionsObject (readonly)

:nodoc:


12
13
14
# File 'lib/fleakr/api/parameter_list.rb', line 12

def upload_options
  @upload_options
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)
  @options.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)
  @upload_options.merge!(name => value)
end

#authentication_tokenObject

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? ? @options[:auth_token] : Fleakr.auth_token
end

#boundaryObject

: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_optionsObject

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 default_options
  {:api_key => Fleakr.api_key}
end

#listObject

:nodoc:


114
115
116
117
118
119
120
# File 'lib/fleakr/api/parameter_list.rb', line 114

def list # :nodoc:
  options_for_list = sign? ? options_with_signature : options_without_signature 
  value_parameters = options_for_list.map {|k,v| ValueParameter.new(k, v) }
  file_parameters  = upload_options.map {|k,v| FileParameter.new(k, v) }

  value_parameters + file_parameters
end

#optionsObject

:nodoc:


88
89
90
91
92
93
# File 'lib/fleakr/api/parameter_list.rb', line 88

def options # :nodoc:
  options = default_options.merge(@options)
  options.merge!(:auth_token => authentication_token) if send_authentication_token?
  
  options
end

#options_with_signatureObject

:nodoc:


110
111
112
# File 'lib/fleakr/api/parameter_list.rb', line 110

def options_with_signature # :nodoc:
  options_without_signature.merge(:api_sig => signature)
end

#options_without_signatureObject

:nodoc:


106
107
108
# File 'lib/fleakr/api/parameter_list.rb', line 106

def options_without_signature # :nodoc:
  options.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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

48
49
50
# File 'lib/fleakr/api/parameter_list.rb', line 48

def sign?
  !Fleakr.shared_secret.blank?
end

#signatureObject

:nodoc:


99
100
101
102
103
104
# File 'lib/fleakr/api/parameter_list.rb', line 99

def signature # :nodoc:
  sorted_options = options_without_signature.sort {|a,b| a[0].to_s <=> b[0].to_s }
  signature_text = sorted_options.map {|o| "#{o[0]}#{o[1]}" }.join

  Digest::MD5.hexdigest("#{Fleakr.shared_secret}#{signature_text}")
end

#to_formObject

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_queryObject

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