Class: Skroutz::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Parsing, UrlHelpers
Defined in:
lib/skroutz/collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlHelpers

#base_path

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, options = {})
  if self.class == Skroutz::CollectionProxy
    raise RuntimeError.new('Attempted to initialize an abstract class')
  end

  @id = id
  @client = client
  @owner = owner
  @prefix = options[: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

Yields:

  • (response)


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
  options = args.first || {}
  url_prefix = options.delete(:url_prefix) || ''
  verb = options.delete(:verb) || options.delete(:via) || :get

  target_url = "#{base_path}/#{id}/#{method}"
  target_url.prepend("#{url_prefix}/") if url_prefix

  response = client.send(verb, target_url, options)

  return parse(response) unless block_given?

  yield response
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/skroutz/collection_proxy.rb', line 5

def client
  @client
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/skroutz/collection_proxy.rb', line 5

def id
  @id
end

#ownerObject

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

Yields:

  • (response)


35
36
37
38
39
40
41
# File 'lib/skroutz/collection_proxy.rb', line 35

def all(options = {})
  response = client.get(base_path, options)

  return parse(response) unless block_given?

  yield response
end

#find(id, options = {}) {|response| ... } ⇒ Object

Yields:

  • (response)


18
19
20
21
22
23
24
# File 'lib/skroutz/collection_proxy.rb', line 18

def find(id, options = {})
  response = client.get("#{base_path}/#{id}", options)

  return parse(response) unless block_given?

  yield response
end

#model_nameObject



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

Yields:

  • (response)


26
27
28
29
30
31
32
33
# File 'lib/skroutz/collection_proxy.rb', line 26

def page(pagenum = 1, options = {})
  per = options[:per] || client.config[:pagination_page_size]
  response = client.get(base_path, { page: pagenum, per: per }.merge(options))

  return parse(response) unless block_given?

  yield response
end

#resourceObject



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_prefixObject



47
48
49
# File 'lib/skroutz/collection_proxy.rb', line 47

def resource_prefix
  @resource_prefix ||= @prefix || resource.pluralize
end