Class: BadgevilleBerlin::BaseResource
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- BadgevilleBerlin::BaseResource
- Defined in:
- lib/badgeville_berlin/base_resource.rb
Overview
Subclasses ActiveResource::Base as BaseResource
Direct Known Subclasses
Activity, ActivityDefinition, Config, Group, Leaderboard, Player, Reward, RewardDefinition, Site, Track, User
Constant Summary collapse
- COMPLEX_ATTRIBUTES =
[]
Instance Method Summary collapse
-
#encode(options = {}) ⇒ Object
Overrides encode call to prevent to_json from converting non-valid type objects to nested-json hash (e.g. BadgevilleBerlin::ActivityDefinition::Selector) to allow for 200 OK response on PUT.
-
#errors ⇒ BadgevilleBerlin::Errors
Overrides the ActiveResource instance method in module Validations in order to call the BadgevilleBerlin::Errors constructor instead of the ActiveResource::Errors constructor.
-
#initialize(attributes = {}, persisted = false) ⇒ BaseResource
constructor
A new instance of BaseResource.
- #load(attributes, remove_root = false) ⇒ Object
-
#load_remote_errors(remote_errors, save_cache = false) ⇒ Object
Overrides the ActiveResource instance method in module Validations in order to load_remote_errors() for the case where the format is the custom BadgevilleJson format.
- #sanitize_request ⇒ Object
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ BaseResource
Returns a new instance of BaseResource.
9 10 11 12 13 14 15 |
# File 'lib/badgeville_berlin/base_resource.rb', line 9 def initialize(attributes = {}, persisted = false) #we return a nested JSON response with player rewards keyed off of mongo id's #on groups endpoint which causes activeresource to break when looking up a #physical id as an attribute on an activeresource model. fix: attributes["rewards"] = attributes["rewards"].try(:values) if self.class.to_s == "BadgevilleBerlin::Group" super end |
Instance Method Details
#encode(options = {}) ⇒ Object
Overrides encode call to prevent to_json from converting non-valid type objects to nested-json hash (e.g. BadgevilleBerlin::ActivityDefinition::Selector) to allow for 200 OK response on PUT
58 59 60 61 |
# File 'lib/badgeville_berlin/base_resource.rb', line 58 def encode(={}) sanitize_request send("to_#{self.class.format.extension}", ) end |
#errors ⇒ BadgevilleBerlin::Errors
Overrides the ActiveResource instance method in module Validations in order to call the BadgevilleBerlin::Errors constructor instead of the ActiveResource::Errors constructor.
errors messages from the remote server and mimics the interface of the errors provided by ActiveRecord::Errors.
79 80 81 |
# File 'lib/badgeville_berlin/base_resource.rb', line 79 def errors @errors ||= BadgevilleBerlin::Errors.new(self) end |
#load(attributes, remove_root = false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/badgeville_berlin/base_resource.rb', line 17 def load(attributes, remove_root = false) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) , attributes = (attributes) if attributes.keys.size == 1 remove_root = self.class.element_name == attributes.keys.first.to_s end attributes = ActiveResource::Formats.remove_root(attributes) if remove_root attributes.each do |key, value| @attributes[key.to_s] = case value when Array resource = nil value.map do |attrs| if attrs.is_a?(Hash) resource ||= find_or_create_resource_for_collection(key) resource.new(attrs) else attrs.duplicable? ? attrs.dup : attrs end end when Hash if self.class::COMPLEX_ATTRIBUTES.include?(key) #if the key is selector or adjustment, as on the ActivityDefinition object, we don't want to create a nested resource value else resource = find_or_create_resource_for(key) resource.new(value) end else value.duplicable? ? value.dup : value end end self end |
#load_remote_errors(remote_errors, save_cache = false) ⇒ Object
Overrides the ActiveResource instance method in module Validations in order to load_remote_errors() for the case where the format is the custom BadgevilleJson format. Loads the set of remote errors into the object’s Errors collection based on the content-type of the error-block received.
cleared by default
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/badgeville_berlin/base_resource.rb', line 92 def load_remote_errors(remote_errors, save_cache = false ) #:nodoc: case self.class.format when ActiveResource::Formats[:xml] errors.from_xml(remote_errors.response.body, save_cache) when ActiveResource::Formats[:json] errors.from_json(remote_errors.response.body, save_cache) when ActiveResource::Formats[:badgeville_berlin_json] errors.from_badgeville_berlin_json(remote_errors.response.body, save_cache) end end |
#sanitize_request ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/badgeville_berlin/base_resource.rb', line 63 def sanitize_request valid_types = ["String", "Fixnum", "NilClass", "TrueClass", "FalseClass", "ActiveSupport::HashWithIndifferentAccess", "Float", "Array"] self.attributes.values.each_with_index do |k,index| if !valid_types.include?(self.attributes[self.attributes.keys[index]].class.to_s) self.attributes[self.attributes.keys[index]] = self.attributes[self.attributes.keys[index]].attributes.to_json end end end |