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.
20 21 22 23 |
# File 'lib/pipe_rocket/service.rb', line 20 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pipe_rocket/service.rb', line 40 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
26 27 28 29 |
# File 'lib/pipe_rocket/service.rb', line 26 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
32 33 34 35 36 37 |
# File 'lib/pipe_rocket/service.rb', line 32 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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pipe_rocket/service.rb', line 56 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pipe_rocket/service.rb', line 71 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
87 88 89 |
# File 'lib/pipe_rocket/service.rb', line 87 def first self.all.first end |
#update(id, params) ⇒ Object
Update @resource_name object by id
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pipe_rocket/service.rb', line 92 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 |