lib/chord.rb in chord-0.0.1 vs lib/chord.rb in chord-0.0.2
- old
+ new
@@ -11,43 +11,49 @@
include HTTParty
class << self
attr_writer :per_page
def per_page; @per_page || 99999; end
- end
- def self.base_url
- CHORD_API_CONFIG[Chord.env][:base_url]
- end
+ def all
+ @all ||= fetch_all_data[base_path].map{ |i| new(i['id'], i) }
+ end
- def self.find(id)
- attrs = get(base_url + "#{base_path}/#{id}", http_options).parsed_response
- new(id, attrs)
- end
+ def where(query_options = {})
+ fetch_all_data(query_options)[base_path].map{ |i| new(i['id'], i) }
+ end
- def self.fetch_all_data(query_options = {})
- query_options = {
- per_page: per_page
- }.merge(query_options)
- get(base_url + "#{base_path}?#{hash_to_query(query_options)}", http_options).parsed_response
- 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)
+ end
- def self.all(query_options = {})
- @all ||= fetch_all_data(query_options)[base_path].map{ |i| new(i['id'], i) }
- 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 self.http_options
- {headers: {
- 'Authorization' => "Bearer #{CHORD_API_CONFIG[Chord.env][:api_key]}",
- 'Content-Type' => 'application/json'
- }}
- end
+ def base_url
+ CHORD_API_CONFIG[Chord.env][:base_url]
+ end
- def self.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 * '&'
+ def http_options
+ {headers: {
+ 'Authorization' => "Bearer #{CHORD_API_CONFIG[Chord.env][:api_key]}",
+ 'Content-Type' => 'application/json'
+ }}
+ end
+
+ private # --------------------------------------------------------------
+
+ 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 * '&'
+ end
end
def base_url
self.class.base_url
end