chargify-loops 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gem 'combustion',
4
+ :git => 'git://github.com/freelancing-god/combustion.git',
5
+ :ref => 'cd3ccae'
6
+
7
+ gemspec
data/HISTORY ADDED
@@ -0,0 +1,2 @@
1
+ 0.1.0 - November 17th 2011
2
+ * First release
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Pat Allan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ h1. Chargify Loops
2
+
3
+ Chargify Loops is a Rails engine that handles Chargify's Web Hooks. All you need to do is pair hooks to loops for any particular events you care about, and this engine handles everything else.
4
+
5
+ This is for Rails 3 only - though developers using Rails 2 are welcome to pick things out of the source for their apps.
6
+
7
+ h2. Installation and Usage
8
+
9
+ Add the following to your Gemfile:
10
+
11
+ <pre><code>gem 'chargify-loops', '~> 0.1.0'</code></pre>
12
+
13
+ Then add an initializer for your loops, and provide your shared key:
14
+
15
+ <pre><code># config/initializers/chargify_loops.rb
16
+ Chargify::Loops.shared_key = 'secret-key-from-chargify'
17
+
18
+ Chargify::Loops.loop! :signup_success do |payload|
19
+ Emails.welcome(payload[:subscription][:customer][:email]).deliver
20
+ end
21
+
22
+ Chargify::Loops.loop! :payment_failure do |payload|
23
+ Banker.loan payload[:payment][:amount_in_cents]
24
+ end</code></pre>
25
+
26
+ If a web hook is invalid, it'll never hit your loops - so you're only dealing with valid hook requests.
27
+
28
+ Also: it's best to keep your loops short and sweet - don't put business logic in your initializer, but somewhere else where you've got some test coverage. As a rule of thumb, if you've got more than one line inside a loop block, then you should probably consider whether that code could be somewhere else.
29
+
30
+ Finally, make sure Chargify is set up to send its hooks your way. This library adds a route that accepts POST requests to @/chargify/hooks@ - so if your site is @www.example.com@, then you'll want to tell Chargify to send hooks to @http://www.example.com/chargify/hooks@.
31
+
32
+ h2. Contributing
33
+
34
+ Contributions are very much welcome - but keep in mind the following:
35
+
36
+ * Keep patches in a separate branch.
37
+ * Write tests for your patches.
38
+ * Don't mess with the version or history file. I'll take care of that when the patch is merged in.
39
+
40
+ h2. Credits
41
+
42
+ Copyright (c) 2011, Chargify Loops is developed and maintained by Pat Allan, and is released under the open MIT Licence.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ class Chargify::HooksController < ActionController::Base
2
+ before_filter :verify_signature
3
+
4
+ def create
5
+ Chargify::Loops.delegate_hook params[:event].to_sym,
6
+ params[:payload].symbolize_keys
7
+
8
+ head :ok
9
+ end
10
+
11
+ private
12
+
13
+ def generated_signature
14
+ Digest::MD5.hexdigest(Chargify::Loops.shared_key + request.raw_post)
15
+ end
16
+
17
+ def provided_signature
18
+ request.headers['X-Chargify-Webhook-Signature']
19
+ end
20
+
21
+ def verify_signature
22
+ head :forbidden unless generated_signature == provided_signature
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'chargify/loops/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'chargify-loops'
7
+ s.version = Chargify::Loops::VERSION
8
+ s.authors = ['Pat Allan']
9
+ s.email = ['pat@freelancing-gods.com']
10
+ s.homepage = 'https://github.com/freelancing-god/chargify-loops'
11
+ s.summary = %q{A Rails Engine for Chargify Webhooks}
12
+ s.description = %q{A Rails Engine that handles Chargify Webhooks requests and validates and forwards them off to registered loops.}
13
+
14
+ s.rubyforge_project = 'chargify-loops'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f|
19
+ File.basename(f)
20
+ }
21
+ s.require_paths = ['lib']
22
+
23
+ s.add_runtime_dependency 'rails', '~> 3.0'
24
+ s.add_development_dependency 'rspec-rails', '2.7.0'
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize! :action_controller
7
+ run Combustion::Application
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ namespace :chargify do
3
+ resources :hooks, :only => :create
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'chargify/loops'
@@ -0,0 +1,31 @@
1
+ module Chargify
2
+ module Loops
3
+ def self.delegate_hook(event, payload)
4
+ loops[event] && loops[event].each do |block|
5
+ block.call payload
6
+ end
7
+ end
8
+
9
+ def self.loops
10
+ @loops ||= {}
11
+ end
12
+
13
+ def self.loop!(event, &block)
14
+ loops[event] ||= []
15
+ loops[event] << block
16
+ end
17
+
18
+ def self.shared_key
19
+ @shared_key || ''
20
+ end
21
+
22
+ def self.shared_key=(key)
23
+ @shared_key = key
24
+ end
25
+ end
26
+ end
27
+
28
+ require 'digest/md5'
29
+
30
+ require 'chargify/loops/version'
31
+ require 'chargify/loops/engine'
@@ -0,0 +1,4 @@
1
+ class Chargify::Loops::Engine < Rails::Engine
2
+ paths['app/controllers'] << 'app/controllers'
3
+ paths['config'] << 'config'
4
+ end
@@ -0,0 +1,5 @@
1
+ module Chargify
2
+ module Loops
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Loops do
4
+ before :each do
5
+ Chargify::Loops.loops.clear
6
+ end
7
+
8
+ describe '.delegate_hook' do
9
+ let(:alpha_hook) { double('hook') }
10
+ let(:beta_hook) { double('hook') }
11
+
12
+ it "calls every registered loop for the given event with the payload" do
13
+ Chargify::Loops.loops[:alpha] = [alpha_hook]
14
+ Chargify::Loops.loops[:beta] = [beta_hook]
15
+
16
+ alpha_hook.should_receive(:call).with(:foo)
17
+ beta_hook.should_not_receive(:call)
18
+
19
+ Chargify::Loops.delegate_hook :alpha, :foo
20
+ end
21
+
22
+ it "does not complain if there's no loops for a given hook" do
23
+ Chargify::Loops.delegate_hook :alpha, :foo
24
+ end
25
+ end
26
+
27
+ describe '.loop!' do
28
+ it "appends a loop for the given event" do
29
+ Chargify::Loops.loop! :test do |payload|
30
+ #
31
+ end
32
+
33
+ Chargify::Loops.loops[:test].length.should == 1
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::HooksController do
4
+ before :each do
5
+ Chargify::Loops.stub :shared_key => Digest::MD5.hexdigest(Time.now.to_s)
6
+ end
7
+
8
+ def post_with_signature(action, params = {})
9
+ body = params.to_query
10
+ signature = Digest::MD5.hexdigest(Chargify::Loops.shared_key + body)
11
+
12
+ request.env['X-Chargify-Webhook-Signature'] = signature
13
+
14
+ post action, params
15
+ end
16
+
17
+ describe '#create' do
18
+ context 'invalid signature' do
19
+ it "responds with a 403 Forbidden code" do
20
+ post :create
21
+
22
+ response.code.to_i.should == 403
23
+ end
24
+ end
25
+
26
+ context 'valid signatures' do
27
+ before :each do
28
+ Chargify::Loops.stub :delegate_hook => true
29
+ end
30
+
31
+ it "responds with a 200 OK code" do
32
+ post_with_signature :create, :payload => {:chargify => 'testing'},
33
+ :event => 'test'
34
+
35
+ response.code.to_i.should == 200
36
+ end
37
+
38
+ it "delegates the hook to the appropriate loops" do
39
+ Chargify::Loops.should_receive(:delegate_hook).
40
+ with(:test, {:chargify => 'testing'})
41
+
42
+ post_with_signature :create, :payload => {:chargify => 'testing'},
43
+ :event => 'test'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize! :action_controller
7
+
8
+ require 'rspec/rails'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chargify-loops
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Pat Allan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-17 00:00:00 +07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-rails
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ hash: 19
45
+ segments:
46
+ - 2
47
+ - 7
48
+ - 0
49
+ version: 2.7.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: A Rails Engine that handles Chargify Webhooks requests and validates and forwards them off to registered loops.
53
+ email:
54
+ - pat@freelancing-gods.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - HISTORY
65
+ - LICENCE
66
+ - README.textile
67
+ - Rakefile
68
+ - app/controllers/chargify/hooks_controller.rb
69
+ - chargify-loops.gemspec
70
+ - config.ru
71
+ - config/routes.rb
72
+ - lib/chargify-loops.rb
73
+ - lib/chargify/loops.rb
74
+ - lib/chargify/loops/engine.rb
75
+ - lib/chargify/loops/version.rb
76
+ - spec/chargify/loops_spec.rb
77
+ - spec/controllers/chargify/hooks_controller_spec.rb
78
+ - spec/internal/log/.gitignore
79
+ - spec/internal/public/favicon.ico
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: https://github.com/freelancing-god/chargify-loops
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project: chargify-loops
111
+ rubygems_version: 1.4.2
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: A Rails Engine for Chargify Webhooks
115
+ test_files:
116
+ - spec/chargify/loops_spec.rb
117
+ - spec/controllers/chargify/hooks_controller_spec.rb
118
+ - spec/internal/log/.gitignore
119
+ - spec/internal/public/favicon.ico
120
+ - spec/spec_helper.rb