Class: PipedriveRuby::HttpFactory

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HTTP::Chainable
Defined in:
lib/http_factory.rb

Overview

responsable for hold all commum endpoints shared between n resources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, client) ⇒ HttpFactory

Returns a new instance of HttpFactory



9
10
11
12
13
14
# File 'lib/http_factory.rb', line 9

def initialize(resource_name, client)
  @client = client
  @class_name = resource_name
  @default_param = { api_token: @client.api_token }
  @base_url = make_base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url



7
8
9
# File 'lib/http_factory.rb', line 7

def base_url
  @base_url
end

#class_nameObject (readonly)

Returns the value of attribute class_name



7
8
9
# File 'lib/http_factory.rb', line 7

def class_name
  @class_name
end

#clientObject (readonly)

Returns the value of attribute client



7
8
9
# File 'lib/http_factory.rb', line 7

def client
  @client
end

#default_paramObject (readonly)

Returns the value of attribute default_param



7
8
9
# File 'lib/http_factory.rb', line 7

def default_param
  @default_param
end

Instance Method Details

#add_follower(resource, user) ⇒ Object



16
17
18
19
# File 'lib/http_factory.rb', line 16

def add_follower(resource, user)
  custom_post(:path => "#{resource['id']}/followers",
              :json => {:id => resource['id'], :user_id => user['id'] })
end

#all(params = {}) ⇒ Object



21
22
23
24
# File 'lib/http_factory.rb', line 21

def all(params={})
  params.merge!(default_param)
  custom_get(:params => params)
end

#create(resource) ⇒ Object



26
27
28
# File 'lib/http_factory.rb', line 26

def create(resource)
  custom_post(:params => default_param, :json => resource)
end

#custom_delete(options = {}) ⇒ Object



45
46
47
48
# File 'lib/http_factory.rb', line 45

def custom_delete(options={})
  options.merge!({:method => :delete})
  custom_request(options)
end

#custom_get(options = {}) ⇒ Object



30
31
32
33
# File 'lib/http_factory.rb', line 30

def custom_get(options={})
  options.merge!({:method => :get})
  custom_request(options)
end

#custom_post(options = {}) ⇒ Object



35
36
37
38
# File 'lib/http_factory.rb', line 35

def custom_post(options={})
  options.merge!({:method => :post})
  custom_request(options)
end

#custom_put(options = {}) ⇒ Object



40
41
42
43
# File 'lib/http_factory.rb', line 40

def custom_put(options={})
  options.merge!({:method => :put})
  custom_request(options)
end

#delete_follower(resource, follower) ⇒ Object



50
51
52
53
# File 'lib/http_factory.rb', line 50

def delete_follower(resource, follower)
  custom_delete(:path => "#{resource['id']}/followers/#{follower['id']}",
              :json => {:id => resource['id'], :user_id => follower['id'] })
end

#duplicate(resource) ⇒ Object



55
56
57
# File 'lib/http_factory.rb', line 55

def duplicate(resource)
  custom_post(:path => "#{resource['id']}/duplicate", :params => default_param)
end

#files(resource) ⇒ Object



67
68
69
# File 'lib/http_factory.rb', line 67

def files(resource)
  custom_get(:path => "#{resource['id']}/files")
end

#find(id) ⇒ Object



59
60
61
# File 'lib/http_factory.rb', line 59

def find(id)
  custom_get(:path => "#{id}/")
end

#find_by(term) ⇒ Object



63
64
65
# File 'lib/http_factory.rb', line 63

def find_by(term)
  custom_get(:path => "find/", :params => {:term => term})
end

#followers(resource) ⇒ Object



71
72
73
# File 'lib/http_factory.rb', line 71

def followers(resource)
  custom_get(:path => "#{resource['id']}/followers")
end

#merge(resource, merge_with_resource) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/http_factory.rb', line 75

def merge(resource, merge_with_resource)
  id = resource['id']
  custom_post(:path => "#{id}/merge",
              :params => default_param,
              :json => {
                          :id => id,
                          :merge_with_id => merge_with_resource['id']
                        }
                )
end

#permitted_users(resource) ⇒ Object



86
87
88
# File 'lib/http_factory.rb', line 86

def permitted_users(resource)
  custom_get(:path => "#{resource['id']}/permittedUsers")
end

#remove(resource) ⇒ Object



90
91
92
# File 'lib/http_factory.rb', line 90

def remove(resource)
  custom_delete(:path => "#{resource['id']}", :params => default_param)
end

#remove_many(resources) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/http_factory.rb', line 94

def remove_many(resources)
  ids = []
  resources.each do |resource|
    ids << resource['data']['id']
  end
  custom_delete(:params => default_param, :json => { ids: ids.join(',') })
end

#update(resource) ⇒ Object



102
103
104
# File 'lib/http_factory.rb', line 102

def update(resource)
  custom_put(:path => "#{resource['id']}", :params => default_param, :json => resource)
end