webhook_system 1.0.1 → 1.0.2

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: 95522481f50719717dc59eef000851d4f6921c15
4
- data.tar.gz: 93e0a7d8465075deeccb66beb51c35870a240732
3
+ metadata.gz: f1e79d3307cae895f3ea016c4dd81c18c5b6354d
4
+ data.tar.gz: 3a8cd5f259b506a2fbf305f718b997580dbd5cb4
5
5
  SHA512:
6
- metadata.gz: 08ae9646bdc1ec58e3b75a715d08aecc973060c5803a9077c88fe6cb1aff2f8a53d394cf4bceac9a7a9b8a4c7f0dcf1622719c3a1270d8e22971909ed3883404
7
- data.tar.gz: 83b5ca480c52977eabd1d6dc82189c5e0491cd1a94990fc0992b46bab25f4ff72ab2f01022d72c37ac9530b29a35b327608a70647eea9392edaa7122e330b39e
6
+ metadata.gz: f64d2e41f827ded493d94c87b2a0ddf8a1bba499111cd34599c006d799c557af9530c4cdbc80d2b7fbdbcb3122f6b0887937f26fffe40b0bb71e57386d278fc2
7
+ data.tar.gz: 76a400599bfd625ea6fa85dc9e40d1cb3cf22200e8acc25118fb1dd8bcea20a7fe4901fdaa335119ab9ad2ed13b74c0ed263206664143c7c2a1b3bca0b7bf722
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ doc/
3
3
  pkg/
4
4
  *.db
5
5
  tmp/
6
+ .ruby-version
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Change Log
2
2
 
3
+ ## [v1.0.1](https://github.com/payrollhero/webhook_system/tree/v1.0.1) (2016-02-16)
4
+ [Full Changelog](https://github.com/payrollhero/webhook_system/compare/v1.0.0...v1.0.1)
5
+
6
+ - Add missing require for Faraday [\#5](https://github.com/payrollhero/webhook_system/pull/5) ([mykola-kyryk](https://github.com/mykola-kyryk))
7
+
3
8
  ## [v1.0.0](https://github.com/payrollhero/webhook_system/tree/v1.0.0) (2016-02-11)
4
9
  [Full Changelog](https://github.com/payrollhero/webhook_system/compare/v0.1.1...v1.0.0)
5
10
 
@@ -0,0 +1,21 @@
1
+ # This is taken from https://github.com/ma2gedev/faraday-encoding,
2
+ # all credits for this goes to Takayuki Matsubara (takayuki.1229+github@gmail.com).
3
+ # We should be able to switch to the original gem when
4
+ # this PR (https://github.com/ma2gedev/faraday-encoding/pull/2) is merged.
5
+ module Faraday
6
+ class Faraday::Encoding < Faraday::Middleware
7
+ def call(environment)
8
+ @app.call(environment).on_complete do |env|
9
+ @content_charset = nil
10
+ if /;\s*charset=\s*(.+?)\s*(;|$)/.match(env[:response_headers][:content_type])
11
+ encoding = $1
12
+ encoding = 'utf-8' if encoding == 'utf8' # this is the actual fix
13
+ @content_charset = ::Encoding.find encoding rescue nil
14
+ end
15
+ env[:body].force_encoding @content_charset if @content_charset
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Faraday::Response.register_middleware :encoding => Faraday::Encoding
@@ -4,6 +4,7 @@ require 'active_job'
4
4
  require 'ph_model'
5
5
  require 'validate_url'
6
6
  require 'faraday'
7
+ require 'faraday_middleware/encoding'
7
8
 
8
9
  module WebhookSystem
9
10
  extend ActiveSupport::Autoload
@@ -4,6 +4,8 @@ module WebhookSystem
4
4
  class EventLog < ActiveRecord::Base
5
5
  self.table_name = 'webhook_event_logs'
6
6
 
7
+ MAX_JSON_ATTRIBUTE_SIZE = 40_000
8
+
7
9
  belongs_to :subscription, class_name: 'WebhookSystem::Subscription'
8
10
 
9
11
  validates :event_id, presence: true
@@ -18,12 +20,12 @@ module WebhookSystem
18
20
  request_info = {
19
21
  'event' => event,
20
22
  'headers' => request.headers.to_hash,
21
- 'body' => request.body,
23
+ 'body' => request.body.truncate(MAX_JSON_ATTRIBUTE_SIZE),
22
24
  'url' => request.path,
23
25
  }
24
26
  response_info = {
25
27
  'headers' => response.headers.to_hash,
26
- 'body' => response.body,
28
+ 'body' => response.body.truncate(MAX_JSON_ATTRIBUTE_SIZE),
27
29
  }
28
30
 
29
31
  attributes = {
@@ -40,12 +40,26 @@ module WebhookSystem
40
40
  end
41
41
 
42
42
  def self.log_response(subscription, event, request, response)
43
- EventLog.construct(subscription, event, request, response).save!
43
+ event_log = EventLog.construct(subscription, event, request, response)
44
+
45
+ # we write log in a separate thread to make sure it is created even if the whole job fails
46
+ # Usually any background job would be wrapped into transaction,
47
+ # so if the job fails we would rollback any DB changes, including the even log record.
48
+ # We want the even log record to always be created, so we check if we are running inside the transaction,
49
+ # if we are - we create the record in a separate thread. New Thread means a new DB connection and
50
+ # ActiveRecord transactions are per connection, which gives us the "transaction jailbreak".
51
+ if ActiveRecord::Base.connection.open_transactions == 0
52
+ event_log.save!
53
+ else
54
+ Thread.new { event_log.save! }.join
55
+ end
44
56
  end
45
57
 
46
58
  def self.build_client
47
59
  Faraday.new do |faraday|
48
60
  faraday.response :logger if ENV['WEBHOOK_DEBUG']
61
+ # use Faraday::Encoding middleware, libfaraday_middleware/encoding.rb
62
+ faraday.response :encoding
49
63
  faraday.adapter Faraday.default_adapter
50
64
  end
51
65
  end
@@ -1,3 +1,3 @@
1
1
  module WebhookSystem
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.add_runtime_dependency 'activesupport', '> 3.2'
22
22
  gem.add_runtime_dependency 'activerecord', '> 3.2'
23
23
  gem.add_runtime_dependency 'activejob'
24
- gem.add_runtime_dependency 'faraday'
24
+ gem.add_runtime_dependency 'faraday', '~> 0.9'
25
25
  gem.add_runtime_dependency 'ph_model'
26
26
  gem.add_runtime_dependency 'validate_url', '~> 1.0'
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webhook_system
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Banasik
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-16 00:00:00.000000000 Z
12
+ date: 2016-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -57,16 +57,16 @@ dependencies:
57
57
  name: faraday
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '0'
62
+ version: '0.9'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: '0.9'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: ph_model
72
72
  requirement: !ruby/object:Gem::Requirement
@@ -268,6 +268,7 @@ files:
268
268
  - LICENSE.txt
269
269
  - README.md
270
270
  - Rakefile
271
+ - lib/faraday_middleware/encoding.rb
271
272
  - lib/webhook_system.rb
272
273
  - lib/webhook_system/base_event.rb
273
274
  - lib/webhook_system/dispatcher.rb