Class: Easy311::Request
- Inherits:
-
Object
- Object
- Easy311::Request
- 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
-
#attrs_values ⇒ Object
Returns the value of attribute attrs_values.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(service) ⇒ Request
constructor
A new instance of Request.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
- #to_post_params ⇒ Object
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_values ⇒ Object
Returns the value of attribute attrs_values.
19 20 21 |
# File 'lib/easy311/request.rb', line 19 def attrs_values @attrs_values end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
18 19 20 |
# File 'lib/easy311/request.rb', line 18 def service @service end |
Instance Method Details
#attributes ⇒ Object
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
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_params ⇒ Object
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 |