lib/quick_travel/adapter.rb in quicktravel_client-3.6.1 vs lib/quick_travel/adapter.rb in quicktravel_client-3.7.0
- old
+ new
@@ -23,31 +23,10 @@
# { :passenger_type => {"position"=>1, "name"=>"Adult", "maximum_age"=>30, "id"=>1} },
# { :passenger_type => {"position"=>2, "name"=>"Child", "maximum_age"=>15, "id"=>2} }
# ]
class_attribute :object_key # Key of sub-objects (i.e. convert under a key)
- def initialize(hash = {})
- return nil if hash.blank?
- define_readers(hash.keys)
- super(Parser.new(hash).parsed_attributes)
- end
-
- def define_readers(keys)
- keys.each do |key|
- next if respond_to?(key)
- define_singleton_method(key) { instance_variable_get("@#{key}") }
- if key.to_s.ends_with? '_cents'
- name = key.to_s.gsub(/_in_cents$/, '')
- define_singleton_method(name) {
- cents = instance_variable_get("@#{key}")
- return nil unless cents
- Money.new(cents)
- }
- end
- end
- end
-
def self.has_many(relation_name, options = {})
define_method relation_name do
instance_variable_get("@#{relation_name}") || instance_variable_set(
"@#{relation_name}",
begin
@@ -103,10 +82,18 @@
def to_hash
instance_values
end
+ def to_s
+ if defined? @to_s
+ @to_s
+ else
+ super
+ end
+ end
+
protected
def self.check_id!(id)
Integer(id)
rescue ArgumentError, # if invalid string
@@ -218,46 +205,9 @@
Api.call_and_validate(http_method, path, query, opts)
end
def self.base_uri(uri = nil)
Api.base_uri uri
- end
- end
-
- class Parser
- def initialize(attributes)
- @attributes = attributes
- end
-
- def attributes
- @attributes ||= {}
- end
-
- def parsed_attributes
- @parsed_attributes ||= parse_attributes
- end
-
- private
-
- def parse_attributes
- attributes.map.with_object({}) do |(attribute, value), hash|
- hash[attribute] = parse(attribute, value)
- end
- end
-
- def parse(attribute, value)
- return nil if value.nil?
- return convert(value, :to_date) if attribute.to_s.ends_with?('_date')
- return convert(value, :to_date) if attribute.to_s.ends_with?('_on')
- # to_datetime as it converts to app time zone, to_time converts to system time zone
- return convert(value, :to_datetime) if attribute.to_s.ends_with?('_time')
- return convert(value, :to_datetime) if attribute.to_s.ends_with?('_at')
- value
- end
-
- def convert(value, conversion_method)
- convertable_value = value.is_a?(Hash) ? value['_value'] : value
- convertable_value.send(conversion_method)
end
end
class Api
include HTTParty