class SurveyMonkey::Parameters

Attributes

api_method[RW]
method_parameters[R]
uri[RW]

Public Class Methods

new(request_method) { |self| ... } click to toggle source
# 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

Public Instance Methods

api_settings() click to toggle source
# File lib/survey_monkey/parameters.rb, line 70
def api_settings
    @settings
end
http_method() click to toggle source
# File lib/survey_monkey/parameters.rb, line 58
def http_method
  ( api_settings[api_method]['http_method'] || 'post' ).to_sym
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/survey_monkey/parameters.rb, line 66
def method_missing(meth, *args, &block)
  super
end
to_hash() click to toggle source
# 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_json() click to toggle source
# File lib/survey_monkey/parameters.rb, line 62
def to_json
  self.to_hash.delete_if{ |k, v| v.nil? }.to_json
end

Private Instance Methods

check_for_valid_api_method(api_method) click to toggle source
# File lib/survey_monkey/parameters.rb, line 76
def check_for_valid_api_method(api_method)
  raise "api_method must be defined in initialization" if api_method.nil?
  raise "Request Type #{api_method} not found" if api_settings[api_method].nil?
end
define_parameter_methods(api_method) click to toggle source
# File lib/survey_monkey/parameters.rb, line 87
def define_parameter_methods(api_method)
  @method_parameters = api_settings[api_method]['parameters']

  self.instance_eval do
    @method_parameters.each do |root_key,root_value|
      if ( root_value == 'optional' ) || ( root_value == 'required' ) || ( root_value == 'conditionally_required' )
        self.singleton_class.class_eval "attr_accessor :#{root_key}"
      elsif root_value.is_a? Array
        ### COLLECTION! key should be plural
        self.singleton_class.class_eval "attr_accessor :#{root_key}" 
        camel_key = plural_key.camelize
        eval "@#{root_key} = #{camel_key}.new"
      elsif root_value.is_a? Hash
        root_value.each do |child_key,child_value|
          if root_value[child_key].is_a? Hash
            ## Child HAS Child ( grand_child is leaf )
            root_value[child_key].each do |grand_child_key,grand_child_value| 
              if ( grand_child_value == 'optional' ) || ( grand_child_value == 'required' ) || ( grand_child_value == 'conditionally_required' )
                self.singleton_class.class_eval "attr_accessor :#{root_key}_#{child_key}_#{grand_child_key}" 
              else
                raise "Invalid Configuration: Leaf Should be 'required' OR 'optional'"
              end
            end
          elsif root_value[child_key].is_a? Array
            self.singleton_class.class_eval "attr_accessor :#{root_key}_#{child_key}" 
            camel_key = "#{child_key}".camelize
            eval "@#{root_key}_#{child_key} = #{camel_key}.new"
          else
            ## Child has no child ( child is leaf )
            if ( child_value == 'optional' ) || ( child_value == 'required' ) || ( child_value == 'conditionally_required' )
              self.singleton_class.class_eval "attr_accessor :#{root_key}_#{child_key}" 
            else
              raise "Invalid Configuration: Leaf Should be 'required' OR 'optional'"
            end
          end
        end
      else
        raise "Invalid Configuration: Leaf Should be 'required' OR 'optional'"
      end
    end
  end
end
load_yaml_settings() click to toggle source
# File lib/survey_monkey/parameters.rb, line 81
def load_yaml_settings
  file = File.open API_SETTINGS_PATH
  @settings = YAML.load file
  raise 'api_settings.yml NOT FOUND' if @settings.nil? || @settings.empty?
end