Class: Skroutz::CollectionProxy
- Inherits:
-
Object
- Object
- Skroutz::CollectionProxy
- Includes:
- Parsing, UrlHelpers
- Defined in:
- lib/skroutz/collection_proxy.rb
Direct Known Subclasses
AddressesCollection, AutocompleteCollection, CategoriesCollection, FavoriteListsCollection, FavoritesCollection, FilterGroupsCollection, LocationsCollection, ManufacturersCollection, NotificationsCollection, ProductsCollection, ReviewsCollection, ShopsCollection, SkusCollection, SpecificationsCollection
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#id ⇒ Object
Returns the value of attribute id.
-
#owner ⇒ Object
Returns the value of attribute owner.
Instance Method Summary collapse
- #all(options = {}) {|response| ... } ⇒ Object
- #find(id, options = {}) {|response| ... } ⇒ Object
-
#initialize(id, client, owner = nil, options = {}) ⇒ CollectionProxy
constructor
A new instance of CollectionProxy.
- #model_name ⇒ Object
- #page(pagenum = 1, options = {}) {|response| ... } ⇒ Object
- #resource ⇒ Object
- #resource_prefix ⇒ Object
Methods included from UrlHelpers
Methods included from Parsing
#collection?, #collection_resource_key, #infer_collection, #infer_model, #link_header, #parse, #parse_collection, #parse_meta, #parse_resource, #resource_key
Constructor Details
#initialize(id, client, owner = nil, options = {}) ⇒ CollectionProxy
Returns a new instance of CollectionProxy.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/skroutz/collection_proxy.rb', line 7 def initialize(id, client, owner = nil, = {}) if self.class == Skroutz::CollectionProxy raise RuntimeError.new('Attempted to initialize an abstract class') end @id = id @client = client @owner = owner @prefix = [:prefix] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) {|response| ... } ⇒ Object (private)
rubocop:disable Metrics/CyclomaticComplexity
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/skroutz/collection_proxy.rb', line 57 def method_missing(method, *args) # rubocop:disable Metrics/CyclomaticComplexity = args.first || {} url_prefix = .delete(:url_prefix) || '' verb = .delete(:verb) || .delete(:via) || :get target_url = "#{base_path}/#{id}/#{method}" target_url.prepend("#{url_prefix}/") if url_prefix response = client.send(verb, target_url, ) return parse(response) unless block_given? yield response end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/skroutz/collection_proxy.rb', line 5 def client @client end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/skroutz/collection_proxy.rb', line 5 def id @id end |
#owner ⇒ Object
Returns the value of attribute owner.
5 6 7 |
# File 'lib/skroutz/collection_proxy.rb', line 5 def owner @owner end |
Instance Method Details
#all(options = {}) {|response| ... } ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/skroutz/collection_proxy.rb', line 35 def all( = {}) response = client.get(base_path, ) return parse(response) unless block_given? yield response end |
#find(id, options = {}) {|response| ... } ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/skroutz/collection_proxy.rb', line 18 def find(id, = {}) response = client.get("#{base_path}/#{id}", ) return parse(response) unless block_given? yield response end |
#model_name ⇒ Object
51 52 53 |
# File 'lib/skroutz/collection_proxy.rb', line 51 def model_name @model_name ||= "Skroutz::#{resource.classify}".constantize end |
#page(pagenum = 1, options = {}) {|response| ... } ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/skroutz/collection_proxy.rb', line 26 def page(pagenum = 1, = {}) per = [:per] || client.config[:pagination_page_size] response = client.get(base_path, { page: pagenum, per: per }.merge()) return parse(response) unless block_given? yield response end |
#resource ⇒ Object
43 44 45 |
# File 'lib/skroutz/collection_proxy.rb', line 43 def resource @resource ||= self.class.to_s.demodulize.chomp('Collection').tableize.singularize end |
#resource_prefix ⇒ Object
47 48 49 |
# File 'lib/skroutz/collection_proxy.rb', line 47 def resource_prefix @resource_prefix ||= @prefix || resource.pluralize end |