lib/chord.rb in chord-0.0.4 vs lib/chord.rb in chord-0.0.5
- old
+ new
@@ -26,26 +26,34 @@
class Base
include HTTParty
class << self
attr_writer :per_page
- def per_page; @per_page || 99999; end
+ def per_page; 99999; end
def all
- @all ||= fetch_all_data[base_path].map{ |i| new(i['id'], i) }
+ @all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) }
end
def where(query_options = {})
- fetch_all_data(query_options)[base_path].map{ |i| new(i['id'], i) }
+ fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) }
end
def find(id)
return nil if id.nil? or id == ''
- attrs = get(base_url + "#{base_path}/#{id}", http_options).parsed_response
- attrs.include?('error') ? nil : new(id, attrs)
+ attrs = fetch_attributes(id)
+ attrs.include?('error') ? nil : new(attrs[id_attribute], attrs)
end
+ def fetch_attributes(id)
+ get(base_url + "#{base_path}/#{id}", http_options).parsed_response
+ end
+
+ def id_attribute
+ 'id'
+ end
+
def fetch_all_data(query_options = {})
query_options = { per_page: per_page }.merge(query_options)
url = base_url + base_path + '?' + hash_to_query(query_options)
get(url, http_options).parsed_response
end
@@ -107,10 +115,17 @@
def delete
self.class.delete(base_url + "#{base_path}/#{id}", http_options).parsed_response
end
+ # fetch all attributes, but don't overwrite existing ones,
+ # in case changes have been made
+ def expand!
+ all_attributes = self.class.fetch_attributes(id)
+ @attributes = all_attributes.merge(@attributes)
+ end
+
def method_missing(method, *args, &block)
if attributes.include?(method.to_s)
attributes[method.to_s]
else
super
@@ -173,12 +188,16 @@
def self.base_path
'orders'
end
+ def self.id_attribute
+ 'number'
+ end
+
def user
- Chord::User.find(attributes['user_id'])
+ @user ||= Chord::User.find(attributes['user_id'])
end
def payments
self.class.get(base_url + "orders/#{id}/payments", http_options).parsed_response['payments'].map{ |p| Chord::Payment.new(p['id'], p) }
end
@@ -196,12 +215,10 @@
def subscription_installment?
channel == 'subscriptions'
end
def subscription_start?
- unless attributes.include?('subscription_in_cart')
- attributes['subscription_in_cart'] = Chord::Order.find(number).subscription_in_cart
- end
+ expand! unless attributes.include?('subscription_in_cart')
subscription_in_cart
end
end