pipe_drive_ruby_wrapper 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a0306cb9643370e9c2bdc925d0b0b9d0457d0a397e918f64d007e33d96db583
4
- data.tar.gz: 995f2307d138885030d5f0b2bfaacfc2c69680cd4817a4e34cef60ffd0a36364
3
+ metadata.gz: af042bbd75c486a6402e783b422e6a7bc8f6144c2efb7bd2c050c149e59290e0
4
+ data.tar.gz: f0eca92a11ca625dc15005ab56fb2f22b8c0020bfff88cee99c7abcfc0ef8262
5
5
  SHA512:
6
- metadata.gz: 8f13c5c3cf4ca52b0d8d2418d1d2e7cf2770ab203679fb1efcf8153b9d0b1c59c4c34d587af15557942c37c92cc5972bc18c4484f95390897d6cf4309ba7ca34
7
- data.tar.gz: beec160b8ab90d67935ec626cef412e28a0d06a44ab7081b21a6e56fe0e29d3b404f39d5127fd340b43765f687201ae15fc99bd140bf8a2c7b29b74d87812d72
6
+ metadata.gz: 81c4ef35bd3ade27bb52d0bf58bad303f5104628ed2bd261a1d39cf50045e904dcf643f42441b4dd560203d08954f7d9b155a4f18b36a5601ee1b4d52439d49d
7
+ data.tar.gz: dd2de0dbf8c30ae95d58ab6897fb22884029f5eea173f73246438e14ab5a91382f35027fdb5c4026ad4743144f36093440680ab2d2dae401a0d108d445d47af4
@@ -2,5 +2,13 @@ module PipeDrive
2
2
  class Deal < ResourceBase
3
3
  ALLOW_FOR_SEARCH_TERMS = %i[title]
4
4
  ALLOW_FOR_ADDITION_SEARCH_OPTS = %i[org_id person_id]
5
+
6
+ def organization
7
+ Organization.find_by_id(org_id)
8
+ end
9
+
10
+ def person
11
+ Person.find_by_id(person_id)
12
+ end
5
13
  end
6
14
  end
@@ -1,9 +1,10 @@
1
1
  module PipeDrive
2
2
  class FieldBase < Base
3
- def add_to_field_keys
3
+ def add_to_field_infos
4
4
  resource = self.class.correspond_resource.to_sym
5
- field_name = parameterize(name, '_').to_sym
6
- PipeDrive.field_keys[resource][field_name] = {id: id, key: key}
5
+ field_name = parameterize(name, '_')
6
+ PipeDrive.field_keys[resource][field_name.to_sym] = {id: id, key: key}
7
+ PipeDrive.field_names[resource][key] = {id: id, name: field_name}
7
8
  end
8
9
 
9
10
  class << self
@@ -33,36 +34,54 @@ module PipeDrive
33
34
 
34
35
  def create(opts)
35
36
  new_field = super(opts)
36
- new_field.add_to_field_keys
37
+ new_field.add_to_field_infos
37
38
  end
38
39
 
39
- def field_keys_map
40
+ def field_infos_map
40
41
  field_keys_map = Hash.new{|hash, key| hash[key] = {}}
42
+ field_names_map = Hash.new{|hash, key| hash[key] = {}}
41
43
  resource = correspond_resource.to_sym
42
44
  list.each do |field|
43
- field_name = parameterize(field.name, '_').to_sym
44
- field_keys_map[resource][field_name] = {id: field.id, key: field.key}
45
+ field_name = parameterize(field.name, '_')
46
+ field_keys_map[resource][field_name.to_sym] = {id: field.id, key: field.key}
47
+ field_names_map[resource][field.key] = {id: field.id, name: field_name}
45
48
  end
46
- field_keys_map
49
+ {key_map: field_keys_map, name_map: field_names_map}
47
50
  end
48
51
 
49
52
  def cache_keys
50
53
  PipeDrive.field_keys[correspond_resource.to_sym]
51
54
  end
52
55
 
56
+ def cache_names
57
+ PipeDrive.field_names[correspond_resource.to_sym]
58
+ end
59
+
53
60
  def pipedrive_key_of(field_name)
54
61
  cache_field_name = field_name.is_a?(String) ? parameterize(field_name, '_').to_sym : field_name
55
62
  pipedrive_key = cache_keys[cache_field_name]
56
63
  return pipedrive_key[:key] unless pipedrive_key.nil?
57
- PipeDrive.reset_field_keys!
64
+ PipeDrive.reset_field_infos!
58
65
  pipedrive_key = cache_keys[cache_field_name]
59
66
  if pipedrive_key.nil?
60
- raise TargetNotFound.new(self.name, :name, field_name)
67
+ raise TargetNotFound.new(self.name, :field_name, field_name)
61
68
  else
62
69
  pipedrive_key[:key]
63
70
  end
64
71
  end
65
72
 
73
+ def pipedrive_name_of(field_key)
74
+ pipedrive_name = cache_names[field_key]
75
+ return pipedrive_name[:name] unless pipedrive_name.nil?
76
+ PipeDrive.reset_field_infos!
77
+ pipedrive_name = cache_names[field_key]
78
+ if pipedrive_name.nil?
79
+ raise TargetNotFound.new(self.name, :field_key, field_key)
80
+ else
81
+ pipedrive_name[:name]
82
+ end
83
+ end
84
+
66
85
  end
67
86
  end
68
87
  end
@@ -1,6 +1,19 @@
1
1
  module PipeDrive
2
2
  class ResourceBase < Base
3
3
 
4
+ def initialize(attrs)
5
+ data = attrs[:data] || PipeDrive.hash_except(attrs, [:additional_data])
6
+ processed_data = {}
7
+ data.each_pair do |key, value|
8
+ field_name_map = self.class.field_names[key.to_s]
9
+ field_name = field_name_map.nil? ? key : field_name_map[:name]
10
+ processed_data[field_name] = value
11
+ end
12
+ attrs[:data] = processed_data
13
+
14
+ super(attrs)
15
+ end
16
+
4
17
  protected
5
18
 
6
19
  def pagination(path, params, &block)
@@ -17,6 +30,10 @@ module PipeDrive
17
30
  PipeDrive.field_keys[resource_name.to_sym]
18
31
  end
19
32
 
33
+ def field_names
34
+ PipeDrive.field_names[resource_name.to_sym]
35
+ end
36
+
20
37
  def resource_class
21
38
  self
22
39
  end
@@ -11,7 +11,7 @@ module PipeDrive
11
11
  FIELD_CLASSES = %w[OrganizationField PersonField DealField]
12
12
 
13
13
  class << self; attr_accessor :api_token; end
14
- class << self; attr_writer :field_keys, :stage_ids; end
14
+ class << self; attr_writer :field_keys, :field_names, :stage_ids; end
15
15
 
16
16
  class << self
17
17
  def setup
@@ -19,21 +19,21 @@ module PipeDrive
19
19
  self
20
20
  end
21
21
 
22
+ def field_infos
23
+ return @field_infos unless @field_infos.nil? || @field_infos.empty?
24
+ @field_infos = obtain_field_infos
25
+ end
26
+
27
+ def reset_field_infos!
28
+ @field_infos = obtain_field_infos
29
+ end
30
+
22
31
  def field_keys
23
- return @field_keys unless @field_keys.nil? || @field_keys.empty?
24
- @field_keys = {}
25
- FIELD_CLASSES.each do |class_name|
26
- @field_keys.merge!(const_get(class_name).field_keys_map)
27
- end
28
- @field_keys
32
+ @fields_keys = field_infos[:key_map]
29
33
  end
30
34
 
31
- def reset_field_keys!
32
- @field_keys = {}
33
- FIELD_CLASSES.each do |class_name|
34
- @field_keys.merge!(const_get(class_name).field_keys_map)
35
- end
36
- @field_keys
35
+ def field_names
36
+ @field_names = field_infos[:name_map]
37
37
  end
38
38
 
39
39
  def stage_ids
@@ -58,6 +58,18 @@ module PipeDrive
58
58
  remain_keys = all_keys - except_keys
59
59
  hash.slice(*remain_keys)
60
60
  end
61
+
62
+ private
63
+
64
+ def obtain_field_infos
65
+ field_infos = {key_map: {}, name_map: {}}
66
+ FIELD_CLASSES.each do |class_name|
67
+ infos = const_get(class_name).field_infos_map
68
+ field_infos[:key_map].merge!(infos[:key_map])
69
+ field_infos[:name_map].merge!(infos[:name_map])
70
+ end
71
+ field_infos
72
+ end
61
73
  end
62
74
 
63
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipe_drive_ruby_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - JiaRou Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-19 00:00:00.000000000 Z
11
+ date: 2018-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec