forest_liana 1.1.28 → 1.1.29

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
  SHA1:
3
- metadata.gz: 0cacff1cf8cc8776b030d561c5f3fb2fcfca0ffd
4
- data.tar.gz: 9bab1157945c7ba58a5e7b60509f1e85d493735c
3
+ metadata.gz: 1b687506a6797279131b36df0bfc64abd86fade4
4
+ data.tar.gz: 4d4a03bcd5f529c234cdfc160f4e8c061c97dea1
5
5
  SHA512:
6
- metadata.gz: bd00d71d1c413cbfd93121f23d2d36cf928f7b5d0233c3870562a957005ee66b171771d69a721f90d1b908225058e01614e9bd723785d451f1e1d12a609b90ca
7
- data.tar.gz: 9097d120274e73de5cbf254cc6da318db7d91d2b12962913d0adfb5057cc0b75551b483823a0ca1669e0cac5b7a50fef441e795a26f473e9c4b6151ad9f32d63
6
+ metadata.gz: 4d17df6bfb4519de6e7425fb7a5315b3233ea58d0a7dc696d16f868f51c3c6829b4eecfaf14a2117f5e5ca1419c98cf47f8b8a8393a43df5b949ce21ff93ec67
7
+ data.tar.gz: 8159e958705d5042b3d8622bb5f8994f5c45710fe50eeae16316f0d29eccedc8b84357fbd2a0c2ebc77b2fd78679198cca4d3f4859ddb4d571129432d927caad
@@ -4,6 +4,10 @@ module ForestLiana
4
4
  class ApplicationController < ActionController::Base
5
5
  before_filter :authenticate_user_from_jwt
6
6
 
7
+ def current_user
8
+ @jwt_decoded_token
9
+ end
10
+
7
11
  def serialize_model(model, options = {})
8
12
  options[:is_collection] = false
9
13
  JSONAPI::Serializer.serialize(model, options)
@@ -28,8 +32,9 @@ module ForestLiana
28
32
 
29
33
  def authenticate_user_from_jwt
30
34
  if request.headers['Authorization']
31
- JWT.decode request.headers['Authorization'].split[1],
32
- ForestLiana.jwt_signing_key
35
+ @jwt_decoded_token = JWT.decode(
36
+ request.headers['Authorization'].split[1],
37
+ ForestLiana.jwt_signing_key).try(:first)
33
38
  else
34
39
  render nothing: true, status: 401
35
40
  end
@@ -25,24 +25,29 @@ module ForestLiana
25
25
  end
26
26
 
27
27
  def create
28
- if Rails::VERSION::MAJOR == 4
29
- record = @resource.create!(resource_params.permit!)
30
- else
31
- record = @resource.create!(resource_params, without_protection: true)
32
- end
28
+ getter = ResourceCreator.new(@resource, params)
29
+ getter.perform
30
+
31
+ ActivityLogger.new.perform(current_user, 'created', params[:collection],
32
+ getter.record.id)
33
33
 
34
- render json: serialize_model(record, include: includes)
34
+ render json: serialize_model(getter.record, include: includes)
35
35
  end
36
36
 
37
37
  def update
38
38
  getter = ResourceUpdater.new(@resource, params)
39
39
  getter.perform
40
40
 
41
+ ActivityLogger.new.perform(current_user, 'updated', params[:collection],
42
+ getter.record.id)
43
+
41
44
  render json: serialize_model(getter.record, include: includes)
42
45
  end
43
46
 
44
47
  def destroy
45
48
  @resource.destroy_all(id: params[:id])
49
+ ActivityLogger.new.perform(current_user, 'deleted', params[:collection],
50
+ params[:id])
46
51
  render nothing: true, status: 204
47
52
  end
48
53
 
@@ -64,7 +69,7 @@ module ForestLiana
64
69
  @resource
65
70
  .reflect_on_all_associations
66
71
  .select do |a|
67
- [:belongs_to, :has_and_belongs_to_many, :has_one]
72
+ [:belongs_to, :has_one]
68
73
  .include?(a.macro) && !a.options[:polymorphic]
69
74
  end
70
75
  .map {|a| a.name.to_s }
@@ -1,9 +1,10 @@
1
1
  module ForestLiana
2
2
  class ResourceDeserializer
3
3
 
4
- def initialize(resource, params)
4
+ def initialize(resource, record, params)
5
5
  @params = params
6
6
  @resource = resource
7
+ @record = record
7
8
  end
8
9
 
9
10
  def perform
@@ -36,9 +37,13 @@ module ForestLiana
36
37
  @attributes[name] = nil
37
38
  end
38
39
  when :has_many, :has_and_belongs_to_many
39
- if data.is_a?(Hash)
40
- @attributes[name] = data.map do |x|
41
- association.klass.find(x[:id])
40
+ if data.is_a?(Array)
41
+ data.each do |x|
42
+ existing_records = @record.send(name)
43
+ new_record = association.klass.find(x[:id])
44
+ if !existing_records.include?(new_record)
45
+ existing_records << new_record
46
+ end
42
47
  end
43
48
  end
44
49
  end
@@ -0,0 +1,35 @@
1
+ require 'jwt'
2
+
3
+ class ForestLiana::ActivityLogger
4
+
5
+ def perform(user, action, collection_name, resource_id)
6
+ token = JWT.encode({}, ForestLiana.jwt_signing_key, 'HS256')
7
+ uri = URI.parse("#{forest_url}/api/projects/#{project_id(user)}/activity-logs")
8
+ http = Net::HTTP.new(uri.host, uri.port)
9
+ http.use_ssl = true if forest_url.start_with?('https')
10
+
11
+ http.start do |client|
12
+ request = Net::HTTP::Post.new(uri.path)
13
+ request['Content-Type'] = 'application/json'
14
+ request['Authorization'] = "Bearer #{token}"
15
+ request.body = {
16
+ session: user['session'],
17
+ action: action,
18
+ collection: collection_name,
19
+ resource: resource_id
20
+ }.to_json
21
+
22
+ client.request(request)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def project_id(user)
29
+ user['session']['data']['relationships']['project']['data']['id'];
30
+ end
31
+
32
+ def forest_url
33
+ ENV['FOREST_URL'] || 'https://forestadmin-server.herokuapp.com';
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module ForestLiana
2
+ class ResourceCreator
3
+ attr_accessor :record
4
+
5
+ def initialize(resource, params)
6
+ @resource = resource
7
+ @params = params
8
+ end
9
+
10
+ def perform
11
+ if Rails::VERSION::MAJOR == 4
12
+ @record = @resource.create!(resource_params.permit!)
13
+ else
14
+ @record = @resource.create!(resource_params, without_protection: true)
15
+ end
16
+ end
17
+
18
+ def resource_params
19
+ ResourceDeserializer.new(@resource, @params[:resource]).perform
20
+ end
21
+
22
+ end
23
+ end
@@ -18,7 +18,7 @@ module ForestLiana
18
18
  end
19
19
 
20
20
  def resource_params
21
- ResourceDeserializer.new(@resource, @params[:resource]).perform
21
+ ResourceDeserializer.new(@resource, @record, @params[:resource]).perform
22
22
  end
23
23
 
24
24
  end
@@ -31,7 +31,8 @@ module ForestLiana
31
31
  type: ['String'],
32
32
  reference: 'intercom_conversations.id',
33
33
  column: nil,
34
- is_searchable: false
34
+ is_searchable: false,
35
+ integration: 'intercom'
35
36
  }
36
37
 
37
38
  @collection.fields << {
@@ -39,7 +40,8 @@ module ForestLiana
39
40
  type: 'String',
40
41
  reference: 'intercom_attributes.id',
41
42
  column: nil,
42
- is_searchable: false
43
+ is_searchable: false,
44
+ integration: 'intercom'
43
45
  }
44
46
  end
45
47
 
@@ -51,7 +53,8 @@ module ForestLiana
51
53
  type: ['String'],
52
54
  reference: 'stripe_payments.id',
53
55
  column: nil,
54
- is_searchable: false
56
+ is_searchable: false,
57
+ integration: 'stripe'
55
58
  }
56
59
 
57
60
  @collection.fields << {
@@ -59,7 +62,8 @@ module ForestLiana
59
62
  type: ['String'],
60
63
  reference: 'stripe_invoices.id',
61
64
  column: nil,
62
- is_searchable: false
65
+ is_searchable: false,
66
+ integration: 'stripe'
63
67
  }
64
68
 
65
69
  @collection.fields << {
@@ -67,7 +71,8 @@ module ForestLiana
67
71
  type: ['String'],
68
72
  reference: 'stripe_cards.id',
69
73
  column: nil,
70
- is_searchable: false
74
+ is_searchable: false,
75
+ integration: 'stripe'
71
76
  }
72
77
  end
73
78
 
@@ -26,7 +26,7 @@ module ForestLiana
26
26
 
27
27
  # Monkey patch the find_serializer_class_name method to specify the
28
28
  # good serializer to use.
29
- JSONAPI::Serializer.class_eval do
29
+ ::JSONAPI::Serializer.class_eval do
30
30
  def self.find_serializer_class_name(obj)
31
31
  SerializerFactory.get_serializer_name(obj.class)
32
32
  end
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.1.28"
2
+ VERSION = "1.1.29"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.28
4
+ version: 1.1.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -170,6 +170,7 @@ files:
170
170
  - app/serializers/forest_liana/stripe_card_serializer.rb
171
171
  - app/serializers/forest_liana/stripe_invoice_serializer.rb
172
172
  - app/serializers/forest_liana/stripe_payment_serializer.rb
173
+ - app/services/forest_liana/activity_logger.rb
173
174
  - app/services/forest_liana/collection.rb
174
175
  - app/services/forest_liana/has_many_getter.rb
175
176
  - app/services/forest_liana/intercom_attributes_getter.rb
@@ -177,6 +178,7 @@ files:
177
178
  - app/services/forest_liana/line_stat_getter.rb
178
179
  - app/services/forest_liana/operator_value_parser.rb
179
180
  - app/services/forest_liana/pie_stat_getter.rb
181
+ - app/services/forest_liana/resource_creator.rb
180
182
  - app/services/forest_liana/resource_getter.rb
181
183
  - app/services/forest_liana/resource_updater.rb
182
184
  - app/services/forest_liana/resources_getter.rb