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, Team, Track, User
Constant Summary collapse
- COMPLEX_ATTRIBUTES =
[]
Instance Method Summary collapse
-
#customize_keys_for_request(attrs) ⇒ Object
Enables revising Berlin JSON response key names to match what is expected by model-level logic.
-
#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
#customize_keys_for_request(attrs) ⇒ Object
Enables revising Berlin JSON response key names to match what is expected by model-level logic. (e.g. Berlin JSON payload returns the player’s nickname under the key :nick_name, but the Player model expects it as :nickname without the underscore.)
:nickname.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/badgeville_berlin/base_resource.rb', line 62 def customize_keys_for_request(attrs) # The given resource type determined by self.class. For each attribute key # in the ActiveResource object that needs renaming, replace the original # hash key with the revised hash key as specified in the resource type # -specific constant CUSTOM_ATTRS_FOR_REQUEST. if defined?(self.class::CUSTOM_REQUEST_KEYS) self.class::CUSTOM_REQUEST_KEYS.each do |orig_key, revised_key| attrs[revised_key] = attrs.delete(orig_key) if attrs.include?(orig_key) end end attrs end |
#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
79 80 81 82 |
# File 'lib/badgeville_berlin/base_resource.rb', line 79 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.
100 101 102 |
# File 'lib/badgeville_berlin/base_resource.rb', line 100 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) @prefix_options, 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 customize_keys_for_request(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
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/badgeville_berlin/base_resource.rb', line 113 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
84 85 86 87 88 89 90 91 |
# File 'lib/badgeville_berlin/base_resource.rb', line 84 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 |