Class: PipedriveJetrockets::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/pipedrive_jetrockets/service.rb

Direct Known Subclasses

FieldService, PersonService

Constant Summary collapse

HOST =
'https://api.pipedrive.com/v1'
RESOURCES_WITH_CUSTOM_FIELDS =
%w(deal organization person)

Instance Method Summary collapse

Constructor Details

#initialize(resource_name) ⇒ Service

Returns a new instance of Service.



19
20
21
22
# File 'lib/pipedrive_jetrockets/service.rb', line 19

def initialize(resource_name)
  @resource_name = resource_name
  @has_custom_fields = RESOURCES_WITH_CUSTOM_FIELDS.include?(@resource_name)
end

Instance Method Details

#allObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pipedrive_jetrockets/service.rb', line 35

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 PipedriveJetrockets::Error.new(response.code)
  end
rescue HTTP::ConnectionError
  raise PipedriveJetrockets::Error.new(408)
end

#build_entity(raw) ⇒ Object



24
25
26
# File 'lib/pipedrive_jetrockets/service.rb', line 24

def build_entity(raw)
  "PipedriveJetrockets::#{@resource_name.titleize.delete(' ')}".constantize.new(raw)
end

#build_uri(params = {}, specificator = nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/pipedrive_jetrockets/service.rb', line 28

def build_uri(params = {}, specificator = 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}?#{query_string}")
end

#create(params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pipedrive_jetrockets/service.rb', line 50

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 PipedriveJetrockets::Error.new(response.code)
  end
rescue HTTP::ConnectionError
  raise PipedriveJetrockets::Error.new(408)
end

#find(id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pipedrive_jetrockets/service.rb', line 64

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 PipedriveJetrockets::Error.new(response.code)
  end
rescue HTTP::ConnectionError
  raise PipedriveJetrockets::Error.new(408)
end

#firstObject



79
80
81
# File 'lib/pipedrive_jetrockets/service.rb', line 79

def first
  self.all.first
end

#update(id, params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pipedrive_jetrockets/service.rb', line 83

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 PipedriveJetrockets::Error.new(response.code)
  end
rescue HTTP::ConnectionError
  raise PipedriveJetrockets::Error.new(408)
end