class Peaty::Base
Constants
- FILTERS
Attributes
connection[RW]
attributes[RW]
connection[RW]
error[RW]
Public Class Methods
all(options = {})
click to toggle source
# File lib/peaty/base.rb, line 99 def all(options = {}) self.parse(self.connection[self.collection_path(options)].get(:params => self.filter_options(options)).body, self.element). each { |e| e.connection = self.connection } end
build(attrs = {})
click to toggle source
# File lib/peaty/base.rb, line 24 def self.build(attrs = {}) new(attrs) end
filter_options(options = {}, filter = [])
click to toggle source
# File lib/peaty/base.rb, line 108 def filter_options(options = {}, filter = []) options = options.dup # make sure we're working on a copy # and delete any keys not supported for queries options.each { |(k,_)| options.delete(k) unless FILTERS.include?(k.to_sym) } FILTERS.each do |term| value = Array.wrap(options.delete(term)) filter << "%s:%s" % [ term, value.map do |v| v = %("%s") % v if v.to_s =~ /\s/ v end.join(',') ] unless value.empty? end # handle the rest of the filter strings Array.wrap(options.delete(:rest)).each do |value| value = %("%s") % value if value.to_s =~ /\s/ filter << value end return options if filter.empty? options.merge(:filter => filter.join(" ")) end
find(*args)
click to toggle source
# File lib/peaty/base.rb, line 81 def find(*args) options = args.extract_options! selection = args.shift case selection when :first; self.first(options) when :all; self.all(options) when Array; selection.map{ |s| self.find_by_id(s, options) } when Numeric; self.find_by_id(selection, options) else self.find_by_id(selection, options) end end
find_by_id(id, options = {})
click to toggle source
# File lib/peaty/base.rb, line 93 def find_by_id(id, options = {}) self.parse(self.connection[self.member_path(id, options)].get(:params => self.filter_options(options)).body, self.element). first. tap{ |e| e.connection = self.connection } end
first(options = {})
click to toggle source
# File lib/peaty/base.rb, line 104 def first(options = {}) self.all(options).first end
new(attrs)
click to toggle source
# File lib/peaty/base.rb, line 15 def initialize(attrs) raise ArgumentError unless attrs.is_a?(Hash) @connection = self.class.connection # if we get a hash like {"item"=>{...}}, pull out the attributes @attributes = if attrs.key?(self.class.element); attrs.dup.delete(self.class.element) else attrs.dup end end
parse(response, element)
click to toggle source
Takes the XML result, transforms to JSON, parses to objects, and returns an array of results, regardless of one or many results.
# File lib/peaty/base.rb, line 76 def parse(response, element) result = JSON.parse(XmlToJson.transform(response)) Array.wrap(result[element] || result[element.pluralize]).map{ |r| new(r) } end
with_connection(connection)
click to toggle source
# File lib/peaty/base.rb, line 69 def with_connection(connection) @connection = connection self # chaining end
Public Instance Methods
error?()
click to toggle source
# File lib/peaty/base.rb, line 53 def error? !!@error end
id()
click to toggle source
# File lib/peaty/base.rb, line 45 def id self.attributes["id"] end
method_missing(method, *args)
click to toggle source
Calls superclass method
# File lib/peaty/base.rb, line 28 def method_missing(method, *args) method.to_s =~ /^([^\?\=]+)(\?|\=)?$/ method_name, predicate = $1, $2 case predicate when '?'; return self.attributes[method_name].present? when '='; return self.attributes[method_name] = args.first else return self.attributes[method_name] if respond_to?(method_name) end super end
new_record?()
click to toggle source
# File lib/peaty/base.rb, line 49 def new_record? id.nil? or id.to_i.zero? end
respond_to?(method)
click to toggle source
Calls superclass method
# File lib/peaty/base.rb, line 41 def respond_to?(method) super or self.attributes.key?(method.to_s) end
save()
click to toggle source
# File lib/peaty/base.rb, line 57 def save @error = nil # reset error @attributes.delete_if{ |k, v| v.nil? } # ignore nil attributes @attributes.replace self.class.parse(self.connection[self.class.collection_path(@attributes)].post(self.class.element => @attributes).body, self.class.element).first.attributes rescue RestClient::UnprocessableEntity => e @error = JSON.parse(XmlToJson.transform(e.response.body))["message"] false end