Class: Pipedrive::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pipedrive/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/pipedrive/base.rb', line 6

def initialize(options = {})
  @options = options
  authenticate
end

Instance Method Details

#[](id) ⇒ Object



101
102
103
104
# File 'lib/pipedrive/base.rb', line 101

def [](id)
  path = [resource_path, id.to_s].join '/'
  get(resource_path: path).first
end

#all(options = @options) ⇒ Object



61
62
63
64
65
# File 'lib/pipedrive/base.rb', line 61

def all(options = @options)
  data = []
  each(options).collect {|i| data << i}
  data
end

#authenticate(token = @options[:api_token]) ⇒ Object



11
12
13
# File 'lib/pipedrive/base.rb', line 11

def authenticate(token = @options[:api_token])
  default_params.merge! api_token: token
end

#base_uriObject



81
82
83
# File 'lib/pipedrive/base.rb', line 81

def base_uri
  protocol + 'api.pipedrive.com/v1'
end

#get(options = @options) ⇒ Object Also known as: each



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pipedrive/base.rb', line 15

def get(options = @options)
  return to_enum(__callee__, options) unless block_given?

  response = _get_resource(options)
  if response.success?
    data = [(response.body['data'] || [])].flatten
    data.each do |item|
      yield OpenStruct.new item
    end

    if next_start(response)
      options[:params] = (options[:params]||{}).merge({start: next_start(response)})
      send(__callee__, options) do |data|
        yield data
      end
    end
  else
    error_class = case response.status
                  when 401 then AuthenticationError
                  else
                    ServiceError
                  end
    fail error_class, JSON.parse(response.body)['error']
  end

end

#metric_key(item) ⇒ Object



57
58
59
# File 'lib/pipedrive/base.rb', line 57

def metric_key(item)
  (item.try(:type) || item.try(:status)).try(:to_sym)
end

#metricsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pipedrive/base.rb', line 43

def metrics
  metrics = {total: all.count}

  get.each do |item|
    key = metric_key(item)
    break if key.nil?

    metrics[key] ||= 0
    metrics[key] += 1
  end

  metrics
end

#prepare_options(options = @options) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/pipedrive/base.rb', line 67

def prepare_options(options = @options)
  if options.has_key?(:sort_by)
    key = options.delete(:sort_by)
    dir = options.delete(:sort_mode) || :desc
    options[:params] = (options[:params]||{}).merge({sort_by: key, sort_mode: dir})
  end

  options
end

#protocolObject



77
78
79
# File 'lib/pipedrive/base.rb', line 77

def protocol
  'http://'
end

#resource(klass_name, options = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/pipedrive/base.rb', line 93

def resource(klass_name, options = {})
  klass_name = klass_name.to_s.split('_').map(&:capitalize).join
  _klasses[klass_name] ||= begin
    klass = Object.const_get "::Pipedrive::#{klass_name}"
    klass.new @options.merge(options)
  end
end

#resource_pathObject



85
86
87
88
89
90
91
# File 'lib/pipedrive/base.rb', line 85

def resource_path
  # The resource path should match the camelCased class name with the
  # first letter downcased.  Pipedrive API is sensitive to capitalisation
  klass = self.class.name.split('::').last
  klass[0] = klass[0].chr.downcase
  klass
end