Class: PipeRocket::Service
- Inherits:
-
Object
- Object
- PipeRocket::Service
- Defined in:
- lib/pipe_rocket/service.rb
Direct Known Subclasses
Constant Summary collapse
- HOST =
'https://api.pipedrive.com/v1'
- RESOURCES_WITH_CUSTOM_FIELDS =
%w(deal organization person)
Instance Method Summary collapse
-
#all ⇒ Object
Getting all @resource_name object from Pipedrive.
-
#build_entity(raw, resource_name = nil) ⇒ Object
Build resource_name class object from hash.
-
#build_uri(params = {}, specificator = nil, action = nil) ⇒ Object
Build uri for request.
-
#create(params) ⇒ Object
Create @resource_name in Pipedrive.
-
#find(id) ⇒ Object
Find @resource_name object by id.
-
#first ⇒ Object
Getting first @resource_name object.
-
#initialize(resource_name) ⇒ Service
constructor
A new instance of Service.
-
#update(id, params) ⇒ Object
Update @resource_name object by id.
Constructor Details
#initialize(resource_name) ⇒ Service
Returns a new instance of Service.
21 22 23 24 |
# File 'lib/pipe_rocket/service.rb', line 21 def initialize(resource_name) @resource_name = resource_name @has_custom_fields = RESOURCES_WITH_CUSTOM_FIELDS.include?(@resource_name) end |
Instance Method Details
#all ⇒ Object
Getting all @resource_name object from Pipedrive
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pipe_rocket/service.rb', line 41 def all uri = build_uri response = HTTP.get(uri) case response.code when 200 json_array = ::JSON.parse(response)['data'] json_array.map{|raw|build_entity(raw)} else raise PipeRocket::Error.new(response.code) end rescue HTTP::ConnectionError raise PipeRocket::Error.new(408) end |
#build_entity(raw, resource_name = nil) ⇒ Object
Build resource_name class object from hash
27 28 29 30 |
# File 'lib/pipe_rocket/service.rb', line 27 def build_entity(raw, resource_name = nil) resource_name ||= @resource_name "PipeRocket::#{resource_name.titleize.delete(' ')}".constantize.new(raw) end |
#build_uri(params = {}, specificator = nil, action = nil) ⇒ Object
Build uri for request
33 34 35 36 37 38 |
# File 'lib/pipe_rocket/service.rb', line 33 def build_uri(params = {}, specificator = nil, action = nil) params.merge!(api_token: ENV['pipedrive_api_token']) query_string = params.map{|k,v|"#{k}=#{v}"}.join('&') plural_resource_name = @resource_name == 'person' ? 'persons' : @resource_name.pluralize uri = URI("#{HOST}/#{[plural_resource_name, specificator, action].compact.join('/')}?#{query_string}") end |
#create(params) ⇒ Object
Create @resource_name in Pipedrive
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pipe_rocket/service.rb', line 57 def create(params) uri = build_uri response = HTTP.post(uri, form: transform_custom_fields(params)) case response.code when 201 build_entity(JSON.parse(response.body)['data']) else raise PipeRocket::Error.new(response.code) end rescue HTTP::ConnectionError raise PipeRocket::Error.new(408) end |
#find(id) ⇒ Object
Find @resource_name object by id
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pipe_rocket/service.rb', line 72 def find(id) uri = build_uri({}, id) response = HTTP.get(uri) case response.code when 200 raw = ::JSON.parse(response)['data'] build_entity(raw) else raise PipeRocket::Error.new(response.code) end rescue HTTP::ConnectionError raise PipeRocket::Error.new(408) end |
#first ⇒ Object
Getting first @resource_name object
88 89 90 |
# File 'lib/pipe_rocket/service.rb', line 88 def first self.all.first end |
#update(id, params) ⇒ Object
Update @resource_name object by id
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pipe_rocket/service.rb', line 93 def update(id, params) uri = build_uri({}, id) response = HTTP.put(uri, form: transform_custom_fields(params)) case response.code when 200 build_entity(JSON.parse(response.body)['data']) else raise PipeRocket::Error.new(response.code) end rescue HTTP::ConnectionError raise PipeRocket::Error.new(408) end |