class PipedriveJetrockets::Service

Constants

HOST
RESOURCES_WITH_CUSTOM_FIELDS

Public Class Methods

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

Public Instance Methods

all() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
first() click to toggle source
# File lib/pipedrive_jetrockets/service.rb, line 79
def first
  self.all.first
end
update(id, params) click to toggle source
# 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

Protected Instance Methods

clear_key(key) click to toggle source
# File lib/pipedrive_jetrockets/service.rb, line 130
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
# File lib/pipedrive_jetrockets/service.rb, line 99
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
transform_field_name(key, name) click to toggle source
# File lib/pipedrive_jetrockets/service.rb, line 123
def transform_field_name(key, name)
  hash = ::CUSTOM_FIELD_NAMES
  class_name = @resource_name
  return name if hash.nil? || hash[class_name].nil? || hash[class_name].key(key.to_sym).nil?
  hash[class_name][key.to_sym]
end