Class: Makeleaps::Request::Generic

Inherits:
Base
  • Object
show all
Defined in:
lib/makeleaps/request/generic.rb

Constant Summary

Constants inherited from Base

Base::ENDPOINT

Instance Attribute Summary collapse

Attributes inherited from Base

#connection

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Generic

Returns a new instance of Generic.



10
11
12
13
14
15
16
# File 'lib/makeleaps/request/generic.rb', line 10

def initialize(access_token)
  super() do |conn|
    conn.authorization :Bearer, access_token
  end
  @handler = Makeleaps::Request::RequestHandler.new(connection)
  @url_manager = Makeleaps::Request::URLManager.new
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



8
9
10
# File 'lib/makeleaps/request/generic.rb', line 8

def handler
  @handler
end

#url_managerObject (readonly)

Returns the value of attribute url_manager.



8
9
10
# File 'lib/makeleaps/request/generic.rb', line 8

def url_manager
  @url_manager
end

Instance Method Details

#delete(resource_or_url, mid = nil, &block) ⇒ Object



81
82
83
# File 'lib/makeleaps/request/generic.rb', line 81

def delete(resource_or_url, mid=nil, &block)
  handler.delete url_manager.build_url_for(resource_or_url, mid), &block
end

#each_page(start_page, *args, params: nil, max_pages: nil, &block) ⇒ Object

set max_pages to change the maximum number of retrievable pages WARNING: this might invoke too many API accesses.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/makeleaps/request/generic.rb', line 32

def each_page(start_page, *args, params: nil, max_pages: nil, &block)
  next_url = start_page
  max_pages = max_pages || 100 # TODO: let default value be customizeable using config

  max_pages.times do
    response = get(next_url, *args) { |req|
      req.params.merge!(params) unless params.nil?
    }
    block.call(response)
    next_url = response.next
    break unless next_url
    sleep 0.1 # TODO: throttling parameter can be adjusted individually
  end
end

#each_resource(start_page, *args, params: nil, max_pages: nil, &block) ⇒ Object

set max_pages to change the maximum number of retrievable pages WARNING: this might invoke too many API accesses.



49
50
51
52
53
54
55
# File 'lib/makeleaps/request/generic.rb', line 49

def each_resource(start_page, *args, params: nil, max_pages: nil, &block)
  each_page(start_page, *args, params: params, max_pages: max_pages) do |page|
    page.each_resource do |resource|
      block.call(resource)
    end
  end
end

#find_partner_by(name:) ⇒ Object



25
26
27
28
# File 'lib/makeleaps/request/generic.rb', line 25

def find_partner_by(name: )
  partners = get(:partner)
  partners.find_resource { |partner| partner['name'] == name }
end

#find_resource(start_page, *args, params: nil, max_pages: nil, &block) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/makeleaps/request/generic.rb', line 57

def find_resource(start_page, *args, params: nil, max_pages: nil, &block)
  each_page(start_page, params: params, max_pages: max_pages) do |page|
    resource = page.find_resource(*args, &block)
    return resource if resource
  end
  nil
end

#get(resource_or_url, mid = nil, &block) ⇒ Object



65
66
67
# File 'lib/makeleaps/request/generic.rb', line 65

def get(resource_or_url, mid=nil, &block)
  handler.get url_manager.build_url_for(resource_or_url, mid), &block
end

#options(resource_or_url, mid = nil, &block) ⇒ Object



85
86
87
# File 'lib/makeleaps/request/generic.rb', line 85

def options(resource_or_url, mid=nil, &block)
  handler.options url_manager.build_url_for(resource_or_url, mid), &block
end

#patch(resource_or_url, mid = nil, &block) ⇒ Object



77
78
79
# File 'lib/makeleaps/request/generic.rb', line 77

def patch(resource_or_url, mid=nil, &block)
  handler.patch url_manager.build_url_for(resource_or_url, mid), &block
end

#post(resource_or_url, mid = nil, &block) ⇒ Object



69
70
71
# File 'lib/makeleaps/request/generic.rb', line 69

def post(resource_or_url, mid=nil, &block)
  handler.post url_manager.build_url_for(resource_or_url, mid), &block
end

#put(resource_or_url, mid = nil, &block) ⇒ Object



73
74
75
# File 'lib/makeleaps/request/generic.rb', line 73

def put(resource_or_url, mid=nil, &block)
  handler.put url_manager.build_url_for(resource_or_url, mid), &block
end

#set_partner!(name:) ⇒ Object



18
19
20
21
22
23
# File 'lib/makeleaps/request/generic.rb', line 18

def set_partner!(name: )
  url = find_partner_by(name: name)['url']
  partner_mid = url.split('/').last
  url_manager.set_partner!(partner_mid)
  self
end