Module: Recurly::Schema::RequestCaster
- Included in:
- Request
- Defined in:
- lib/recurly/schema/request_caster.rb
Overview
Note: This module is for internal use. The RequestCaster turns mixed data into a pure Hash so it can be serialized into JSON and used as the body of a request. This module is to be extended by the Request class.
Instance Method Summary collapse
-
#cast_request(data, schema = self.schema) ⇒ Hash
This method casts the data object (of mixed types) into a Hash ready for JSON serialization.
Instance Method Details
#cast_request(data, schema = self.schema) ⇒ Hash
This method casts the data object (of mixed types) into a Hash ready for JSON serialization. The schema will default to the self's schema. You should pass in the schema where possible. This is because objects are serialized differently depending on the context in which they are being written.
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 57 |
# File 'lib/recurly/schema/request_caster.rb', line 29 def cast_request(data, schema = self.schema) casted = {} if data.is_a?(Resource) || data.is_a?(Request) data = data.attributes.reject { |_k, v| v.nil? } end data.each do |k, v| schema_attr = schema.get_attribute(k) norm_val = if v.respond_to?(:attributes) cast_request(v, v.class.schema) elsif v.is_a?(Array) v.map do |elem| if elem.respond_to?(:attributes) cast_request(elem, elem.class.schema) else elem end end elsif v.is_a?(Hash) && schema_attr && schema_attr.is_a?(Schema::ResourceAttribute) cast_request(v, schema_attr.recurly_class.schema) else v end casted[k] = norm_val end casted end |