Module: Balanced::Resource::ClassMethods
- Defined in:
- lib/balanced/resources/resource.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #collection_name ⇒ Object
- #collection_path ⇒ Object
- #construct_from_response(payload) ⇒ Object
- #find(*arguments) ⇒ Object
- #member_name ⇒ Object
- #paginate(options = {}) ⇒ Object (also: #scoped, #where)
- #resource_name ⇒ Object
-
#uri ⇒ String
Returns the resource URI for a given class.
Instance Method Details
#all(options = {}) ⇒ Object
229 230 231 232 |
# File 'lib/balanced/resources/resource.rb', line 229 def all = {} pager = paginate() pager.to_a end |
#collection_name ⇒ Object
145 146 147 |
# File 'lib/balanced/resources/resource.rb', line 145 def collection_name Utils.pluralize Utils.underscore(resource_name) end |
#collection_path ⇒ Object
149 150 151 |
# File 'lib/balanced/resources/resource.rb', line 149 def collection_path ["/v#{Balanced.config[:version]}", collection_name].compact.join '/' end |
#construct_from_response(payload) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/balanced/resources/resource.rb', line 181 def construct_from_response payload payload = Balanced::Utils.hash_with_indifferent_read_access payload return payload if payload[:uri].nil? klass = Balanced.from_uri(payload[:uri]) instance = klass.new payload # http://stackoverflow.com/a/2495650/133514 instance_eigen = class << instance; self; end payload.each do |name, value| # here is where our interpretations will begin. # if the value is a sub-resource, lets instantiate the class # and set it correctly if value.instance_of? Hash and value.has_key? 'uri' value = construct_from_response value end # Get attribute instance.class.send(:define_method, name, proc{@attributes[name]}) # Set attribute instance.class.send(:define_method, "#{name}=", proc{ |value| @attributes[name] = value }) # Is attribute present? instance.class.send(:define_method, "#{name}?", proc{ !!@attributes[name] }) instance.send("#{name}=".to_s, value) end instance end |
#find(*arguments) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/balanced/resources/resource.rb', line 211 def find *arguments scope = arguments.slice!(0) = arguments.slice!(0) || {} case scope when :all then all() when :first then paginate().first else response = Balanced.get scope, construct_from_response response.body end end |
#member_name ⇒ Object
153 154 155 |
# File 'lib/balanced/resources/resource.rb', line 153 def member_name Utils.underscore resource_name end |
#paginate(options = {}) ⇒ Object Also known as: scoped, where
223 224 225 |
# File 'lib/balanced/resources/resource.rb', line 223 def paginate = {} Balanced::Pager.new uri, end |
#resource_name ⇒ Object
141 142 143 |
# File 'lib/balanced/resources/resource.rb', line 141 def resource_name Utils.demodulize name end |
#uri ⇒ String
Returns the resource URI for a given class.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/balanced/resources/resource.rb', line 163 def uri # the uri of a particular resource depends if there's a marketplace # created or not. if there's a marketplace, then all resources have their # own uri from there and the top level ones. if there's not a marketplace # # if there's an api key, then the merchant is available # if there's no api key, the only resources exposed are purely top level if self == Balanced::Merchant || self == Balanced::Marketplace || self == Balanced::ApiKey collection_path else unless Balanced::Marketplace.marketplace_exists? raise Balanced::StandardError, "#{self.name} is nested under a marketplace, which is not created or configured." end Balanced::Marketplace.marketplace_uri + "/#{collection_name}" end end |