lib/chord.rb in chord-0.0.6 vs lib/chord.rb in chord-0.0.7

- old
+ new

@@ -43,37 +43,26 @@ class << self attr_writer :per_page def per_page; 99999; end def all + check_for_config! @all ||= fetch_all_data[base_path].map{ |i| new(i[id_attribute], i) } end def where(query_options = {}) + check_for_config! fetch_all_data(query_options)[base_path].map{ |i| new(i[id_attribute], i) } end def find(id) + check_for_config! return nil if id.nil? or id == '' 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 - def base_url Chord.base_url end def http_options @@ -83,10 +72,32 @@ }} end private # -------------------------------------------------------------- + def id_attribute + 'id' + end + + def fetch_attributes(id) + check_for_config! + get(base_url + "#{base_path}/#{id}", http_options).parsed_response or raise APIError, 'No data returned by API' + end + + def fetch_all_data(query_options = {}) + check_for_config! + 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 or raise APIError, 'No data returned by API' + end + + def check_for_config! + if Chord.base_url.nil? or Chord.api_key.nil? + raise ConfigurationError, 'Please configure Chord by calling Chord.config(base_url: ..., api_key: ...)' + end + end + def hash_to_query(hash) require 'cgi' unless defined?(CGI) && defined?(CGI.escape) hash.collect{ |p| p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '=' }.compact.sort * '&' @@ -112,18 +123,10 @@ @id = id @attributes = attributes end def update(new_attributes) - new_attributes.stringify_keys! - # merge values into existing metadata - if new_attributes.include?('metadata') - # Chord expects all metadata values to be strings - new_metadata = new_attributes['metadata'].map{ |k,v| [k.to_s, v.to_s] }.to_h - new_attributes['metadata'] = (attributes['metadata'] || {}).merge(new_metadata) - # TODO: delete entries with empty value? - end self.attributes = self.class.patch(base_url + "#{base_path}/#{id}", http_options.merge(body: new_attributes.to_json) ).parsed_response end @@ -132,11 +135,11 @@ 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) + all_attributes = self.class.send(:fetch_attributes, id) @attributes = all_attributes.merge(@attributes) end def method_missing(method, *args, &block) if attributes.include?(method.to_s) @@ -263,7 +266,13 @@ class Product < Base def self.base_path 'products' end + end + + class ConfigurationError < StandardError + end + + class APIError < StandardError end end