forest_liana 1.3.28 → 1.3.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/forest_liana/intercom_controller.rb +8 -1
  3. data/app/controllers/forest_liana/stripe_controller.rb +33 -0
  4. data/app/models/forest_liana/model/collection.rb +1 -1
  5. data/app/serializers/forest_liana/collection_serializer.rb +3 -0
  6. data/app/serializers/forest_liana/intercom_attribute_serializer.rb +1 -1
  7. data/app/serializers/forest_liana/intercom_conversation_serializer.rb +1 -1
  8. data/app/serializers/forest_liana/serializer_factory.rb +44 -8
  9. data/app/serializers/forest_liana/stripe_bank_account_serializer.rb +43 -0
  10. data/app/serializers/forest_liana/stripe_card_serializer.rb +1 -1
  11. data/app/serializers/forest_liana/stripe_invoice_serializer.rb +1 -1
  12. data/app/serializers/forest_liana/stripe_payment_serializer.rb +1 -1
  13. data/app/serializers/forest_liana/stripe_subscription_serializer.rb +45 -0
  14. data/app/services/forest_liana/intercom_attributes_getter.rb +3 -11
  15. data/app/services/forest_liana/intercom_conversations_getter.rb +3 -10
  16. data/app/services/forest_liana/operator_value_parser.rb +1 -1
  17. data/app/services/forest_liana/schema_adapter.rb +61 -31
  18. data/app/services/forest_liana/stripe_bank_accounts_getter.rb +86 -0
  19. data/app/services/forest_liana/stripe_cards_getter.rb +26 -22
  20. data/app/services/forest_liana/stripe_invoices_getter.rb +28 -26
  21. data/app/services/forest_liana/stripe_payments_getter.rb +28 -25
  22. data/app/services/forest_liana/stripe_subscriptions_getter.rb +97 -0
  23. data/config/routes.rb +5 -2
  24. data/lib/forest_liana/bootstraper.rb +205 -30
  25. data/lib/forest_liana/version.rb +1 -1
  26. metadata +6 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a6c7f15a6e133527eec75f090625392109cb4c6
4
- data.tar.gz: 28303118fd5b82b5521729466fb29e99aea40e85
3
+ metadata.gz: 260b5a5f3205cc16a71e70e18ba1082f2e886d38
4
+ data.tar.gz: e3d7e787be1f394888c948d7db0c89c90866179d
5
5
  SHA512:
6
- metadata.gz: cc6e3b73720d9ea1656f65f69e893d5d0a67d53b184774a82aa03439c8c7f60292f77141d2e0551d99c7b279cd27853758710ee60609447816e344b9d4e17b9c
7
- data.tar.gz: 0d432fa88bf65db51ad5e2183f4d26714833c05c8cb0622a0d6310146b61647a90b4a0b2beb9f22a1685e670c0904c5e4109e70baf41c258b715e1e2b2202378
6
+ metadata.gz: f15bb5a2fe9c322dfa86639076a26d7cbe44ed33532e5ee8ae2c01ec282857619e9a3bf277067b52c51d25c02bb89204399d34cf0a51e0b69aef84780b90e48d
7
+ data.tar.gz: 7a98b3b22f891447a3f28357045478312621bf10662ced2d960f09cd4451109c93aa4aa8e4c01cf75f3e8d4ba7c25f6fed0aa0fa3fdd89a965ff74d4b880425d
@@ -5,6 +5,7 @@ module ForestLiana
5
5
  getter.perform
6
6
 
7
7
  render serializer: nil, json: serialize_models(getter.records, {
8
+ context: { type: get_serializer_type('intercom_conversations') },
8
9
  count: getter.count
9
10
  })
10
11
  end
@@ -13,7 +14,13 @@ module ForestLiana
13
14
  getter = IntercomAttributesGetter.new(params)
14
15
  getter.perform
15
16
 
16
- render json: serialize_model(getter.records), serializer: nil
17
+ render serializer: nil, json: serialize_model(getter.records, {
18
+ context: { type: get_serializer_type('intercom_attributes') }
19
+ })
20
+ end
21
+
22
+ def get_serializer_type(suffix)
23
+ "#{params[:collection]}_#{suffix}"
17
24
  end
18
25
  end
19
26
  end
@@ -8,6 +8,7 @@ module ForestLiana
8
8
  getter.perform
9
9
 
10
10
  render serializer: nil, json: serialize_models(getter.records, {
11
+ context: { type: get_serializer_type('stripe_payments') },
11
12
  count: getter.count,
12
13
  include: ['customer']
13
14
  })
@@ -31,6 +32,7 @@ module ForestLiana
31
32
  getter.perform
32
33
 
33
34
  render serializer: nil, json: serialize_models(getter.records, {
35
+ context: { type: get_serializer_type('stripe_cards') },
34
36
  count: getter.count,
35
37
  include: ['customer']
36
38
  })
@@ -43,9 +45,40 @@ module ForestLiana
43
45
  getter.perform
44
46
 
45
47
  render serializer: nil, json: serialize_models(getter.records, {
48
+ context: { type: get_serializer_type('stripe_invoices') },
46
49
  count: getter.count,
47
50
  include: ['customer']
48
51
  })
49
52
  end
53
+
54
+ def subscriptions
55
+ getter = StripeSubscriptionsGetter.new(params,
56
+ request.headers['Stripe-Secret-Key'],
57
+ request.headers['Stripe-Reference'])
58
+ getter.perform
59
+
60
+ render serializer: nil, json: serialize_models(getter.records, {
61
+ context: { type: get_serializer_type('stripe_subscriptions') },
62
+ count: getter.count,
63
+ include: ['customer']
64
+ })
65
+ end
66
+
67
+ def bank_accounts
68
+ getter = StripeBankAccountsGetter.new(params,
69
+ request.headers['Stripe-Secret-Key'],
70
+ request.headers['Stripe-Reference'])
71
+ getter.perform
72
+
73
+ render serializer: nil, json: serialize_models(getter.records, {
74
+ context: { type: get_serializer_type('stripe_bank_accounts') },
75
+ count: getter.count,
76
+ include: ['customer']
77
+ })
78
+ end
79
+
80
+ def get_serializer_type(suffix)
81
+ "#{params[:collection]}_#{suffix}"
82
+ end
50
83
  end
51
84
  end
@@ -5,7 +5,7 @@ class ForestLiana::Model::Collection
5
5
  extend ActiveModel::Naming
6
6
 
7
7
  attr_accessor :name, :fields, :actions, :only_for_relationships, :is_virtual,
8
- :is_read_only, :is_searchable
8
+ :is_read_only, :is_searchable, :display_name, :icon, :pagination_type
9
9
 
10
10
  def initialize(attributes = {})
11
11
  @actions = []
@@ -4,11 +4,14 @@ class ForestLiana::CollectionSerializer
4
4
  include JSONAPI::Serializer
5
5
 
6
6
  attribute :name
7
+ attribute :display_name
8
+ attribute :icon
7
9
  attribute :fields
8
10
  attribute :only_for_relationships
9
11
  attribute :is_virtual
10
12
  attribute :is_read_only
11
13
  attribute :is_searchable
14
+ attribute :pagination_type
12
15
 
13
16
  has_many :actions do
14
17
  object.actions
@@ -63,7 +63,7 @@ module ForestLiana
63
63
  end
64
64
 
65
65
  def type
66
- 'intercom-attributes'
66
+ @options[:context][:type] || 'intercom-attributes'
67
67
  end
68
68
 
69
69
  def format_name(attribute_name)
@@ -24,7 +24,7 @@ module ForestLiana
24
24
  end
25
25
 
26
26
  def type
27
- 'intercom-conversations'
27
+ @options[:context][:type] || 'intercom-conversations'
28
28
  end
29
29
 
30
30
  def format_name(attribute_name)
@@ -29,6 +29,12 @@ module ForestLiana
29
29
  elsif defined?(::Stripe::Invoice) &&
30
30
  active_record_class == ::Stripe::Invoice
31
31
  "ForestLiana::StripeInvoiceSerializer"
32
+ elsif defined?(::Stripe::Subscription) &&
33
+ active_record_class == ::Stripe::Subscription
34
+ "ForestLiana::StripeSubscriptionSerializer"
35
+ elsif defined?(::Stripe::BankAccount) &&
36
+ active_record_class == ::Stripe::BankAccount
37
+ "ForestLiana::StripeBankAccountSerializer"
32
38
  elsif active_record_class == ForestLiana::Model::Stat
33
39
  "ForestLiana::StatSerializer"
34
40
  elsif active_record_class == ForestLiana::Model::Collection
@@ -118,15 +124,25 @@ module ForestLiana
118
124
  private
119
125
 
120
126
  def intercom_integration?
121
- object.class.name == ForestLiana.integrations
127
+ ForestLiana.integrations
122
128
  .try(:[], :intercom)
123
- .try(:[], :user_collection)
129
+ .try(:[], :mapping)
130
+ .try(:include?, object.class.name)
124
131
  end
125
132
 
126
133
  def stripe_integration?
127
- object.class.name == ForestLiana.integrations
134
+ mapping = ForestLiana.integrations
128
135
  .try(:[], :stripe)
129
- .try(:[], :user_collection)
136
+ .try(:[], :mapping)
137
+
138
+ if mapping
139
+ collection_names = mapping.map do |collection_name_and_field|
140
+ collection_name_and_field.split('.')[0]
141
+ end
142
+ collection_names.include?(object.class.name)
143
+ else
144
+ false
145
+ end
130
146
  end
131
147
  }
132
148
 
@@ -169,15 +185,13 @@ module ForestLiana
169
185
  end
170
186
 
171
187
  # Intercom
172
- if active_record_class.name == ForestLiana.integrations
173
- .try(:[], :intercom).try(:[], :user_collection)
188
+ if has_intercom_integration?(active_record_class.name)
174
189
  serializer.send(:has_many, :intercom_conversations) { }
175
190
  serializer.send(:has_many, :intercom_attributes) { }
176
191
  end
177
192
 
178
193
  # Stripe
179
- if active_record_class.name == ForestLiana.integrations
180
- .try(:[], :stripe).try(:[], :user_collection)
194
+ if has_stripe_integration?(active_record_class.name)
181
195
  serializer.send(:has_many, :stripe_payments) { }
182
196
  serializer.send(:has_many, :stripe_invoices) { }
183
197
  serializer.send(:has_many, :stripe_cards) { }
@@ -195,6 +209,28 @@ module ForestLiana
195
209
  active_record_class.to_s.tableize.to_sym
196
210
  end
197
211
 
212
+ def has_intercom_integration?(collection_name)
213
+ ForestLiana.integrations
214
+ .try(:[], :intercom)
215
+ .try(:[], :mapping)
216
+ .try(:include?, collection_name)
217
+ end
218
+
219
+ def has_stripe_integration?(collection_name)
220
+ mapping = ForestLiana.integrations
221
+ .try(:[], :stripe)
222
+ .try(:[], :mapping)
223
+
224
+ if mapping
225
+ collection_names = mapping.map do |collection_name_and_field|
226
+ collection_name_and_field.split('.')[0]
227
+ end
228
+ collection_names.include?(collection_name)
229
+ else
230
+ false
231
+ end
232
+ end
233
+
198
234
  def serializer_association(association)
199
235
  case association.macro
200
236
  when :has_one, :belongs_to
@@ -0,0 +1,43 @@
1
+ module ForestLiana
2
+ class StripeCardSerializer
3
+ include JSONAPI::Serializer
4
+
5
+ attribute :account
6
+ attribute :account_holder_name
7
+ attribute :account_holder_type
8
+ attribute :bank_name
9
+ attribute :country
10
+ attribute :currency
11
+ attribute :default_for_currency
12
+ attribute :fingerprint
13
+ attribute :last4
14
+ attribute :rooting_number
15
+ attribute :status
16
+
17
+ has_one :customer
18
+
19
+ def self_link
20
+ "/forest#{super}"
21
+ end
22
+
23
+ def type
24
+ @options[:context][:type] || 'stripe_bank_accounts'
25
+ end
26
+
27
+ def format_name(attribute_name)
28
+ attribute_name.to_s
29
+ end
30
+
31
+ def unformat_name(attribute_name)
32
+ attribute_name.to_s
33
+ end
34
+
35
+ def relationship_self_link(attribute_name)
36
+ nil
37
+ end
38
+
39
+ def relationship_related_link(attribute_name)
40
+ nil
41
+ end
42
+ end
43
+ end
@@ -24,7 +24,7 @@ module ForestLiana
24
24
  end
25
25
 
26
26
  def type
27
- 'stripe_cards'
27
+ @options[:context][:type] || 'stripe_cards'
28
28
  end
29
29
 
30
30
  def format_name(attribute_name)
@@ -25,7 +25,7 @@ module ForestLiana
25
25
  end
26
26
 
27
27
  def type
28
- 'stripe_invoices'
28
+ @options[:context][:type] || 'stripe_invoices'
29
29
  end
30
30
 
31
31
  def format_name(attribute_name)
@@ -16,7 +16,7 @@ module ForestLiana
16
16
  end
17
17
 
18
18
  def type
19
- 'stripe_payments'
19
+ @options[:context][:type] || 'stripe_payments'
20
20
  end
21
21
 
22
22
  def format_name(attribute_name)
@@ -0,0 +1,45 @@
1
+ module ForestLiana
2
+ class StripeInvoiceSerializer
3
+ include JSONAPI::Serializer
4
+
5
+ attribute :cancel_at_period_end
6
+ attribute :canceled_at
7
+ attribute :created
8
+ attribute :current_period_end
9
+ attribute :current_period_start
10
+ attribute :ended_at
11
+ attribute :livemode
12
+ attribute :quantity
13
+ attribute :start
14
+ attribute :status
15
+ attribute :tax_percent
16
+ attribute :trial_end
17
+ attribute :trial_start
18
+
19
+ has_one :customer
20
+
21
+ def self_link
22
+ "/forest#{super}"
23
+ end
24
+
25
+ def type
26
+ @options[:context][:type] || 'stripe_subscriptions'
27
+ end
28
+
29
+ def format_name(attribute_name)
30
+ attribute_name.to_s
31
+ end
32
+
33
+ def unformat_name(attribute_name)
34
+ attribute_name.to_s
35
+ end
36
+
37
+ def relationship_self_link(attribute_name)
38
+ nil
39
+ end
40
+
41
+ def relationship_related_link(attribute_name)
42
+ nil
43
+ end
44
+ end
45
+ end
@@ -14,9 +14,8 @@ module ForestLiana
14
14
  end
15
15
 
16
16
  def perform
17
- resource = user_collection.find(@params[:id])
18
-
19
17
  begin
18
+ resource = collection.find(@params[:id])
20
19
  user = @intercom.users.find(email: resource.email)
21
20
 
22
21
  user.segments = user.segments.map do |segment|
@@ -30,15 +29,8 @@ module ForestLiana
30
29
 
31
30
  private
32
31
 
33
- def user_collection
34
- ForestLiana.integrations
35
- .try(:[], :intercom)
36
- .try(:[], :user_collection)
37
- .try(:constantize)
38
- end
39
-
40
- def user_collection?
41
- user_collection.present?
32
+ def collection
33
+ @params[:collection].singularize.capitalize.constantize
42
34
  end
43
35
  end
44
36
  end
@@ -27,7 +27,7 @@ module ForestLiana
27
27
 
28
28
  def perform
29
29
  begin
30
- resource = user_collection.find(@params[:id])
30
+ resource = collection.find(@params[:id])
31
31
  @records = @intercom.conversations.find_all(
32
32
  email: resource.email,
33
33
  type: 'user',
@@ -40,15 +40,8 @@ module ForestLiana
40
40
 
41
41
  private
42
42
 
43
- def user_collection
44
- ForestLiana.integrations
45
- .try(:[], :intercom)
46
- .try(:[], :user_collection)
47
- .try(:constantize)
48
- end
49
-
50
- def user_collection?
51
- user_collection.present?
43
+ def collection
44
+ @params[:collection].singularize.capitalize.constantize
52
45
  end
53
46
 
54
47
  def link(conversation)
@@ -14,7 +14,7 @@ module ForestLiana
14
14
  operator = '<'
15
15
  value.slice!(0)
16
16
  elsif value.include?('*')
17
- operator = 'ILIKE'
17
+ operator = 'LIKE'
18
18
  value.gsub!('*', '%')
19
19
  elsif value === '$present'
20
20
  operator = 'IS NOT NULL'
@@ -56,11 +56,14 @@ module ForestLiana
56
56
 
57
57
  # Intercom
58
58
  if ForestLiana.integrations.try(:[], :intercom)
59
- .try(:[], :user_collection) == @model.name
59
+ .try(:[], :mapping).try(:include?, @model.name)
60
+
61
+ model_name = @model.table_name
62
+
60
63
  collection.fields << {
61
64
  field: :intercom_conversations,
62
65
  type: ['String'],
63
- reference: 'intercom_conversations.id',
66
+ reference: "#{model_name}_intercom_conversations.id",
64
67
  column: nil,
65
68
  is_searchable: false,
66
69
  integration: 'intercom'
@@ -69,7 +72,7 @@ module ForestLiana
69
72
  @collection.fields << {
70
73
  field: :intercom_attributes,
71
74
  type: 'String',
72
- reference: 'intercom_attributes.id',
75
+ reference: "#{model_name}_intercom_attributes.id",
73
76
  column: nil,
74
77
  is_searchable: false,
75
78
  integration: 'intercom'
@@ -77,34 +80,61 @@ module ForestLiana
77
80
  end
78
81
 
79
82
  # Stripe
80
- if ForestLiana.integrations.try(:[], :stripe)
81
- .try(:[], :user_collection) == @model.name
82
- collection.fields << {
83
- field: :stripe_payments,
84
- type: ['String'],
85
- reference: 'stripe_payments.id',
86
- column: nil,
87
- is_searchable: false,
88
- integration: 'stripe'
89
- }
90
-
91
- collection.fields << {
92
- field: :stripe_invoices,
93
- type: ['String'],
94
- reference: 'stripe_invoices.id',
95
- column: nil,
96
- is_searchable: false,
97
- integration: 'stripe'
98
- }
99
-
100
- collection.fields << {
101
- field: :stripe_cards,
102
- type: ['String'],
103
- reference: 'stripe_cards.id',
104
- column: nil,
105
- is_searchable: false,
106
- integration: 'stripe'
107
- }
83
+ stripe_mapping = ForestLiana.integrations.try(:[], :stripe)
84
+ .try(:[], :mapping)
85
+
86
+ if stripe_mapping
87
+ if stripe_mapping
88
+ .select { |mapping| mapping.split('.')[0] == @model.name }
89
+ .size > 0
90
+
91
+ model_name = @model.table_name
92
+
93
+ collection.fields << {
94
+ field: :stripe_payments,
95
+ type: ['String'],
96
+ reference: "#{model_name}_stripe_payments.id",
97
+ column: nil,
98
+ is_searchable: false,
99
+ integration: 'stripe'
100
+ }
101
+
102
+ collection.fields << {
103
+ field: :stripe_invoices,
104
+ type: ['String'],
105
+ reference: "#{model_name}_stripe_invoices.id",
106
+ column: nil,
107
+ is_searchable: false,
108
+ integration: 'stripe'
109
+ }
110
+
111
+ collection.fields << {
112
+ field: :stripe_cards,
113
+ type: ['String'],
114
+ reference: "#{model_name}_stripe_cards.id",
115
+ column: nil,
116
+ is_searchable: false,
117
+ integration: 'stripe'
118
+ }
119
+
120
+ collection.fields << {
121
+ field: :stripe_subscriptions,
122
+ type: ['String'],
123
+ reference: "#{model_name}_stripe_subscriptions.id",
124
+ column: nil,
125
+ is_searchable: false,
126
+ integration: 'stripe'
127
+ }
128
+
129
+ collection.fields << {
130
+ field: :stripe_bank_accounts,
131
+ type: ['String'],
132
+ reference: "#{model_name}_stripe_bank_accounts.id",
133
+ column: nil,
134
+ is_searchable: false,
135
+ integration: 'stripe'
136
+ }
137
+ end
108
138
  end
109
139
 
110
140
  # Paperclip url attribute