Class: SurveyMonkey::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/survey_monkey/parameters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method) {|_self| ... } ⇒ Parameters

Returns a new instance of Parameters.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
# File 'lib/survey_monkey/parameters.rb', line 12

def initialize(request_method)
  load_yaml_settings
  check_for_valid_api_method request_method
  @api_method = "#{request_method}"
  @uri = api_settings[api_method]['uri']
  define_parameter_methods(api_method)
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



66
67
68
# File 'lib/survey_monkey/parameters.rb', line 66

def method_missing(meth, *args, &block)
  super
end

Instance Attribute Details

#api_methodObject

Returns the value of attribute api_method.



9
10
11
# File 'lib/survey_monkey/parameters.rb', line 9

def api_method
  @api_method
end

#method_parametersObject (readonly)

Returns the value of attribute method_parameters.



10
11
12
# File 'lib/survey_monkey/parameters.rb', line 10

def method_parameters
  @method_parameters
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/survey_monkey/parameters.rb', line 9

def uri
  @uri
end

Instance Method Details

#api_settingsObject



70
71
72
# File 'lib/survey_monkey/parameters.rb', line 70

def api_settings
    @settings
end

#http_methodObject



58
59
60
# File 'lib/survey_monkey/parameters.rb', line 58

def http_method
  ( api_settings[api_method]['http_method'] || 'post' ).to_sym
end

#to_hashObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/survey_monkey/parameters.rb', line 21

def to_hash
  converted_hash = Hash.new
  method_parameters.keys.each do |root|
    if method_parameters[root].is_a? Hash   ## The parameter has children
      converted_hash[root] = Hash.new
      method_parameters[root].keys.each do |child|
        if method_parameters[root][child].is_a? Hash   ## has grand_child
          method_parameters[root].keys.each do |grand_child|
            # save the value for each grand_child into the hash
            converted_hash[root][child][grand_child] = self.send("#{root}_#{child}_#{grand_child}")
          end
          # Delete any nil grand_child keys
          converted_hash[root][child].delete_if{ |k, v| v.nil? } if converted_hash[root].has_key?(child) 
        elsif method_parameters[root][child].is_a? Array
          # child is an array. elements of array to hash
          value = self.send("#{root}_#{child}")
          unless value.empty?
            converted_hash[root][child] = value
            converted_hash[root][child].map{ |e| e.to_hash }
          end
        else
          value = self.send("#{root}_#{child}")
          converted_hash[root][child] = value unless value.nil? ## save value for child (dont assign nil values)
        end
        # Delete any root keys with nil values
        converted_hash[root] = converted_hash[root].delete_if{ |k, v| v.nil? } if converted_hash.has_key?(root)
      end
    elsif method_parameters[root].is_a? Array
      converted_hash[root] = self.send("#{root}").to_hash.delete_if{ |k, v| v.nil? } ## root Array
    else
      value = self.send("#{root}") ## No children
      converted_hash[root] = value unless value.nil?
    end
  end
  converted_hash
end

#to_jsonObject



62
63
64
# File 'lib/survey_monkey/parameters.rb', line 62

def to_json
  self.to_hash.delete_if{ |k, v| v.nil? }.to_json
end