Created
March 10, 2012 21:19
-
-
Save DanKnox/2013235 to your computer and use it in GitHub Desktop.
RestClient Model Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ModelExtensions | |
def self.included(base_class) | |
base_class.send(:include,ActiveModel::Serializers::JSON) | |
base_class.send(:extend,ActiveModel::Naming) | |
base_class.send(:extend,ActiveModel::Translation) | |
base_class.send(:include,ActiveModel::AttributeMethods) | |
base_class.send(:include,ActiveModel::Validations) | |
base_class.send(:include,ActiveModel::Conversion) | |
base_class.send(:validate,:check_base_errors) | |
end | |
def check_base_errors | |
errors.add('base','invalid') if self.errors.any? | |
false if self.errors.any? | |
end | |
def initialize(attributes={}) | |
self.attributes = attributes | |
@collection_store = Hash.new | |
end | |
def read_attribute_for_validation(key) | |
@attributes[key] | |
end | |
def meta_class | |
(class << self; self; end) | |
end | |
def valid? | |
validate! | |
end | |
def attributes | |
@attributes | |
end | |
def persisted? | |
false | |
end | |
def attributes=(args) | |
@attributes ||= Hash.new | |
args.each do |key,value| | |
if value.is_a?(Hash) | |
(self.attributes = value and next) if key.camelize.singularize == self.class.name | |
if key.to_s == 'errors' | |
value.each {|attri,error| errors.add attri, error } and next | |
end | |
self.collection_methods = {class_name: key, collection: value} and next | |
end | |
if value.is_a?(Array) | |
self.collection_methods = {class_name: key, collection: value} and next | |
end | |
@attributes[key] = value | |
instance_variable_set("@#{key}",value) | |
end | |
end | |
def collection_methods=(args) | |
collection_name = args[:class_name].to_sym | |
class_name = args[:class_name].camelize.singularize | |
class_const = Object::const_get(class_name.to_s) | |
meta_class.send(:define_method,collection_name) do | |
@collection_store[collection_name] | |
end | |
if args[:collection].is_a?(Hash) | |
@collection_store[collection_name] = class_const.new(args[:collection]) | |
else | |
args[:collection].each do |hash| | |
class_object = class_const.new | |
class_object.attributes = hash | |
@collection_store[collection_name] ||= [] | |
@collection_store[collection_name] << class_object | |
end | |
end | |
end | |
def update_attributes(new_attributes,access_token=nil) | |
new_attributes.merge!(access_token) | |
success = false | |
@uri ||= WEBSITE_API_PATH | |
@uri = @uri + self.class.name.downcase.pluralize + "/" + self.id.to_s | |
begin | |
RestClient.put @uri, new_attributes do |response,request,body,&block| | |
case response.code | |
when 200 | |
self.attributes = ActiveSupport::JSON.decode(response.body).first.last | |
success = true | |
when 422 || 404 | |
errors_hash = ActiveSupport::JSON.decode(response.body) | |
errors_hash.each do |key,value| | |
self.errors.add("#{key}","Problem updating #{key}. Please fix #{value}.") | |
end | |
success = false | |
else | |
self.errors.add('base',"Unknown system error.") | |
end | |
end | |
rescue | |
success = false | |
end | |
success | |
end | |
def method_missing(method,*args,&block) | |
if method =~ /=/ | |
@attributes[method.to_s] = args.first | |
return @attributes[method.to_s] | |
end | |
if @attributes.keys.include?(method.to_s) | |
return @attributes[method.to_s] | |
else | |
super | |
end | |
end | |
def respond_to(*args) | |
return true if @attributes.has_key?(args.first.to_s) | |
super | |
end | |
def include_root_in_json | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment