Class: Easy311::Request

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Model
Defined in:
lib/easy311/request.rb

Constant Summary collapse

POST_KEYS =
[
  :service_code,
  :description,
  :lat,
  :long,
  :email,
  :device_id,
  :account_id,
  :first_name,
  :last_name,
  :phone,
  :description,
  :media_url,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Request

Returns a new instance of Request.



40
41
42
43
# File 'lib/easy311/request.rb', line 40

def initialize(service)
  @attrs_values = ActiveSupport::HashWithIndifferentAccess.new
  @service = service
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/easy311/request.rb', line 49

def method_missing(method, *args, &block)
  attr_name = method.to_s.gsub(/=$/, '')
  if service.attrs.has_key?(attr_name)
    if method.to_s.match(/=$/)
      @attrs_values[attr_name] = args.first
    else
      @attrs_values[attr_name]
    end
  else
    super
  end
end

Instance Attribute Details

#attrs_valuesObject

Returns the value of attribute attrs_values.



19
20
21
# File 'lib/easy311/request.rb', line 19

def attrs_values
  @attrs_values
end

#serviceObject (readonly)

Returns the value of attribute service.



18
19
20
# File 'lib/easy311/request.rb', line 18

def service
  @service
end

Instance Method Details

#attributesObject



45
46
47
# File 'lib/easy311/request.rb', line 45

def attributes
  super.merge("attrs" => Hash[service.attrs.map { |k, v| [k.to_s, self.send(k)]}])
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/easy311/request.rb', line 62

def respond_to?(method, include_private=false)
  if service.attrs.has_key?(method.to_s.gsub(/=$/, ''))
    true
  else
    super
  end
end

#to_post_paramsObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/easy311/request.rb', line 70

def to_post_params
  params = attributes.select { |k,v| !v.nil? and POST_KEYS.include? k.to_sym }
  params['service_code'] = @service.code if @service.code

  attrs_values.each do |key, value|
    param_key = "attribute[#{key}]"
    params[param_key] = value
  end

  params
end