class PipeRocket::Service

Constants

HOST
RESOURCES_WITH_CUSTOM_FIELDS

Public Class Methods

new(resource_name) click to toggle source
# 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

Public Instance Methods

all() click to toggle source

Getting all @resource_name object from Pipedrive

# 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) click to toggle source

Build resource_name class object from hash

# 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) click to toggle source

Build uri for request

# 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) click to toggle source

Create @resource_name in Pipedrive

# 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) click to toggle source

Find @resource_name object by id

# 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() click to toggle source

Getting first @resource_name object

# File lib/pipe_rocket/service.rb, line 88
def first
  self.all.first
end
update(id, params) click to toggle source

Update @resource_name object by id

# 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

Protected Instance Methods

clear_key(key) click to toggle source

Clear string from forbidden symbols for ruby variables

# File lib/pipe_rocket/service.rb, line 135
def clear_key(key)
  key = key.underscore.gsub(' ','_')
  key = key.gsub('%','percent').gsub(/[^a-zA-Z0-9_]/,'')
end
transform_custom_fields(params) click to toggle source

Trasform custom fields from their names to keys

# File lib/pipe_rocket/service.rb, line 110
def transform_custom_fields(params)
  return params unless @has_custom_fields

  @@name_key_hash = Pipedrive.send("#{@resource_name}_fields").name_key_hash
  @@name_key_hash.transform_keys!{|key|clear_key(key)}

  hash = ::CUSTOM_FIELD_NAMES
  if hash && hash[@resource_name.to_sym]
    @@name_key_hash = @@name_key_hash.merge(hash[@resource_name.to_sym].invert)
  end

  keys = @@name_key_hash.keys
  res = {}

  params.each do |name, value|
    if keys.include?(name.to_s) #Custom Field
      res[@@name_key_hash[name.to_s]] = value
    else
      res[name] = value
    end
  end
  res
end