lib/a2z/responses/item.rb in a2z-0.0.1 vs lib/a2z/responses/item.rb in a2z-0.0.2
- old
+ new
@@ -5,14 +5,42 @@
attr_accessor :asin, :detail_page_url, :links
def initialize
@links = []
+ @attrs = {}
end
+ def keys
+ @attrs.keys
+ end
+
+ def respond_to_missing?(name, include_private = false)
+ name.to_s.end_with?('?') || super
+ end
+
+ def method_missing(name, *args, &block)
+ method_name = name.to_s
+
+ if method_name.end_with?('?') # && method_name != 'has_key?'
+ has_key?(method_name.sub(/\?$/, ''))
+ elsif has_key?(method_name)
+ self[method_name]
+ else
+ super
+ end
+ end
+
def []=(key, value)
- instance_variable_set("@#{key}".to_sym, value)
- self.class.class_eval { attr_reader key.to_sym }
+ @attrs[key] = value
+ end
+
+ def [](key)
+ @attrs[key]
+ end
+
+ def has_key?(key)
+ @attrs.has_key?(key)
end
def self.from_response(data)
new.tap do |item|
item.asin = data['ASIN']