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
189 190 191 192 |
# File 'lib/balanced/resources/resource.rb', line 189 def all = {} pager = paginate() pager.to_a end |
#collection_name ⇒ Object
94 95 96 |
# File 'lib/balanced/resources/resource.rb', line 94 def collection_name Utils.pluralize Utils.underscore(resource_name) end |
#collection_path ⇒ Object
98 99 100 |
# File 'lib/balanced/resources/resource.rb', line 98 def collection_path ["/v#{Balanced.config[:version]}", collection_name].compact.join '/' end |
#construct_from_response(payload) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/balanced/resources/resource.rb', line 130 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 payload.each do |name, value| klass.class_eval { attr_accessor name.to_s } # 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 elsif name =~ /_uri$/ modified_name = name.sub(/_uri$/, '') klass.instance_eval { define_method(modified_name) { values_class = Balanced.from_uri(value) # if uri is a collection -> this would definitely be if it ends in a symbol # then we should allow a lazy executor of the query pager if Balanced.is_collection(value) pager = Pager.new value, {} pager.to_a else values_class.find(value) end } } end instance.class.instance_eval { define_method(name) { @attributes[name] } # Get. define_method("#{name}=") { |value| @attributes[name] = value } # Set. define_method("#{name}?") { !!@attributes[name] } # Present. } instance.send("#{name}=".to_s, value) end instance end |
#find(*arguments) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/balanced/resources/resource.rb', line 171 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
102 103 104 |
# File 'lib/balanced/resources/resource.rb', line 102 def member_name Utils.underscore resource_name end |
#paginate(options = {}) ⇒ Object Also known as: scoped, where
183 184 185 |
# File 'lib/balanced/resources/resource.rb', line 183 def paginate = {} Pager.new uri, end |
#resource_name ⇒ Object
90 91 92 |
# File 'lib/balanced/resources/resource.rb', line 90 def resource_name Utils.demodulize name end |
#uri ⇒ String
Returns the resource URI for a given class.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/balanced/resources/resource.rb', line 112 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 |