trusty 0.0.7 → 0.0.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b77b80ef65c070c5a9495fe84266637f79d3f0f6
4
- data.tar.gz: 404bce742414fee7241f75997c52e9e577254d2c
3
+ metadata.gz: f19690f72d7d3b49ee371125c130f005e1a7b347
4
+ data.tar.gz: 525c37e879aa8a171a2d5ba213db391206091f79
5
5
  SHA512:
6
- metadata.gz: 8bdebec526788514c3503e985037e13813e74a1f1e811b4bd1433c496dacedffa6d7c6d9a0eced42e8d2c17639ed6c7de7a10038ecf3fc707dec2f6e1c48f605
7
- data.tar.gz: 6db733cc52d5da507261dd29c69a9b0a35ce881052cf3fac3ef379adf58fa399b26ffdbf46114d2a1b20d7edbea960af3fd43c195b3daf9189e8274b0a35a4bb
6
+ metadata.gz: 6e1fdf638e05d39b2783c2fabdfc6fb669a197e032bff5f2e8d6a91f372846bc7f16fbb5a05ec9b361ce177be0358085f5a54d1890715657b291644365fe04ee
7
+ data.tar.gz: 0aaf8265f6ce15530213afb547a544638b63ac31340df3310c570ddef4ae663e84bff0764f8586ee5c96a4fad694d44d7e7d987c8de8d8bc5ca758a6473c9514
data/README.md CHANGED
@@ -152,6 +152,46 @@ class MyModel
152
152
  end
153
153
  ```
154
154
 
155
+ ### Error Handling
156
+
157
+ You can include the `Trusty::Errors::Retry` module to call the `retry_block` helper method to retry a block of code.
158
+ This is useful when dealing with HTTP errors and such things with APIs and so on.
159
+ You can also easily add the retry behavior to a method with the same options you would pass to `retry_block`.
160
+
161
+ ```Ruby
162
+ class MyModel
163
+ include Trusty::Errors::Retry
164
+
165
+ def api_request
166
+ retry_block :retry => 3 do
167
+ # ... some API calls ...
168
+ end
169
+ end
170
+
171
+ # add retry behavior to any method
172
+ def api_request2
173
+ # ... some API calls ...
174
+ end
175
+ retry_method :api_request2, :retry => 3
176
+
177
+ end
178
+ ```
179
+
180
+ #### Exception Notification Integration
181
+
182
+ If you use the "ExceptionNotification" gem, requiring `trusty/exception_notification` will automatically listen for exceptions thrown and handled from `Trusty::Errors::*`.
183
+ Exceptions handled by Trusty will include contextual data and the `env` data for ExceptionNotification to include in error emails.
184
+
185
+ ### Rails Extensions
186
+
187
+ Require `trusty/rails` to register the `Trusty::Rails::Engine` and add some extensions to Rails.
188
+ Controller extensions are added to fix quirks such as making sure Flash messages aren't lost on redirect.
189
+ Error handling in Rake and ExceptionNotification are also wired up to listen to exceptions handled by Trusty (see above).
190
+
191
+ ### Iron.io
192
+
193
+ The `Trusty::IronIo::QueueProcessor` makes it easy to pull messages off a queue.
194
+
155
195
  ## Contributing
156
196
 
157
197
  1. Fork it
@@ -0,0 +1,27 @@
1
+ require 'active_support'
2
+
3
+ module Trusty
4
+ module Errors
5
+ module ExceptionHandlers
6
+
7
+ def try_with_data(data, &block)
8
+ begin
9
+ yield
10
+ rescue => exception
11
+ notify_exception exception, :data => data, :raise => true
12
+ end
13
+ end
14
+
15
+ # include in classes
16
+ def notify_exception(exception, options = {})
17
+
18
+ options[:env] ||= respond_to?(:request) ? request.env : respond_to?(:env) ? env : nil
19
+
20
+ ActiveSupport::Notifications.publish("trusty.errors.notify_exception", exception, options)
21
+
22
+ raise exception if options[:raise] == true
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'trusty/errors/exception_handlers'
2
+
3
+ module Trusty
4
+ module Errors
5
+ module Retry
6
+ include ExceptionHandlers
7
+
8
+ # retry a block of code
9
+ def retry_block(options = {}, &block)
10
+ options = {
11
+ :retry => 1,
12
+ :data => {},
13
+ :type => StandardError
14
+ }.merge(options)
15
+
16
+ retries = case options[:retry]
17
+ when true
18
+ 1
19
+ when Integer
20
+ options[:retry]
21
+ else
22
+ 0
23
+ end
24
+
25
+ types = [ options[:type] ].flatten.compact
26
+
27
+ begin
28
+ yield
29
+ rescue *types => ex
30
+ if retries > 0
31
+ return self.send(__method__, options.merge(:retry => retries - 1), &block)
32
+ else
33
+ notify_exception(ex, :data => options[:data], :raise => true)
34
+ end
35
+ end
36
+ end
37
+
38
+ # helper method to redefine method (a la alias_method_chain, etc)
39
+ def retry_method(method, options = {})
40
+ define_method method do |*args, &block|
41
+ super_method = method(:super)
42
+ retry_block options do
43
+ super_method(*args, &block)
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,2 @@
1
+ require 'trusty/errors/exception_handlers'
2
+ require 'trusty/errors/retry'
@@ -0,0 +1,17 @@
1
+ require 'exception_notification'
2
+
3
+ module Trusty
4
+ module ExceptionNotification
5
+
6
+ end
7
+ end
8
+
9
+ # register exception listener for Trusty::Errors::ExceptionHelper and Trusty::ExceptionNotification::Rake
10
+
11
+ ActiveSupport::Notifications.subscribe("trusty.errors.notify_exception") do |exception, options|
12
+ if env = options.delete(:env)
13
+ ExceptionNotifier::Notifier.exception_notification(env, exception, options).deliver!
14
+ else
15
+ ExceptionNotifier::Notifier.background_exception_notification(exception, options).deliver!
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ require 'iron_mq'
2
+ require 'trusty/errors/exception_handlers'
3
+
4
+ module Trusty
5
+ module IronIo
6
+ class QueueProcessor
7
+ include ::Trusty::Errors::ExceptionHandlers
8
+
9
+ # helper method forwards to "run" instance method
10
+ def self.run(queue_name, *args, &block)
11
+ new(queue_name).run(*args, &block)
12
+ end
13
+
14
+ attr_reader :queue_name, :client
15
+
16
+ def initialize(queue_name)
17
+ @queue_name = queue_name
18
+ @client = IronMQ::Client.new(:token => ENV['IRON_TOKEN'], :project_id => ENV['IRON_PROJECT_ID'])
19
+ end
20
+
21
+ def default_options
22
+ { :timeout => 30, :break_if_nil => true }
23
+ end
24
+
25
+ def queue
26
+ @queue ||= client.queue(queue_name)
27
+ end
28
+
29
+ def run(options = {}, &block)
30
+ options = default_options.merge(options)
31
+
32
+ queue.poll(options) do |message|#, :break_if_nil => true do |message|
33
+
34
+ # parse body for better data formatting
35
+ begin
36
+ body = JSON.parse message.body
37
+ rescue JSON::ParserError => ex
38
+ body = message.body
39
+ end
40
+
41
+ try_with_data :message_id => message.id, :body => body do
42
+ block.call(message, queue)
43
+ end
44
+ end
45
+ end
46
+
47
+ def webhook_url
48
+ url_template = 'https://mq-aws-us-east-1.iron.io/1/projects/%{project_id}/queues/%{queue_name}/messages/webhook?oauth=%{token}'
49
+
50
+ url_template % {
51
+ project_id: ENV['IRON_TOKEN'],
52
+ token: ENV['IRON_PROJECT_ID'],
53
+ queue_name: queue_name
54
+ }
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,3 @@
1
- module Trusty
2
- module Omniauth
3
-
4
- end
5
- end
1
+ require 'trusty/omniauth/mapping_helpers'
2
+ require 'trusty/omniauth/model_mapper'
3
+ require 'trusty/omniauth/provider_mapper'
@@ -0,0 +1,13 @@
1
+ module Trusty
2
+ module Rails
3
+ module ControllerExtensions
4
+
5
+ # make sure Flash messages are preserved on redirect
6
+ def redirect_to(*args)
7
+ flash.keep
8
+ super
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ require 'trusty/rails/controller_extensions'
2
+
3
+ module Trusty
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+
7
+ initializer "trusty.rails.initialize_views" do |app|
8
+ ActiveSupport.on_load :action_view do
9
+ # TODO: Add view extensions
10
+ end
11
+ end
12
+
13
+ initializer "trusty.rails.initialize_controllers" do |app|
14
+ ActiveSupport.on_load :action_controller do
15
+ include ControllerExtensions
16
+ end
17
+ end
18
+
19
+ if defined? Rake
20
+ initializer "trusty.rails.initialize_rake" do |app|
21
+ require 'trusty/rake'
22
+ end
23
+ end
24
+
25
+ if defined? ExceptionNotification
26
+ initializer "trusty.rails.initialize_exception_notification" do |app|
27
+ require 'trusty/exception_notification'
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require 'trusty/rails/engine'
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'trusty/errors/exception_handlers'
3
+
4
+ # FROM:
5
+ # https://github.com/thoughtbot/airbrake/blob/master/lib/airbrake/rake_handler.rb
6
+ # Patch Rake::Application to handle errors with Airbrake
7
+ module Trusty
8
+ module Rake
9
+ include ::Trusty::Errors::ExceptionHandlers
10
+
11
+ def self.included(base)
12
+ base.class_eval do
13
+ alias_method :display_error_message_without_email, :display_error_message
14
+ alias_method :display_error_message, :display_error_message_with_email
15
+ end
16
+ end
17
+
18
+ def self.handle_exceptions!
19
+ Rake.application.instance_eval do
20
+ class << self
21
+ # include this module
22
+ include Rake
23
+ end
24
+ end
25
+ end
26
+
27
+ def display_error_message_with_email(exception)
28
+ notify_exception(exception)
29
+ display_error_message_without_email(exception)
30
+ end
31
+ end
32
+ end
33
+
34
+ # wire up exception handling in Rake tasks
35
+ Trusty::Rake.handle_exceptions!
@@ -1,5 +1,2 @@
1
- module Trusty
2
- module Utilities
3
-
4
- end
5
- end
1
+ require 'trusty/utilities/method_name'
2
+ require 'trusty/utilities/method_name_extensions'
@@ -1,3 +1,3 @@
1
1
  module Trusty
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/trusty.rb CHANGED
@@ -2,5 +2,13 @@ require "trusty/version"
2
2
  require "trusty/environment"
3
3
  require "trusty/utilities"
4
4
 
5
+ # Optional features that need to be required manually when using this gem
6
+ # require "trusty/errors"
7
+ # require "trusty/exception_notification"
8
+ # require "trusty/iron_io"
9
+ # require "trusty/omniauth"
10
+ # require "trusty/rails"
11
+ # require "trusty/rake"
12
+
5
13
  # copy out of namespace
6
14
  ::Vars = Trusty::Environment
data/trusty.gemspec CHANGED
@@ -20,9 +20,27 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "omniauth"
23
+
24
+ # Trusty::Rails (and defined?(Rails) checks sprinkled throughout)
24
25
  spec.add_development_dependency "rails"
25
26
 
27
+ # Trusty::Omniauth
28
+ spec.add_development_dependency "omniauth"
29
+
30
+ # Trusty::Errors::ExceptionHandlers
31
+ spec.add_development_dependency "active_support" # active_support/notifications
32
+
33
+ # Trusty::ExceptionNotification
34
+ spec.add_development_dependency "exception_notification"
35
+
36
+ # Trusty::Rake
37
+ spec.add_development_dependency "rake"
38
+
39
+ # Trusty::IronIo::QueueProcessor
40
+ spec.add_development_dependency "typhoeus" # used by iron_mq
41
+ spec.add_development_dependency "iron_mq"
42
+
43
+ # Trusty::Environment
26
44
  spec.add_dependency "dotenv", ">= 0.9.0"
27
45
  spec.add_dependency "hashie", ">= 2.0"
28
46
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trusty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Van Horn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-01 00:00:00.000000000 Z
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: omniauth
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -53,7 +67,63 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: rails
70
+ name: active_support
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: exception_notification
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: typhoeus
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: iron_mq
57
127
  requirement: !ruby/object:Gem::Requirement
58
128
  requirements:
59
129
  - - '>='
@@ -109,10 +179,19 @@ files:
109
179
  - lib/trusty.rb
110
180
  - lib/trusty/environment.rb
111
181
  - lib/trusty/environment/dot_file_parser.rb
182
+ - lib/trusty/errors.rb
183
+ - lib/trusty/errors/exception_handlers.rb
184
+ - lib/trusty/errors/retry.rb
185
+ - lib/trusty/exception_notification.rb
186
+ - lib/trusty/iron_io/queue_processor.rb
112
187
  - lib/trusty/omniauth.rb
113
188
  - lib/trusty/omniauth/mapping_helpers.rb
114
189
  - lib/trusty/omniauth/model_mapper.rb
115
190
  - lib/trusty/omniauth/provider_mapper.rb
191
+ - lib/trusty/rails.rb
192
+ - lib/trusty/rails/controller_extensions.rb
193
+ - lib/trusty/rails/engine.rb
194
+ - lib/trusty/rake.rb
116
195
  - lib/trusty/utilities.rb
117
196
  - lib/trusty/utilities/method_name.rb
118
197
  - lib/trusty/utilities/method_name_extensions.rb