├── spec
├── dummy
│ ├── log
│ │ └── .gitkeep
│ ├── app
│ │ ├── mailers
│ │ │ └── .gitkeep
│ │ ├── models
│ │ │ └── .gitkeep
│ │ ├── helpers
│ │ │ └── application_helper.rb
│ │ ├── controllers
│ │ │ └── application_controller.rb
│ │ └── views
│ │ │ └── layouts
│ │ │ └── application.html.erb
│ ├── lib
│ │ └── assets
│ │ │ └── .gitkeep
│ ├── public
│ │ ├── favicon.ico
│ │ ├── 500.html
│ │ ├── 422.html
│ │ └── 404.html
│ ├── config
│ │ ├── initializers
│ │ │ ├── stripe.rb
│ │ │ ├── mime_types.rb
│ │ │ ├── backtrace_silencers.rb
│ │ │ ├── session_store.rb
│ │ │ ├── wrap_parameters.rb
│ │ │ ├── inflections.rb
│ │ │ └── secret_token.rb
│ │ ├── routes.rb
│ │ ├── environment.rb
│ │ ├── locales
│ │ │ └── en.yml
│ │ ├── boot.rb
│ │ ├── database.yml
│ │ ├── environments
│ │ │ ├── development.rb
│ │ │ ├── test.rb
│ │ │ └── production.rb
│ │ └── application.rb
│ ├── config.ru
│ ├── Rakefile
│ ├── script
│ │ └── rails
│ └── README.rdoc
├── rails_helper.rb
├── support
│ └── fixtures
│ │ ├── evt_invalid_id.json
│ │ └── evt_charge_succeeded.json
├── spec_helper.rb
├── controllers
│ └── webhook_controller_spec.rb
└── lib
│ └── stripe_event_spec.rb
├── .rspec
├── Gemfile
├── lib
├── stripe_event
│ ├── version.rb
│ └── engine.rb
└── stripe_event.rb
├── config
└── routes.rb
├── gemfiles
├── rails3.1.gemfile
├── rails3.2.gemfile
├── rails4.0.gemfile
├── rails4.1.gemfile
├── rails4.0.gemfile.lock
├── rails4.1.gemfile.lock
├── rails3.2.gemfile.lock
└── rails3.1.gemfile.lock
├── .gitignore
├── Appraisals
├── app
└── controllers
│ └── stripe_event
│ └── webhook_controller.rb
├── Rakefile
├── .travis.yml
├── stripe_event.gemspec
├── LICENSE.md
├── CHANGELOG.md
└── README.md
/spec/dummy/log/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/mailers/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/app/models/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/lib/assets/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spec/dummy/public/favicon.ico:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.rspec:
--------------------------------------------------------------------------------
1 | --colour
2 | --format documentation
3 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "https://rubygems.org"
2 | gemspec
3 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/stripe.rb:
--------------------------------------------------------------------------------
1 | Stripe.api_key = 'STRIPE_API_KEY'
2 |
--------------------------------------------------------------------------------
/lib/stripe_event/version.rb:
--------------------------------------------------------------------------------
1 | module StripeEvent
2 | VERSION = "1.4.0"
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/app/helpers/application_helper.rb:
--------------------------------------------------------------------------------
1 | module ApplicationHelper
2 | end
3 |
--------------------------------------------------------------------------------
/config/routes.rb:
--------------------------------------------------------------------------------
1 | StripeEvent::Engine.routes.draw do
2 | root to: 'webhook#event', via: :post
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/routes.rb:
--------------------------------------------------------------------------------
1 | Rails.application.routes.draw do
2 | mount StripeEvent::Engine => "/stripe_event"
3 | end
4 |
--------------------------------------------------------------------------------
/lib/stripe_event/engine.rb:
--------------------------------------------------------------------------------
1 | module StripeEvent
2 | class Engine < ::Rails::Engine
3 | isolate_namespace StripeEvent
4 | end
5 | end
6 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | protect_from_forgery
3 | end
4 |
--------------------------------------------------------------------------------
/spec/rails_helper.rb:
--------------------------------------------------------------------------------
1 | ENV["RAILS_ENV"] = 'test'
2 | require File.expand_path("../dummy/config/environment", __FILE__)
3 | require 'rspec/rails'
4 |
--------------------------------------------------------------------------------
/gemfiles/rails3.1.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "https://rubygems.org"
4 |
5 | gem "rails", "~> 3.1.0"
6 |
7 | gemspec :path => "../"
8 |
--------------------------------------------------------------------------------
/gemfiles/rails3.2.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "https://rubygems.org"
4 |
5 | gem "rails", "~> 3.2.0"
6 |
7 | gemspec :path => "../"
8 |
--------------------------------------------------------------------------------
/gemfiles/rails4.0.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "https://rubygems.org"
4 |
5 | gem "rails", "~> 4.0.0"
6 |
7 | gemspec :path => "../"
8 |
--------------------------------------------------------------------------------
/gemfiles/rails4.1.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "https://rubygems.org"
4 |
5 | gem "rails", "~> 4.1.0"
6 |
7 | gemspec :path => "../"
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle/
2 | log/*.log
3 | pkg/
4 | spec/dummy/db/*.sqlite3
5 | spec/dummy/log/*.log
6 | spec/dummy/tmp/
7 | spec/dummy/.sass-cache
8 | Gemfile.lock
9 | coverage/*
10 |
--------------------------------------------------------------------------------
/spec/dummy/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Dummy::Application
5 |
--------------------------------------------------------------------------------
/spec/support/fixtures/evt_invalid_id.json:
--------------------------------------------------------------------------------
1 | {
2 | "error": {
3 | "message": "No such notification: invalid-id",
4 | "param": "id",
5 | "type": "invalid_request_error"
6 | }
7 | }
--------------------------------------------------------------------------------
/spec/dummy/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the rails application
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the rails application
5 | Dummy::Application.initialize!
6 |
--------------------------------------------------------------------------------
/spec/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Sample localization file for English. Add more files in this directory for other locales.
2 | # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 |
4 | en:
5 | hello: "Hello world"
6 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/mime_types.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new mime types for use in respond_to blocks:
4 | # Mime::Type.register "text/richtext", :rtf
5 | # Mime::Type.register_alias "text/html", :iphone
6 |
--------------------------------------------------------------------------------
/Appraisals:
--------------------------------------------------------------------------------
1 | appraise "rails3.1" do
2 | gem "rails", "~> 3.1.0"
3 | end
4 |
5 | appraise "rails3.2" do
6 | gem "rails", "~> 3.2.0"
7 | end
8 |
9 | appraise "rails4.0" do
10 | gem "rails", "~> 4.0.0"
11 | end
12 |
13 | appraise "rails4.1" do
14 | gem "rails", "~> 4.1.0"
15 | end
16 |
--------------------------------------------------------------------------------
/spec/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3 |
4 | if File.exist?(gemfile)
5 | ENV['BUNDLE_GEMFILE'] = gemfile
6 | require 'bundler'
7 | Bundler.setup
8 | end
9 |
10 | $:.unshift File.expand_path('../../../../lib', __FILE__)
--------------------------------------------------------------------------------
/spec/dummy/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env rake
2 | # Add your own tasks in files placed in lib/tasks ending in .rake,
3 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4 |
5 | require File.expand_path('../config/application', __FILE__)
6 |
7 | Dummy::Application.load_tasks
8 |
--------------------------------------------------------------------------------
/app/controllers/stripe_event/webhook_controller.rb:
--------------------------------------------------------------------------------
1 | module StripeEvent
2 | class WebhookController < ActionController::Base
3 | def event
4 | StripeEvent.instrument(params)
5 | head :ok
6 | rescue StripeEvent::UnauthorizedError
7 | head :unauthorized
8 | end
9 | end
10 | end
11 |
--------------------------------------------------------------------------------
/spec/dummy/script/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3 |
4 | APP_PATH = File.expand_path('../../config/application', __FILE__)
5 | require File.expand_path('../../config/boot', __FILE__)
6 | require 'rails/commands'
7 |
--------------------------------------------------------------------------------
/spec/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dummy
5 | <%= stylesheet_link_tag "application", :media => "all" %>
6 | <%= javascript_include_tag "application" %>
7 | <%= csrf_meta_tags %>
8 |
9 |
10 |
11 | <%= yield %>
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler/setup'
3 | require 'bundler/gem_tasks'
4 | require 'rspec/core/rake_task'
5 |
6 | RSpec::Core::RakeTask.new(:spec)
7 |
8 | if ENV['CI']
9 | task default: :spec
10 | else
11 | require 'appraisal'
12 | task :default do
13 | system('bundle exec rake appraisal spec')
14 | end
15 | end
16 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/backtrace_silencers.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5 |
6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7 | # Rails.backtrace_cleaner.remove_silencers!
8 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/session_store.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4 |
5 | # Use the database for sessions instead of the cookie-based default,
6 | # which shouldn't be used to store highly confidential information
7 | # (create the session table with "rails generate session_migration")
8 | # Dummy::Application.config.session_store :active_record_store
9 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: ruby
2 |
3 | rvm:
4 | - 1.9.2
5 | - 1.9.3
6 | - 2.0.0
7 | - 2.1
8 | - jruby-19mode
9 |
10 | gemfile:
11 | - gemfiles/rails3.1.gemfile
12 | - gemfiles/rails3.2.gemfile
13 | - gemfiles/rails4.0.gemfile
14 | - gemfiles/rails4.1.gemfile
15 |
16 | sudo: false
17 |
18 | matrix:
19 | exclude:
20 | - rvm: 1.9.2
21 | gemfile: gemfiles/rails4.0.gemfile
22 | - rvm: 1.9.2
23 | gemfile: gemfiles/rails4.1.gemfile
24 |
25 | notifications:
26 | email:
27 | - daniel.r.whalen+travis-ci@gmail.com
28 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 | #
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActiveSupport.on_load(:action_controller) do
8 | wrap_parameters format: [:json]
9 | end
10 |
11 | # Disable root element in JSON by default.
12 | ActiveSupport.on_load(:active_record) do
13 | self.include_root_in_json = false
14 | end
15 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/inflections.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new inflection rules using the following format
4 | # (all these examples are active by default):
5 | # ActiveSupport::Inflector.inflections do |inflect|
6 | # inflect.plural /^(ox)$/i, '\1en'
7 | # inflect.singular /^(ox)en/i, '\1'
8 | # inflect.irregular 'person', 'people'
9 | # inflect.uncountable %w( fish sheep )
10 | # end
11 | #
12 | # These inflection rules are supported but not enabled by default:
13 | # ActiveSupport::Inflector.inflections do |inflect|
14 | # inflect.acronym 'RESTful'
15 | # end
16 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | require 'coveralls'
2 | Coveralls.wear!
3 |
4 | require 'webmock/rspec'
5 | require File.expand_path('../../lib/stripe_event', __FILE__)
6 | Dir[File.expand_path('../spec/support/**/*.rb', __FILE__)].each { |f| require f }
7 |
8 | RSpec.configure do |config|
9 | config.order = 'random'
10 |
11 | config.expect_with :rspec do |c|
12 | c.syntax = :expect
13 | end
14 |
15 | config.before do
16 | @event_retriever = StripeEvent.event_retriever
17 | @notifier = StripeEvent.backend.notifier
18 | StripeEvent.backend.notifier = @notifier.class.new
19 | end
20 |
21 | config.after do
22 | StripeEvent.event_retriever = @event_retriever
23 | StripeEvent.backend.notifier = @notifier
24 | end
25 | end
26 |
--------------------------------------------------------------------------------
/spec/dummy/config/database.yml:
--------------------------------------------------------------------------------
1 | # # SQLite version 3.x
2 | # # gem install sqlite3
3 | # #
4 | # # Ensure the SQLite 3 gem is defined in your Gemfile
5 | # # gem 'sqlite3'
6 | # development:
7 | # adapter: sqlite3
8 | # database: db/development.sqlite3
9 | # pool: 5
10 | # timeout: 5000
11 | #
12 | # # Warning: The database defined as "test" will be erased and
13 | # # re-generated from your development database when you run "rake".
14 | # # Do not set this db to the same as development or production.
15 | # test:
16 | # adapter: sqlite3
17 | # database: db/test.sqlite3
18 | # pool: 5
19 | # timeout: 5000
20 | #
21 | # production:
22 | # adapter: sqlite3
23 | # database: db/production.sqlite3
24 | # pool: 5
25 | # timeout: 5000
26 |
--------------------------------------------------------------------------------
/spec/dummy/public/500.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | We're sorry, but something went wrong (500)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
We're sorry, but something went wrong.
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/secret_token.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 | # Make sure the secret is at least 30 characters and all random,
6 | # no regular words or you'll be exposed to dictionary attacks.
7 |
8 | key = 'e13aca438075c637003a11031f93861a73aea50c8520fd45af70c28f55348930da4274d3965462bbcf81b6e08f4eeb03cdda627a59379cd7ab15a2cbe6648ce2'
9 |
10 | # Renamed in Rails 4 - we'll avoid deprecation warning
11 | # by checking if config responds to 'secret_key_base='
12 | if Dummy::Application.config.respond_to? :secret_key_base=
13 | Dummy::Application.config.secret_key_base = key
14 | else
15 | Dummy::Application.config.secret_token = key
16 | end
17 |
--------------------------------------------------------------------------------
/spec/dummy/public/422.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The change you wanted was rejected (422)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
The change you wanted was rejected.
23 |
Maybe you tried to change something you didn't have access to.
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/spec/dummy/public/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The page you were looking for doesn't exist (404)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
The page you were looking for doesn't exist.
23 |
You may have mistyped the address or the page may have moved.
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/stripe_event.gemspec:
--------------------------------------------------------------------------------
1 | $:.push File.expand_path("../lib", __FILE__)
2 |
3 | # Maintain your gem's version:
4 | require "stripe_event/version"
5 |
6 | # Describe your gem and declare its dependencies:
7 | Gem::Specification.new do |s|
8 | s.name = "stripe_event"
9 | s.version = StripeEvent::VERSION
10 | s.license = "MIT"
11 | s.authors = ["Danny Whalen"]
12 | s.email = "daniel.r.whalen@gmail.com"
13 | s.homepage = "https://github.com/integrallis/stripe_event"
14 | s.summary = "Stripe webhook integration for Rails applications."
15 | s.description = "Stripe webhook integration for Rails applications."
16 |
17 | s.files = `git ls-files`.split("\n")
18 | s.test_files = `git ls-files -- Appraisals {spec,gemfiles}/*`.split("\n")
19 |
20 | s.add_dependency "activesupport", ">= 3.1"
21 | s.add_dependency "stripe", "~> 1.6"
22 |
23 | s.add_development_dependency "rails", ">= 3.1"
24 | s.add_development_dependency "rspec-rails", "~> 2.12"
25 | s.add_development_dependency "webmock", "~> 1.9"
26 | s.add_development_dependency "appraisal"
27 | s.add_development_dependency "coveralls"
28 | end
29 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright 2012-2014 Integrallis Software
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/spec/support/fixtures/evt_charge_succeeded.json:
--------------------------------------------------------------------------------
1 | {
2 | "created": 1335427082,
3 | "id": "evt_charge_succeeded",
4 | "livemode": false,
5 | "object": "event",
6 | "pending_webhooks": 0,
7 | "type": "charge.succeeded",
8 | "data": {
9 | "object": {
10 | "amount": 3279,
11 | "amount_refunded": 0,
12 | "created": 1335427082,
13 | "currency": "usd",
14 | "customer": null,
15 | "description": "order_id=21 email=jimmorrison@example.com",
16 | "disputed": false,
17 | "failure_message": null,
18 | "fee": 0,
19 | "id": "ch_00000000000000",
20 | "invoice": null,
21 | "livemode": false,
22 | "object": "charge",
23 | "paid": true,
24 | "refunded": false,
25 | "card": {
26 | "address_country": null,
27 | "address_line1": "4130 N. Paradise Way",
28 | "address_line1_check": "pass",
29 | "address_line2": "",
30 | "address_state": "AZ",
31 | "address_zip": "85251",
32 | "address_zip_check": "pass",
33 | "country": "US",
34 | "cvc_check": "pass",
35 | "exp_month": 1,
36 | "exp_year": 2013,
37 | "fingerprint": "5twio8INGMQkPl6M",
38 | "id": "cc_00000000000000",
39 | "last4": "4242",
40 | "name": "Jim Morrison",
41 | "object": "card",
42 | "type": "Visa"
43 | }
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/spec/dummy/config/environments/development.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb
3 |
4 | # In the development environment your application's code is reloaded on
5 | # every request. This slows down response time but is perfect for development
6 | # since you don't have to restart the web server when you make code changes.
7 | config.cache_classes = false
8 |
9 | # Log error messages when you accidentally call methods on nil.
10 | config.whiny_nils = true
11 |
12 | # Show full error reports and disable caching
13 | config.consider_all_requests_local = true
14 | config.action_controller.perform_caching = false
15 |
16 | # Don't care if the mailer can't send
17 | # config.action_mailer.raise_delivery_errors = false
18 |
19 | # Print deprecation notices to the Rails logger
20 | config.active_support.deprecation = :log
21 |
22 | # Only use best-standards-support built into browsers
23 | config.action_dispatch.best_standards_support = :builtin
24 |
25 | # Raise exception on mass assignment protection for Active Record models
26 | # config.active_record.mass_assignment_sanitizer = :strict
27 |
28 | # Log the query plan for queries taking more than this (works
29 | # with SQLite, MySQL, and PostgreSQL)
30 | # config.active_record.auto_explain_threshold_in_seconds = 0.5
31 |
32 | # Do not compress assets
33 | config.assets.compress = false
34 |
35 | # Expands the lines which load the assets
36 | config.assets.debug = true
37 | end
38 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 |
10 | # Configure static asset server for tests with Cache-Control for performance
11 | config.serve_static_assets = true
12 | config.static_cache_control = "public, max-age=3600"
13 |
14 | # Log error messages when you accidentally call methods on nil
15 | # config.whiny_nils = true
16 |
17 | # Configure eager load to avoid Rails 4 warning
18 | config.eager_load = false
19 |
20 | # Show full error reports and disable caching
21 | config.consider_all_requests_local = true
22 | config.action_controller.perform_caching = false
23 |
24 | # Raise exceptions instead of rendering exception templates
25 | config.action_dispatch.show_exceptions = false
26 |
27 | # Disable request forgery protection in test environment
28 | config.action_controller.allow_forgery_protection = false
29 |
30 | # Tell Action Mailer not to deliver emails to the real world.
31 | # The :test delivery method accumulates sent emails in the
32 | # ActionMailer::Base.deliveries array.
33 | # config.action_mailer.delivery_method = :test
34 |
35 | # Raise exception on mass assignment protection for Active Record models
36 | # config.active_record.mass_assignment_sanitizer = :strict
37 |
38 | # Print deprecation notices to the stderr
39 | config.active_support.deprecation = :stderr
40 | end
41 |
--------------------------------------------------------------------------------
/spec/controllers/webhook_controller_spec.rb:
--------------------------------------------------------------------------------
1 | require 'rails_helper'
2 | require 'spec_helper'
3 |
4 | describe StripeEvent::WebhookController do
5 | def stub_event(identifier, status = 200)
6 | stub_request(:get, "https://api.stripe.com/v1/events/#{identifier}").
7 | to_return(status: status, body: File.read("spec/support/fixtures/#{identifier}.json"))
8 | end
9 |
10 | def webhook(params)
11 | post :event, params.merge(use_route: :stripe_event)
12 | end
13 |
14 | it "succeeds with valid event data" do
15 | count = 0
16 | StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
17 | stub_event('evt_charge_succeeded')
18 |
19 | webhook id: 'evt_charge_succeeded'
20 |
21 | expect(response.code).to eq '200'
22 | expect(count).to eq 1
23 | end
24 |
25 | it "succeeds when the event_retriever returns nil (simulating an ignored webhook event)" do
26 | count = 0
27 | StripeEvent.event_retriever = lambda { |params| return nil }
28 | StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
29 | stub_event('evt_charge_succeeded')
30 |
31 | webhook id: 'evt_charge_succeeded'
32 |
33 | expect(response.code).to eq '200'
34 | expect(count).to eq 0
35 | end
36 |
37 | it "denies access with invalid event data" do
38 | count = 0
39 | StripeEvent.subscribe('charge.succeeded') { |evt| count += 1 }
40 | stub_event('evt_invalid_id', 404)
41 |
42 | webhook id: 'evt_invalid_id'
43 |
44 | expect(response.code).to eq '401'
45 | expect(count).to eq 0
46 | end
47 |
48 | it "ensures user-generated Stripe exceptions pass through" do
49 | StripeEvent.subscribe('charge.succeeded') { |evt| raise Stripe::StripeError, "testing" }
50 | stub_event('evt_charge_succeeded')
51 |
52 | expect { webhook id: 'evt_charge_succeeded' }.to raise_error(Stripe::StripeError, /testing/)
53 | end
54 | end
55 |
--------------------------------------------------------------------------------
/lib/stripe_event.rb:
--------------------------------------------------------------------------------
1 | require "active_support/notifications"
2 | require "stripe"
3 | require "stripe_event/engine" if defined?(Rails)
4 |
5 | module StripeEvent
6 | class << self
7 | attr_accessor :adapter, :backend, :event_retriever, :event_namespacer, :namespace
8 |
9 | def configure(&block)
10 | raise ArgumentError, "must provide a block" unless block_given?
11 | block.arity.zero? ? instance_eval(&block) : yield(self)
12 | end
13 | alias :setup :configure
14 |
15 | def instrument(params)
16 | begin
17 | event = event_retriever.call(params)
18 | rescue Stripe::AuthenticationError => e
19 | if params[:type] == "account.application.deauthorized"
20 | event = Stripe::Event.construct_from(params.deep_symbolize_keys)
21 | else
22 | raise UnauthorizedError.new(e)
23 | end
24 | rescue Stripe::StripeError => e
25 | raise UnauthorizedError.new(e)
26 | end
27 |
28 | backend.instrument namespace.call(event_namespacer.call(event, params)), event if event
29 | end
30 |
31 | def subscribe(name, callable = Proc.new)
32 | backend.subscribe namespace.to_regexp(name), adapter.call(callable)
33 | end
34 |
35 | def all(callable = Proc.new)
36 | subscribe nil, callable
37 | end
38 |
39 | def listening?(name)
40 | namespaced_name = namespace.call(name)
41 | backend.notifier.listening?(namespaced_name)
42 | end
43 | end
44 |
45 | class Namespace < Struct.new(:value, :delimiter)
46 | def call(name = nil)
47 | "#{value}#{delimiter}#{name}"
48 | end
49 |
50 | def to_regexp(name = nil)
51 | %r{^#{Regexp.escape call(name)}}
52 | end
53 | end
54 |
55 | class NotificationAdapter < Struct.new(:subscriber)
56 | def self.call(callable)
57 | new(callable)
58 | end
59 |
60 | def call(*args)
61 | payload = args.last
62 | subscriber.call(payload)
63 | end
64 | end
65 |
66 | class Error < StandardError; end
67 | class UnauthorizedError < Error; end
68 |
69 | self.adapter = NotificationAdapter
70 | self.backend = ActiveSupport::Notifications
71 | self.event_retriever = lambda { |params| Stripe::Event.retrieve(params[:id]) }
72 | self.event_namespacer = lambda { |event, params| event[:type] }
73 | self.namespace = Namespace.new("stripe_event", ".")
74 | end
75 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/production.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb
3 |
4 | # Code is not reloaded between requests
5 | config.cache_classes = true
6 |
7 | # Full error reports are disabled and caching is turned on
8 | config.consider_all_requests_local = false
9 | config.action_controller.perform_caching = true
10 |
11 | # Disable Rails's static asset server (Apache or nginx will already do this)
12 | config.serve_static_assets = false
13 |
14 | # Compress JavaScripts and CSS
15 | config.assets.compress = true
16 |
17 | # Don't fallback to assets pipeline if a precompiled asset is missed
18 | config.assets.compile = false
19 |
20 | # Generate digests for assets URLs
21 | config.assets.digest = true
22 |
23 | # Defaults to nil and saved in location specified by config.assets.prefix
24 | # config.assets.manifest = YOUR_PATH
25 |
26 | # Specifies the header that your server uses for sending files
27 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29 |
30 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31 | # config.force_ssl = true
32 |
33 | # See everything in the log (default is :info)
34 | # config.log_level = :debug
35 |
36 | # Prepend all log lines with the following tags
37 | # config.log_tags = [ :subdomain, :uuid ]
38 |
39 | # Use a different logger for distributed setups
40 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
41 |
42 | # Use a different cache store in production
43 | # config.cache_store = :mem_cache_store
44 |
45 | # Enable serving of images, stylesheets, and JavaScripts from an asset server
46 | # config.action_controller.asset_host = "http://assets.example.com"
47 |
48 | # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
49 | # config.assets.precompile += %w( search.js )
50 |
51 | # Disable delivery errors, bad email addresses will be ignored
52 | # config.action_mailer.raise_delivery_errors = false
53 |
54 | # Enable threaded mode
55 | # config.threadsafe!
56 |
57 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
58 | # the I18n.default_locale when a translation can not be found)
59 | config.i18n.fallbacks = true
60 |
61 | # Send deprecation notices to registered listeners
62 | config.active_support.deprecation = :notify
63 |
64 | # Log the query plan for queries taking more than this (works
65 | # with SQLite, MySQL, and PostgreSQL)
66 | # config.active_record.auto_explain_threshold_in_seconds = 0.5
67 | end
68 |
--------------------------------------------------------------------------------
/spec/dummy/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | # Pick the frameworks you want:
4 | require "action_controller/railtie"
5 | # require "action_mailer/railtie"
6 | # require "active_resource/railtie"
7 | # require "sprockets/railtie"
8 | # require "rails/test_unit/railtie"
9 |
10 | # Bundler.require
11 | require "stripe_event"
12 |
13 | module Dummy
14 | class Application < Rails::Application
15 | # Settings in config/environments/* take precedence over those specified here.
16 | # Application configuration should go into files in config/initializers
17 | # -- all .rb files in that directory are automatically loaded.
18 |
19 | # Custom directories with classes and modules you want to be autoloadable.
20 | # config.autoload_paths += %W(#{config.root}/extras)
21 |
22 | # Only load the plugins named here, in the order given (default is alphabetical).
23 | # :all can be used as a placeholder for all plugins not explicitly named.
24 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
25 |
26 | # Activate observers that should always be running.
27 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
28 |
29 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
30 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
31 | # config.time_zone = 'Central Time (US & Canada)'
32 |
33 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
34 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
35 | # config.i18n.default_locale = :de
36 |
37 | # Configure the default encoding used in templates for Ruby 1.9.
38 | config.encoding = "utf-8"
39 |
40 | # Configure sensitive parameters which will be filtered from the log file.
41 | config.filter_parameters += [:password]
42 |
43 | # Enable escaping HTML in JSON.
44 | config.active_support.escape_html_entities_in_json = true
45 |
46 | # Use SQL instead of Active Record's schema dumper when creating the database.
47 | # This is necessary if your schema can't be completely dumped by the schema dumper,
48 | # like if you have constraints or database-specific column types
49 | # config.active_record.schema_format = :sql
50 |
51 | # Enforce whitelist mode for mass assignment.
52 | # This will create an empty whitelist of attributes available for mass-assignment for all models
53 | # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
54 | # parameters by using an attr_accessible or attr_protected declaration.
55 | # config.active_record.whitelist_attributes = true
56 |
57 | # Enable the asset pipeline
58 | config.assets.enabled = true
59 |
60 | # Version of your assets, change this if you want to expire all your assets
61 | config.assets.version = '1.0'
62 | end
63 | end
64 |
65 |
--------------------------------------------------------------------------------
/gemfiles/rails4.0.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: ../
3 | specs:
4 | stripe_event (1.4.0)
5 | activesupport (>= 3.1)
6 | stripe (~> 1.6)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (4.0.11)
12 | actionpack (= 4.0.11)
13 | mail (~> 2.5, >= 2.5.4)
14 | actionpack (4.0.11)
15 | activesupport (= 4.0.11)
16 | builder (~> 3.1.0)
17 | erubis (~> 2.7.0)
18 | rack (~> 1.5.2)
19 | rack-test (~> 0.6.2)
20 | activemodel (4.0.11)
21 | activesupport (= 4.0.11)
22 | builder (~> 3.1.0)
23 | activerecord (4.0.11)
24 | activemodel (= 4.0.11)
25 | activerecord-deprecated_finders (~> 1.0.2)
26 | activesupport (= 4.0.11)
27 | arel (~> 4.0.0)
28 | activerecord-deprecated_finders (1.0.3)
29 | activesupport (4.0.11)
30 | i18n (~> 0.6, >= 0.6.9)
31 | minitest (~> 4.2)
32 | multi_json (~> 1.3)
33 | thread_safe (~> 0.1)
34 | tzinfo (~> 0.3.37)
35 | addressable (2.3.6)
36 | appraisal (1.0.2)
37 | bundler
38 | rake
39 | thor (>= 0.14.0)
40 | arel (4.0.2)
41 | builder (3.1.4)
42 | coveralls (0.7.1)
43 | multi_json (~> 1.3)
44 | rest-client
45 | simplecov (>= 0.7)
46 | term-ansicolor
47 | thor
48 | crack (0.4.2)
49 | safe_yaml (~> 1.0.0)
50 | diff-lcs (1.2.5)
51 | docile (1.1.5)
52 | erubis (2.7.0)
53 | hike (1.2.3)
54 | i18n (0.6.11)
55 | json (1.8.1)
56 | mail (2.6.1)
57 | mime-types (>= 1.16, < 3)
58 | mime-types (2.4.3)
59 | minitest (4.7.5)
60 | multi_json (1.10.1)
61 | netrc (0.8.0)
62 | rack (1.5.2)
63 | rack-test (0.6.2)
64 | rack (>= 1.0)
65 | rails (4.0.11)
66 | actionmailer (= 4.0.11)
67 | actionpack (= 4.0.11)
68 | activerecord (= 4.0.11)
69 | activesupport (= 4.0.11)
70 | bundler (>= 1.3.0, < 2.0)
71 | railties (= 4.0.11)
72 | sprockets-rails (~> 2.0)
73 | railties (4.0.11)
74 | actionpack (= 4.0.11)
75 | activesupport (= 4.0.11)
76 | rake (>= 0.8.7)
77 | thor (>= 0.18.1, < 2.0)
78 | rake (10.3.2)
79 | rest-client (1.7.2)
80 | mime-types (>= 1.16, < 3.0)
81 | netrc (~> 0.7)
82 | rspec-collection_matchers (1.0.0)
83 | rspec-expectations (>= 2.99.0.beta1)
84 | rspec-core (2.99.2)
85 | rspec-expectations (2.99.2)
86 | diff-lcs (>= 1.1.3, < 2.0)
87 | rspec-mocks (2.99.2)
88 | rspec-rails (2.99.0)
89 | actionpack (>= 3.0)
90 | activemodel (>= 3.0)
91 | activesupport (>= 3.0)
92 | railties (>= 3.0)
93 | rspec-collection_matchers
94 | rspec-core (~> 2.99.0)
95 | rspec-expectations (~> 2.99.0)
96 | rspec-mocks (~> 2.99.0)
97 | safe_yaml (1.0.4)
98 | simplecov (0.9.1)
99 | docile (~> 1.1.0)
100 | multi_json (~> 1.0)
101 | simplecov-html (~> 0.8.0)
102 | simplecov-html (0.8.0)
103 | sprockets (2.12.3)
104 | hike (~> 1.2)
105 | multi_json (~> 1.0)
106 | rack (~> 1.0)
107 | tilt (~> 1.1, != 1.3.0)
108 | sprockets-rails (2.2.0)
109 | actionpack (>= 3.0)
110 | activesupport (>= 3.0)
111 | sprockets (>= 2.8, < 4.0)
112 | stripe (1.16.0)
113 | json (~> 1.8.1)
114 | mime-types (>= 1.25, < 3.0)
115 | rest-client (~> 1.4)
116 | term-ansicolor (1.3.0)
117 | tins (~> 1.0)
118 | thor (0.19.1)
119 | thread_safe (0.3.4)
120 | tilt (1.4.1)
121 | tins (1.3.3)
122 | tzinfo (0.3.42)
123 | webmock (1.20.0)
124 | addressable (>= 2.3.6)
125 | crack (>= 0.3.2)
126 |
127 | PLATFORMS
128 | ruby
129 |
130 | DEPENDENCIES
131 | appraisal
132 | coveralls
133 | rails (~> 4.0.0)
134 | rspec-rails (~> 2.12)
135 | stripe_event!
136 | webmock (~> 1.9)
137 |
--------------------------------------------------------------------------------
/gemfiles/rails4.1.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: ../
3 | specs:
4 | stripe_event (1.4.0)
5 | activesupport (>= 3.1)
6 | stripe (~> 1.6)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (4.1.7)
12 | actionpack (= 4.1.7)
13 | actionview (= 4.1.7)
14 | mail (~> 2.5, >= 2.5.4)
15 | actionpack (4.1.7)
16 | actionview (= 4.1.7)
17 | activesupport (= 4.1.7)
18 | rack (~> 1.5.2)
19 | rack-test (~> 0.6.2)
20 | actionview (4.1.7)
21 | activesupport (= 4.1.7)
22 | builder (~> 3.1)
23 | erubis (~> 2.7.0)
24 | activemodel (4.1.7)
25 | activesupport (= 4.1.7)
26 | builder (~> 3.1)
27 | activerecord (4.1.7)
28 | activemodel (= 4.1.7)
29 | activesupport (= 4.1.7)
30 | arel (~> 5.0.0)
31 | activesupport (4.1.7)
32 | i18n (~> 0.6, >= 0.6.9)
33 | json (~> 1.7, >= 1.7.7)
34 | minitest (~> 5.1)
35 | thread_safe (~> 0.1)
36 | tzinfo (~> 1.1)
37 | addressable (2.3.6)
38 | appraisal (1.0.2)
39 | bundler
40 | rake
41 | thor (>= 0.14.0)
42 | arel (5.0.1.20140414130214)
43 | builder (3.2.2)
44 | coveralls (0.7.1)
45 | multi_json (~> 1.3)
46 | rest-client
47 | simplecov (>= 0.7)
48 | term-ansicolor
49 | thor
50 | crack (0.4.2)
51 | safe_yaml (~> 1.0.0)
52 | diff-lcs (1.2.5)
53 | docile (1.1.5)
54 | erubis (2.7.0)
55 | hike (1.2.3)
56 | i18n (0.6.11)
57 | json (1.8.1)
58 | mail (2.6.1)
59 | mime-types (>= 1.16, < 3)
60 | mime-types (2.4.3)
61 | minitest (5.4.2)
62 | multi_json (1.10.1)
63 | netrc (0.8.0)
64 | rack (1.5.2)
65 | rack-test (0.6.2)
66 | rack (>= 1.0)
67 | rails (4.1.7)
68 | actionmailer (= 4.1.7)
69 | actionpack (= 4.1.7)
70 | actionview (= 4.1.7)
71 | activemodel (= 4.1.7)
72 | activerecord (= 4.1.7)
73 | activesupport (= 4.1.7)
74 | bundler (>= 1.3.0, < 2.0)
75 | railties (= 4.1.7)
76 | sprockets-rails (~> 2.0)
77 | railties (4.1.7)
78 | actionpack (= 4.1.7)
79 | activesupport (= 4.1.7)
80 | rake (>= 0.8.7)
81 | thor (>= 0.18.1, < 2.0)
82 | rake (10.3.2)
83 | rest-client (1.7.2)
84 | mime-types (>= 1.16, < 3.0)
85 | netrc (~> 0.7)
86 | rspec-collection_matchers (1.0.0)
87 | rspec-expectations (>= 2.99.0.beta1)
88 | rspec-core (2.99.2)
89 | rspec-expectations (2.99.2)
90 | diff-lcs (>= 1.1.3, < 2.0)
91 | rspec-mocks (2.99.2)
92 | rspec-rails (2.99.0)
93 | actionpack (>= 3.0)
94 | activemodel (>= 3.0)
95 | activesupport (>= 3.0)
96 | railties (>= 3.0)
97 | rspec-collection_matchers
98 | rspec-core (~> 2.99.0)
99 | rspec-expectations (~> 2.99.0)
100 | rspec-mocks (~> 2.99.0)
101 | safe_yaml (1.0.4)
102 | simplecov (0.9.1)
103 | docile (~> 1.1.0)
104 | multi_json (~> 1.0)
105 | simplecov-html (~> 0.8.0)
106 | simplecov-html (0.8.0)
107 | sprockets (2.12.3)
108 | hike (~> 1.2)
109 | multi_json (~> 1.0)
110 | rack (~> 1.0)
111 | tilt (~> 1.1, != 1.3.0)
112 | sprockets-rails (2.2.0)
113 | actionpack (>= 3.0)
114 | activesupport (>= 3.0)
115 | sprockets (>= 2.8, < 4.0)
116 | stripe (1.16.0)
117 | json (~> 1.8.1)
118 | mime-types (>= 1.25, < 3.0)
119 | rest-client (~> 1.4)
120 | term-ansicolor (1.3.0)
121 | tins (~> 1.0)
122 | thor (0.19.1)
123 | thread_safe (0.3.4)
124 | tilt (1.4.1)
125 | tins (1.3.3)
126 | tzinfo (1.2.2)
127 | thread_safe (~> 0.1)
128 | webmock (1.20.0)
129 | addressable (>= 2.3.6)
130 | crack (>= 0.3.2)
131 |
132 | PLATFORMS
133 | ruby
134 |
135 | DEPENDENCIES
136 | appraisal
137 | coveralls
138 | rails (~> 4.1.0)
139 | rspec-rails (~> 2.12)
140 | stripe_event!
141 | webmock (~> 1.9)
142 |
--------------------------------------------------------------------------------
/gemfiles/rails3.2.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: ../
3 | specs:
4 | stripe_event (1.4.0)
5 | activesupport (>= 3.1)
6 | stripe (~> 1.6)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (3.2.20)
12 | actionpack (= 3.2.20)
13 | mail (~> 2.5.4)
14 | actionpack (3.2.20)
15 | activemodel (= 3.2.20)
16 | activesupport (= 3.2.20)
17 | builder (~> 3.0.0)
18 | erubis (~> 2.7.0)
19 | journey (~> 1.0.4)
20 | rack (~> 1.4.5)
21 | rack-cache (~> 1.2)
22 | rack-test (~> 0.6.1)
23 | sprockets (~> 2.2.1)
24 | activemodel (3.2.20)
25 | activesupport (= 3.2.20)
26 | builder (~> 3.0.0)
27 | activerecord (3.2.20)
28 | activemodel (= 3.2.20)
29 | activesupport (= 3.2.20)
30 | arel (~> 3.0.2)
31 | tzinfo (~> 0.3.29)
32 | activeresource (3.2.20)
33 | activemodel (= 3.2.20)
34 | activesupport (= 3.2.20)
35 | activesupport (3.2.20)
36 | i18n (~> 0.6, >= 0.6.4)
37 | multi_json (~> 1.0)
38 | addressable (2.3.6)
39 | appraisal (1.0.2)
40 | bundler
41 | rake
42 | thor (>= 0.14.0)
43 | arel (3.0.3)
44 | builder (3.0.4)
45 | coveralls (0.7.1)
46 | multi_json (~> 1.3)
47 | rest-client
48 | simplecov (>= 0.7)
49 | term-ansicolor
50 | thor
51 | crack (0.4.2)
52 | safe_yaml (~> 1.0.0)
53 | diff-lcs (1.2.5)
54 | docile (1.1.5)
55 | erubis (2.7.0)
56 | hike (1.2.3)
57 | i18n (0.6.11)
58 | journey (1.0.4)
59 | json (1.8.1)
60 | mail (2.5.4)
61 | mime-types (~> 1.16)
62 | treetop (~> 1.4.8)
63 | mime-types (1.25.1)
64 | multi_json (1.10.1)
65 | netrc (0.8.0)
66 | polyglot (0.3.5)
67 | rack (1.4.5)
68 | rack-cache (1.2)
69 | rack (>= 0.4)
70 | rack-ssl (1.3.4)
71 | rack
72 | rack-test (0.6.2)
73 | rack (>= 1.0)
74 | rails (3.2.20)
75 | actionmailer (= 3.2.20)
76 | actionpack (= 3.2.20)
77 | activerecord (= 3.2.20)
78 | activeresource (= 3.2.20)
79 | activesupport (= 3.2.20)
80 | bundler (~> 1.0)
81 | railties (= 3.2.20)
82 | railties (3.2.20)
83 | actionpack (= 3.2.20)
84 | activesupport (= 3.2.20)
85 | rack-ssl (~> 1.3.2)
86 | rake (>= 0.8.7)
87 | rdoc (~> 3.4)
88 | thor (>= 0.14.6, < 2.0)
89 | rake (10.3.2)
90 | rdoc (3.12.2)
91 | json (~> 1.4)
92 | rest-client (1.7.2)
93 | mime-types (>= 1.16, < 3.0)
94 | netrc (~> 0.7)
95 | rspec-collection_matchers (1.0.0)
96 | rspec-expectations (>= 2.99.0.beta1)
97 | rspec-core (2.99.2)
98 | rspec-expectations (2.99.2)
99 | diff-lcs (>= 1.1.3, < 2.0)
100 | rspec-mocks (2.99.2)
101 | rspec-rails (2.99.0)
102 | actionpack (>= 3.0)
103 | activemodel (>= 3.0)
104 | activesupport (>= 3.0)
105 | railties (>= 3.0)
106 | rspec-collection_matchers
107 | rspec-core (~> 2.99.0)
108 | rspec-expectations (~> 2.99.0)
109 | rspec-mocks (~> 2.99.0)
110 | safe_yaml (1.0.4)
111 | simplecov (0.9.1)
112 | docile (~> 1.1.0)
113 | multi_json (~> 1.0)
114 | simplecov-html (~> 0.8.0)
115 | simplecov-html (0.8.0)
116 | sprockets (2.2.3)
117 | hike (~> 1.2)
118 | multi_json (~> 1.0)
119 | rack (~> 1.0)
120 | tilt (~> 1.1, != 1.3.0)
121 | stripe (1.16.0)
122 | json (~> 1.8.1)
123 | mime-types (>= 1.25, < 3.0)
124 | rest-client (~> 1.4)
125 | term-ansicolor (1.3.0)
126 | tins (~> 1.0)
127 | thor (0.19.1)
128 | tilt (1.4.1)
129 | tins (1.3.3)
130 | treetop (1.4.15)
131 | polyglot
132 | polyglot (>= 0.3.1)
133 | tzinfo (0.3.42)
134 | webmock (1.20.0)
135 | addressable (>= 2.3.6)
136 | crack (>= 0.3.2)
137 |
138 | PLATFORMS
139 | ruby
140 |
141 | DEPENDENCIES
142 | appraisal
143 | coveralls
144 | rails (~> 3.2.0)
145 | rspec-rails (~> 2.12)
146 | stripe_event!
147 | webmock (~> 1.9)
148 |
--------------------------------------------------------------------------------
/gemfiles/rails3.1.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: ../
3 | specs:
4 | stripe_event (1.4.0)
5 | activesupport (>= 3.1)
6 | stripe (~> 1.6)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (3.1.12)
12 | actionpack (= 3.1.12)
13 | mail (~> 2.4.4)
14 | actionpack (3.1.12)
15 | activemodel (= 3.1.12)
16 | activesupport (= 3.1.12)
17 | builder (~> 3.0.0)
18 | erubis (~> 2.7.0)
19 | i18n (~> 0.6)
20 | rack (~> 1.3.6)
21 | rack-cache (~> 1.2)
22 | rack-mount (~> 0.8.2)
23 | rack-test (~> 0.6.1)
24 | sprockets (~> 2.0.4)
25 | activemodel (3.1.12)
26 | activesupport (= 3.1.12)
27 | builder (~> 3.0.0)
28 | i18n (~> 0.6)
29 | activerecord (3.1.12)
30 | activemodel (= 3.1.12)
31 | activesupport (= 3.1.12)
32 | arel (~> 2.2.3)
33 | tzinfo (~> 0.3.29)
34 | activeresource (3.1.12)
35 | activemodel (= 3.1.12)
36 | activesupport (= 3.1.12)
37 | activesupport (3.1.12)
38 | multi_json (~> 1.0)
39 | addressable (2.3.6)
40 | appraisal (1.0.2)
41 | bundler
42 | rake
43 | thor (>= 0.14.0)
44 | arel (2.2.3)
45 | builder (3.0.4)
46 | coveralls (0.7.1)
47 | multi_json (~> 1.3)
48 | rest-client
49 | simplecov (>= 0.7)
50 | term-ansicolor
51 | thor
52 | crack (0.4.2)
53 | safe_yaml (~> 1.0.0)
54 | diff-lcs (1.2.5)
55 | docile (1.1.5)
56 | erubis (2.7.0)
57 | hike (1.2.3)
58 | i18n (0.6.11)
59 | json (1.8.1)
60 | mail (2.4.4)
61 | i18n (>= 0.4.0)
62 | mime-types (~> 1.16)
63 | treetop (~> 1.4.8)
64 | mime-types (1.25.1)
65 | multi_json (1.10.1)
66 | netrc (0.8.0)
67 | polyglot (0.3.5)
68 | rack (1.3.10)
69 | rack-cache (1.2)
70 | rack (>= 0.4)
71 | rack-mount (0.8.3)
72 | rack (>= 1.0.0)
73 | rack-ssl (1.3.4)
74 | rack
75 | rack-test (0.6.2)
76 | rack (>= 1.0)
77 | rails (3.1.12)
78 | actionmailer (= 3.1.12)
79 | actionpack (= 3.1.12)
80 | activerecord (= 3.1.12)
81 | activeresource (= 3.1.12)
82 | activesupport (= 3.1.12)
83 | bundler (~> 1.0)
84 | railties (= 3.1.12)
85 | railties (3.1.12)
86 | actionpack (= 3.1.12)
87 | activesupport (= 3.1.12)
88 | rack-ssl (~> 1.3.2)
89 | rake (>= 0.8.7)
90 | rdoc (~> 3.4)
91 | thor (~> 0.14.6)
92 | rake (10.3.2)
93 | rdoc (3.12.2)
94 | json (~> 1.4)
95 | rest-client (1.7.2)
96 | mime-types (>= 1.16, < 3.0)
97 | netrc (~> 0.7)
98 | rspec-collection_matchers (1.0.0)
99 | rspec-expectations (>= 2.99.0.beta1)
100 | rspec-core (2.99.2)
101 | rspec-expectations (2.99.2)
102 | diff-lcs (>= 1.1.3, < 2.0)
103 | rspec-mocks (2.99.2)
104 | rspec-rails (2.99.0)
105 | actionpack (>= 3.0)
106 | activemodel (>= 3.0)
107 | activesupport (>= 3.0)
108 | railties (>= 3.0)
109 | rspec-collection_matchers
110 | rspec-core (~> 2.99.0)
111 | rspec-expectations (~> 2.99.0)
112 | rspec-mocks (~> 2.99.0)
113 | safe_yaml (1.0.4)
114 | simplecov (0.9.1)
115 | docile (~> 1.1.0)
116 | multi_json (~> 1.0)
117 | simplecov-html (~> 0.8.0)
118 | simplecov-html (0.8.0)
119 | sprockets (2.0.5)
120 | hike (~> 1.2)
121 | rack (~> 1.0)
122 | tilt (~> 1.1, != 1.3.0)
123 | stripe (1.16.0)
124 | json (~> 1.8.1)
125 | mime-types (>= 1.25, < 3.0)
126 | rest-client (~> 1.4)
127 | term-ansicolor (1.3.0)
128 | tins (~> 1.0)
129 | thor (0.14.6)
130 | tilt (1.4.1)
131 | tins (1.3.3)
132 | treetop (1.4.15)
133 | polyglot
134 | polyglot (>= 0.3.1)
135 | tzinfo (0.3.42)
136 | webmock (1.20.0)
137 | addressable (>= 2.3.6)
138 | crack (>= 0.3.2)
139 |
140 | PLATFORMS
141 | ruby
142 |
143 | DEPENDENCIES
144 | appraisal
145 | coveralls
146 | rails (~> 3.1.0)
147 | rspec-rails (~> 2.12)
148 | stripe_event!
149 | webmock (~> 1.9)
150 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ### 1.4.0 (November 1, 2014)
2 | * Add `StripeEvent.listening?` method to easily determine if an event type has any registered handlers. Thank you to [Vladimir Andrijevik](https://github.com/vandrijevik) for the [idea and implementation](https://github.com/integrallis/stripe_event/pull/42).
3 |
4 | ### 1.3.0 (July 22, 2014)
5 | * Allow for ignoring particular events. Thank you to [anark](https://github.com/anark) for suggesting the change, and [Ryan McGeary](https://github.com/rmm5t) and [Pete Keen](https://github.com/peterkeen) for working on the implementation.
6 |
7 | ### 1.2.0 (June 17, 2014)
8 | * Gracefully authenticate `account.application.deauthorized` events. Thank you to [Ryan McGeary](https://github.com/rmm5t) for the pull request and for taking the time to test the change in a live environment.
9 |
10 | ### 1.1.0 (January 8, 2014)
11 | * Deprecate `StripeEvent.setup` in favor of `StripeEvent.configure`. Remove `setup` at next major release.
12 | * `StripeEvent.configure` yields the module to the block for configuration.
13 | * `StripeEvent.configure` will raise `ArgumentError` unless a block is given.
14 | * Track test coverage
15 |
16 | ### 1.0.0 (December 19, 2013)
17 | * Internally namespace dispatched events to avoid maintaining a list of all possible event types.
18 | * Subscribe to all event types with `StripeEvent.all` instead of `StripeEvent.subscribe`.
19 | * Remove ability to subscribe to many event types with once call to `StripeEvent.subscribe`.
20 | * Subscribers can be an object that responds to #call.
21 | * Allow subscriber-generated `Stripe::StripeError`'s to bubble up. Thank you to [adamonduty](https://github.com/adamonduty) for the [patch](https://github.com/integrallis/stripe_event/pull/26).
22 | * Only depend on `stripe` and `activesupport` gems.
23 | * Add `rails` as a development dependency.
24 | * Only `require 'stripe_event/engine'` if `Rails` constant exists to allow StripeEvent to be used outside of a Rails application.
25 |
26 | ### 0.6.1 (August 19, 2013)
27 | * Update event type list
28 | * Update test gemfiles
29 |
30 | ### 0.6.0 (March 18, 2013)
31 | * Rails 4 compatibility. Thank you to Ben Ubois for reporting the [issue](https://github.com/integrallis/stripe_event/issues/13) and to Matt Goldman for the [pull request](https://github.com/integrallis/stripe_event/pull/14).
32 | * Run specs against different Rails versions
33 | * Refactor internal usage of AS::Notifications
34 | * Remove jruby-openssl as platform conditional dependency
35 |
36 | ### 0.5.0 (December 16, 2012)
37 | * Remove `Gemfile.lock` from version control
38 | * Internal event type list is now a set
39 | * Update event type list
40 | * Various internal refactorings
41 | * More readable tests
42 |
43 | ### 0.4.0 (September 24, 2012)
44 | * Add configuration for custom event retrieval. Thanks to Dan Hodos for the [pull request](https://github.com/integrallis/stripe_event/pull/6).
45 | * Move module methods only used in tests into a test helper.
46 | * Various internal refactorings and additional tests.
47 | * Error classes will inherit from a base error class now.
48 |
49 | ### 0.3.1 (August 14, 2012)
50 | * Fix controller inheritance issue. Thanks to Christopher Baran for [reporting the bug](https://github.com/integrallis/stripe_event/issues/1), and to Robert Bousquet for [fixing it](https://github.com/integrallis/stripe_event/pull/3).
51 | * Deprecate registration method. Use 'setup' instead.
52 |
53 | ### 0.3.0 (July 16, 2012)
54 | * Add registration method for conveniently adding many subscribers
55 | * Depend on jruby-openssl when running on jruby
56 | * Remove unneeded rake dependency
57 | * Remove configure method
58 |
59 | ### 0.2.0 (July 12, 2012)
60 | * Register a subscriber to one/many/all events
61 | * Remove sqlite3 development dependency
62 | * Setup travis-ci for repo
63 | * Hard code a placeholder api key in dummy app. Fixes failing tests when env var not defined.
64 |
65 | ### 0.1.1 (July 4, 2012)
66 | * Improve README
67 | * Specify development dependency versions
68 | * Fix controller test which was passing incorrectly
69 |
70 | ### 0.1.0 (June 24, 2012)
71 | * Initial release
72 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # StripeEvent
2 | [](http://travis-ci.org/integrallis/stripe_event) [](https://gemnasium.com/integrallis/stripe_event) [](http://badge.fury.io/rb/stripe_event) [](https://codeclimate.com/github/integrallis/stripe_event) [](https://coveralls.io/r/integrallis/stripe_event)
3 |
4 | StripeEvent is built on the [ActiveSupport::Notifications API](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html). Incoming webhook requests are authenticated by [retrieving the event object](https://stripe.com/docs/api?lang=ruby#retrieve_event) from Stripe. Define subscribers to handle specific event types. Subscribers can be a block or an object that responds to `#call`.
5 |
6 | ## Install
7 |
8 | ```ruby
9 | # Gemfile
10 | gem 'stripe_event'
11 | ```
12 |
13 | ```ruby
14 | # config/routes.rb
15 | mount StripeEvent::Engine, at: '/my-chosen-path' # provide a custom path
16 | ```
17 |
18 | ## Usage
19 |
20 | ```ruby
21 | # config/initializers/stripe.rb
22 | Stripe.api_key = ENV['STRIPE_API_KEY'] # Set your api key
23 |
24 | StripeEvent.configure do |events|
25 | events.subscribe 'charge.failed' do |event|
26 | # Define subscriber behavior based on the event object
27 | event.class #=> Stripe::Event
28 | event.type #=> "charge.failed"
29 | event.data.object #=> #
30 | end
31 |
32 | events.all do |event|
33 | # Handle all event types - logging, etc.
34 | end
35 | end
36 | ```
37 |
38 | ### Subscriber objects that respond to #call
39 |
40 | ```ruby
41 | class CustomerCreated
42 | def call(event)
43 | # Event handling
44 | end
45 | end
46 |
47 | class BillingEventLogger
48 | def initialize(logger)
49 | @logger = logger
50 | end
51 |
52 | def call(event)
53 | @logger.info "BILLING:#{event.type}:#{event.id}"
54 | end
55 | end
56 | ```
57 |
58 | ```ruby
59 | StripeEvent.configure do |events|
60 | events.all BillingEventLogger.new(Rails.logger)
61 | events.subscribe 'customer.created', CustomerCreated.new
62 | end
63 | ```
64 |
65 | ### Subscribing to a namespace of event types
66 |
67 | ```ruby
68 | StripeEvent.subscribe 'customer.card.' do |event|
69 | # Will be triggered for any customer.card.* events
70 | end
71 | ```
72 |
73 | ## Configuration
74 |
75 | If you have built an application that has multiple Stripe accounts--say, each of your customers has their own--you may want to define your own way of retrieving events from Stripe (e.g. perhaps you want to use the [user_id parameter](https://stripe.com/docs/apps/getting-started#webhooks) from the top level to detect the customer for the event, then grab their specific API key). You can do this:
76 |
77 | ```ruby
78 | StripeEvent.event_retriever = lambda do |params|
79 | api_key = Account.find_by!(stripe_user_id: params[:user_id]).api_key
80 | Stripe::Event.retrieve(params[:id], api_key)
81 | end
82 | ```
83 |
84 | ```ruby
85 | class EventRetriever
86 | def call(params)
87 | api_key = retrieve_api_key(params[:user_id])
88 | Stripe::Event.retrieve(params[:id], api_key)
89 | end
90 |
91 | def retrieve_api_key(stripe_user_id)
92 | Account.find_by!(stripe_user_id: stripe_user_id).api_key
93 | rescue ActiveRecord::RecordNotFound
94 | # whoops something went wrong - error handling
95 | end
96 | end
97 |
98 | StripeEvent.event_retriever = EventRetriever.new
99 | ```
100 |
101 | If you'd like to ignore particular webhook events (perhaps to ignore test webhooks in production, or to ignore webhooks for a non-paying customer), you can do so by returning `nil` in you custom `event_retriever`. For example:
102 |
103 | ```ruby
104 | StripeEvent.event_retriever = lambda do |params|
105 | return nil if Rails.env.production? && !params[:livemode]
106 | Stripe::Event.retrieve(params[:id])
107 | end
108 | ```
109 |
110 | ```ruby
111 | StripeEvent.event_retriever = lambda do |params|
112 | account = Account.find_by!(stripe_user_id: params[:user_id])
113 | return nil if account.delinquent?
114 | Stripe::Event.retrieve(params[:id], account.api_key)
115 | end
116 | ```
117 |
118 | ## Without Rails
119 |
120 | StripeEvent can be used outside of Rails applications as well. Here is a basic Sinatra implementation:
121 |
122 | ```ruby
123 | require 'json'
124 | require 'sinatra'
125 | require 'stripe_event'
126 |
127 | Stripe.api_key = ENV['STRIPE_API_KEY']
128 |
129 | StripeEvent.subscribe 'charge.failed' do |event|
130 | # Look ma, no Rails!
131 | end
132 |
133 | post '/_billing_events' do
134 | data = JSON.parse(request.body.read, symbolize_names: true)
135 | StripeEvent.instrument(data)
136 | 200
137 | end
138 | ```
139 |
140 | ## Testing
141 |
142 | Handling webhooks is a critical piece of modern billing systems. Verifying the behavior of StripeEvent subscribers can be done fairly easily by stubbing out the HTTP request used to authenticate the webhook request. Tools like [Webmock](https://github.com/bblimke/webmock) and [VCR](https://github.com/vcr/vcr) work well. [RequestBin](http://requestb.in/) is great for collecting the payloads. For exploratory phases of development, [UltraHook](http://www.ultrahook.com/) and other tools can forward webhook requests directly to localhost. You can check out [test-hooks](https://github.com/invisiblefunnel/test-hooks), an example Rails application to see how to test StripeEvent subscribers with RSpec request specs and Webmock. A quick look:
143 |
144 | ```ruby
145 | # spec/requests/billing_events_spec.rb
146 | require 'spec_helper'
147 |
148 | describe "Billing Events" do
149 | def stub_event(fixture_id, status = 200)
150 | stub_request(:get, "https://api.stripe.com/v1/events/#{fixture_id}").
151 | to_return(status: status, body: File.read("spec/support/fixtures/#{fixture_id}.json"))
152 | end
153 |
154 | describe "customer.created" do
155 | before do
156 | stub_event 'evt_customer_created'
157 | end
158 |
159 | it "is successful" do
160 | post '/_billing_events', id: 'evt_customer_created'
161 | expect(response.code).to eq "200"
162 | # Additional expectations...
163 | end
164 | end
165 | end
166 | ```
167 |
168 | ### Note: 'Test Webhooks' Button on Stripe Dashboard
169 |
170 | This button sends an example event to your webhook urls, including an `id` of `evt_00000000000000`. To confirm that Stripe sent the webhook, StripeEvent attempts to retrieve the event details from Stripe using the given `id`. In this case the event does not exist and StripeEvent responds with `401 Unauthorized`. Instead of using the 'Test Webhooks' button, trigger webhooks by using the Stripe API or Dashboard to create test payments, customers, etc.
171 |
172 | ### Maintainers
173 |
174 | * [Ryan McGeary](https://github.com/rmm5t)
175 | * [Pete Keen](https://github.com/peterkeen)
176 | * [Danny Whalen](https://github.com/invisiblefunnel)
177 |
178 | Special thanks to all the [contributors](https://github.com/integrallis/stripe_event/graphs/contributors).
179 |
180 | ### License
181 |
182 | [MIT License](https://github.com/integrallis/stripe_event/blob/master/LICENSE.md). Copyright 2012-2014 Integrallis Software.
183 |
--------------------------------------------------------------------------------
/spec/lib/stripe_event_spec.rb:
--------------------------------------------------------------------------------
1 | require 'spec_helper'
2 |
3 | describe StripeEvent do
4 | let(:events) { [] }
5 | let(:subscriber) { ->(evt){ events << evt } }
6 | let(:charge_succeeded) { double('charge succeeded') }
7 | let(:charge_failed) { double('charge failed') }
8 |
9 | describe ".configure" do
10 | it "yields itself to the block" do
11 | yielded = nil
12 | StripeEvent.configure { |events| yielded = events }
13 | expect(yielded).to eq StripeEvent
14 | end
15 |
16 | it "requires a block argument" do
17 | expect { StripeEvent.configure }.to raise_error ArgumentError
18 | end
19 |
20 | describe ".setup - deprecated" do
21 | it "evaluates the block in its own context" do
22 | ctx = nil
23 | StripeEvent.setup { ctx = self }
24 | expect(ctx).to eq StripeEvent
25 | end
26 | end
27 | end
28 |
29 | describe "subscribing to a specific event type" do
30 | before do
31 | expect(charge_succeeded).to receive(:[]).with(:type).and_return('charge.succeeded')
32 | expect(Stripe::Event).to receive(:retrieve).with('evt_charge_succeeded').and_return(charge_succeeded)
33 | end
34 |
35 | context "with a block subscriber" do
36 | it "calls the subscriber with the retrieved event" do
37 | StripeEvent.subscribe('charge.succeeded', &subscriber)
38 |
39 | StripeEvent.instrument(id: 'evt_charge_succeeded', type: 'charge.succeeded')
40 |
41 | expect(events).to eq [charge_succeeded]
42 | end
43 | end
44 |
45 | context "with a subscriber that responds to #call" do
46 | it "calls the subscriber with the retrieved event" do
47 | StripeEvent.subscribe('charge.succeeded', subscriber)
48 |
49 | StripeEvent.instrument(id: 'evt_charge_succeeded', type: 'charge.succeeded')
50 |
51 | expect(events).to eq [charge_succeeded]
52 | end
53 | end
54 | end
55 |
56 | describe "subscribing to the 'account.application.deauthorized' event type" do
57 | before do
58 | expect(Stripe::Event).to receive(:retrieve).with('evt_account_application_deauthorized').and_raise(Stripe::AuthenticationError)
59 | end
60 |
61 | context "with a subscriber params with symbolized keys" do
62 | it "calls the subscriber with the retrieved event" do
63 | StripeEvent.subscribe('account.application.deauthorized', subscriber)
64 |
65 | StripeEvent.instrument(id: 'evt_account_application_deauthorized', type: 'account.application.deauthorized')
66 |
67 | expect(events.first.type).to eq 'account.application.deauthorized'
68 | expect(events.first[:type]).to eq 'account.application.deauthorized'
69 | end
70 | end
71 |
72 | # The Stripe api expects params to be passed into their StripeObject's
73 | # with symbolized keys, but the params that we pass through from a
74 | # accont.application.deauthorized webhook are a HashWithIndifferentAccess
75 | # (keys stored as strings always.
76 | context "with a subscriber params with indifferent access (stringified keys)" do
77 | it "calls the subscriber with the retrieved event" do
78 | StripeEvent.subscribe('account.application.deauthorized', subscriber)
79 |
80 | StripeEvent.instrument({ id: 'evt_account_application_deauthorized', type: 'account.application.deauthorized' }.with_indifferent_access)
81 |
82 | expect(events.first.type).to eq 'account.application.deauthorized'
83 | expect(events.first[:type]).to eq 'account.application.deauthorized'
84 | end
85 | end
86 | end
87 |
88 | describe "subscribing to a namespace of event types" do
89 | let(:card_created) { double('card created') }
90 | let(:card_updated) { double('card updated') }
91 |
92 | before do
93 | expect(card_created).to receive(:[]).with(:type).and_return('customer.card.created')
94 | expect(Stripe::Event).to receive(:retrieve).with('evt_card_created').and_return(card_created)
95 |
96 | expect(card_updated).to receive(:[]).with(:type).and_return('customer.card.updated')
97 | expect(Stripe::Event).to receive(:retrieve).with('evt_card_updated').and_return(card_updated)
98 | end
99 |
100 | context "with a block subscriber" do
101 | it "calls the subscriber with any events in the namespace" do
102 | StripeEvent.subscribe('customer.card', &subscriber)
103 |
104 | StripeEvent.instrument(id: 'evt_card_created', type: 'customer.card.created')
105 | StripeEvent.instrument(id: 'evt_card_updated', type: 'customer.card.updated')
106 |
107 | expect(events).to eq [card_created, card_updated]
108 | end
109 | end
110 |
111 | context "with a subscriber that responds to #call" do
112 | it "calls the subscriber with any events in the namespace" do
113 | StripeEvent.subscribe('customer.card.', subscriber)
114 |
115 | StripeEvent.instrument(id: 'evt_card_updated', type: 'customer.card.updated')
116 | StripeEvent.instrument(id: 'evt_card_created', type: 'customer.card.created')
117 |
118 | expect(events).to eq [card_updated, card_created]
119 | end
120 | end
121 | end
122 |
123 | describe "subscribing to all event types" do
124 | before do
125 | expect(charge_succeeded).to receive(:[]).with(:type).and_return('charge.succeeded')
126 | expect(Stripe::Event).to receive(:retrieve).with('evt_charge_succeeded').and_return(charge_succeeded)
127 |
128 | expect(charge_failed).to receive(:[]).with(:type).and_return('charge.failed')
129 | expect(Stripe::Event).to receive(:retrieve).with('evt_charge_failed').and_return(charge_failed)
130 | end
131 |
132 | context "with a block subscriber" do
133 | it "calls the subscriber with all retrieved events" do
134 | StripeEvent.all(&subscriber)
135 |
136 | StripeEvent.instrument(id: 'evt_charge_succeeded', type: 'charge.succeeded')
137 | StripeEvent.instrument(id: 'evt_charge_failed', type: 'charge.failed')
138 |
139 | expect(events).to eq [charge_succeeded, charge_failed]
140 | end
141 | end
142 |
143 | context "with a subscriber that responds to #call" do
144 | it "calls the subscriber with all retrieved events" do
145 | StripeEvent.all(subscriber)
146 |
147 | StripeEvent.instrument(id: 'evt_charge_succeeded', type: 'charge.succeeded')
148 | StripeEvent.instrument(id: 'evt_charge_failed', type: 'charge.failed')
149 |
150 | expect(events).to eq [charge_succeeded, charge_failed]
151 | end
152 | end
153 | end
154 |
155 | describe ".listening?" do
156 | it "returns true when there is a subscriber for a matching event type" do
157 | StripeEvent.subscribe('customer.', &subscriber)
158 |
159 | expect(StripeEvent.listening?('customer.card')).to be true
160 | expect(StripeEvent.listening?('customer.')).to be true
161 | end
162 |
163 | it "returns false when there is not a subscriber for a matching event type" do
164 | StripeEvent.subscribe('customer.', &subscriber)
165 |
166 | expect(StripeEvent.listening?('account')).to be false
167 | end
168 |
169 | it "returns true when a subscriber is subscribed to all events" do
170 | StripeEvent.all(&subscriber)
171 |
172 | expect(StripeEvent.listening?('customer.')).to be true
173 | expect(StripeEvent.listening?('account')).to be true
174 | end
175 | end
176 |
177 | describe StripeEvent::NotificationAdapter do
178 | let(:adapter) { StripeEvent.adapter }
179 |
180 | it "calls the subscriber with the last argument" do
181 | expect(subscriber).to receive(:call).with(:last)
182 |
183 | adapter.call(subscriber).call(:first, :last)
184 | end
185 | end
186 |
187 | describe StripeEvent::Namespace do
188 | let(:namespace) { StripeEvent.namespace }
189 |
190 | describe "#call" do
191 | it "prepends the namespace to a given string" do
192 | expect(namespace.call('foo.bar')).to eq 'stripe_event.foo.bar'
193 | end
194 |
195 | it "returns the namespace given no arguments" do
196 | expect(namespace.call).to eq 'stripe_event.'
197 | end
198 | end
199 |
200 | describe "#to_regexp" do
201 | it "matches namespaced strings" do
202 | expect(namespace.to_regexp('foo.bar')).to match namespace.call('foo.bar')
203 | end
204 |
205 | it "matches all namespaced strings given no arguments" do
206 | expect(namespace.to_regexp).to match namespace.call('foo.bar')
207 | end
208 | end
209 | end
210 | end
211 |
--------------------------------------------------------------------------------
/spec/dummy/README.rdoc:
--------------------------------------------------------------------------------
1 | == Welcome to Rails
2 |
3 | Rails is a web-application framework that includes everything needed to create
4 | database-backed web applications according to the Model-View-Control pattern.
5 |
6 | This pattern splits the view (also called the presentation) into "dumb"
7 | templates that are primarily responsible for inserting pre-built data in between
8 | HTML tags. The model contains the "smart" domain objects (such as Account,
9 | Product, Person, Post) that holds all the business logic and knows how to
10 | persist themselves to a database. The controller handles the incoming requests
11 | (such as Save New Account, Update Product, Show Post) by manipulating the model
12 | and directing data to the view.
13 |
14 | In Rails, the model is handled by what's called an object-relational mapping
15 | layer entitled Active Record. This layer allows you to present the data from
16 | database rows as objects and embellish these data objects with business logic
17 | methods. You can read more about Active Record in
18 | link:files/vendor/rails/activerecord/README.html.
19 |
20 | The controller and view are handled by the Action Pack, which handles both
21 | layers by its two parts: Action View and Action Controller. These two layers
22 | are bundled in a single package due to their heavy interdependence. This is
23 | unlike the relationship between the Active Record and Action Pack that is much
24 | more separate. Each of these packages can be used independently outside of
25 | Rails. You can read more about Action Pack in
26 | link:files/vendor/rails/actionpack/README.html.
27 |
28 |
29 | == Getting Started
30 |
31 | 1. At the command prompt, create a new Rails application:
32 | rails new myapp (where myapp is the application name)
33 |
34 | 2. Change directory to myapp and start the web server:
35 | cd myapp; rails server (run with --help for options)
36 |
37 | 3. Go to http://localhost:3000/ and you'll see:
38 | "Welcome aboard: You're riding Ruby on Rails!"
39 |
40 | 4. Follow the guidelines to start developing your application. You can find
41 | the following resources handy:
42 |
43 | * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44 | * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45 |
46 |
47 | == Debugging Rails
48 |
49 | Sometimes your application goes wrong. Fortunately there are a lot of tools that
50 | will help you debug it and get it back on the rails.
51 |
52 | First area to check is the application log files. Have "tail -f" commands
53 | running on the server.log and development.log. Rails will automatically display
54 | debugging and runtime information to these files. Debugging info will also be
55 | shown in the browser on requests from 127.0.0.1.
56 |
57 | You can also log your own messages directly into the log file from your code
58 | using the Ruby logger class from inside your controllers. Example:
59 |
60 | class WeblogController < ActionController::Base
61 | def destroy
62 | @weblog = Weblog.find(params[:id])
63 | @weblog.destroy
64 | logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65 | end
66 | end
67 |
68 | The result will be a message in your log file along the lines of:
69 |
70 | Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71 |
72 | More information on how to use the logger is at http://www.ruby-doc.org/core/
73 |
74 | Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75 | several books available online as well:
76 |
77 | * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78 | * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79 |
80 | These two books will bring you up to speed on the Ruby language and also on
81 | programming in general.
82 |
83 |
84 | == Debugger
85 |
86 | Debugger support is available through the debugger command when you start your
87 | Mongrel or WEBrick server with --debugger. This means that you can break out of
88 | execution at any point in the code, investigate and change the model, and then,
89 | resume execution! You need to install ruby-debug to run the server in debugging
90 | mode. With gems, use sudo gem install ruby-debug. Example:
91 |
92 | class WeblogController < ActionController::Base
93 | def index
94 | @posts = Post.all
95 | debugger
96 | end
97 | end
98 |
99 | So the controller will accept the action, run the first line, then present you
100 | with a IRB prompt in the server window. Here you can do things like:
101 |
102 | >> @posts.inspect
103 | => "[#nil, "body"=>nil, "id"=>"1"}>,
105 | #"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107 | >> @posts.first.title = "hello from a debugger"
108 | => "hello from a debugger"
109 |
110 | ...and even better, you can examine how your runtime objects actually work:
111 |
112 | >> f = @posts.first
113 | => #nil, "body"=>nil, "id"=>"1"}>
114 | >> f.
115 | Display all 152 possibilities? (y or n)
116 |
117 | Finally, when you're ready to resume execution, you can enter "cont".
118 |
119 |
120 | == Console
121 |
122 | The console is a Ruby shell, which allows you to interact with your
123 | application's domain model. Here you'll have all parts of the application
124 | configured, just like it is when the application is running. You can inspect
125 | domain models, change values, and save to the database. Starting the script
126 | without arguments will launch it in the development environment.
127 |
128 | To start the console, run rails console from the application
129 | directory.
130 |
131 | Options:
132 |
133 | * Passing the -s, --sandbox argument will rollback any modifications
134 | made to the database.
135 | * Passing an environment name as an argument will load the corresponding
136 | environment. Example: rails console production.
137 |
138 | To reload your controllers and models after launching the console run
139 | reload!
140 |
141 | More information about irb can be found at:
142 | link:http://www.rubycentral.org/pickaxe/irb.html
143 |
144 |
145 | == dbconsole
146 |
147 | You can go to the command line of your database directly through rails
148 | dbconsole. You would be connected to the database with the credentials
149 | defined in database.yml. Starting the script without arguments will connect you
150 | to the development database. Passing an argument will connect you to a different
151 | database, like rails dbconsole production. Currently works for MySQL,
152 | PostgreSQL and SQLite 3.
153 |
154 | == Description of Contents
155 |
156 | The default directory structure of a generated Ruby on Rails application:
157 |
158 | |-- app
159 | | |-- assets
160 | | |-- images
161 | | |-- javascripts
162 | | `-- stylesheets
163 | | |-- controllers
164 | | |-- helpers
165 | | |-- mailers
166 | | |-- models
167 | | `-- views
168 | | `-- layouts
169 | |-- config
170 | | |-- environments
171 | | |-- initializers
172 | | `-- locales
173 | |-- db
174 | |-- doc
175 | |-- lib
176 | | `-- tasks
177 | |-- log
178 | |-- public
179 | |-- script
180 | |-- test
181 | | |-- fixtures
182 | | |-- functional
183 | | |-- integration
184 | | |-- performance
185 | | `-- unit
186 | |-- tmp
187 | | |-- cache
188 | | |-- pids
189 | | |-- sessions
190 | | `-- sockets
191 | `-- vendor
192 | |-- assets
193 | `-- stylesheets
194 | `-- plugins
195 |
196 | app
197 | Holds all the code that's specific to this particular application.
198 |
199 | app/assets
200 | Contains subdirectories for images, stylesheets, and JavaScript files.
201 |
202 | app/controllers
203 | Holds controllers that should be named like weblogs_controller.rb for
204 | automated URL mapping. All controllers should descend from
205 | ApplicationController which itself descends from ActionController::Base.
206 |
207 | app/models
208 | Holds models that should be named like post.rb. Models descend from
209 | ActiveRecord::Base by default.
210 |
211 | app/views
212 | Holds the template files for the view that should be named like
213 | weblogs/index.html.erb for the WeblogsController#index action. All views use
214 | eRuby syntax by default.
215 |
216 | app/views/layouts
217 | Holds the template files for layouts to be used with views. This models the
218 | common header/footer method of wrapping views. In your views, define a layout
219 | using the layout :default and create a file named default.html.erb.
220 | Inside default.html.erb, call <% yield %> to render the view using this
221 | layout.
222 |
223 | app/helpers
224 | Holds view helpers that should be named like weblogs_helper.rb. These are
225 | generated for you automatically when using generators for controllers.
226 | Helpers can be used to wrap functionality for your views into methods.
227 |
228 | config
229 | Configuration files for the Rails environment, the routing map, the database,
230 | and other dependencies.
231 |
232 | db
233 | Contains the database schema in schema.rb. db/migrate contains all the
234 | sequence of Migrations for your schema.
235 |
236 | doc
237 | This directory is where your application documentation will be stored when
238 | generated using rake doc:app
239 |
240 | lib
241 | Application specific libraries. Basically, any kind of custom code that
242 | doesn't belong under controllers, models, or helpers. This directory is in
243 | the load path.
244 |
245 | public
246 | The directory available for the web server. Also contains the dispatchers and the
247 | default HTML files. This should be set as the DOCUMENT_ROOT of your web
248 | server.
249 |
250 | script
251 | Helper scripts for automation and generation.
252 |
253 | test
254 | Unit and functional tests along with fixtures. When using the rails generate
255 | command, template test files will be generated for you and placed in this
256 | directory.
257 |
258 | vendor
259 | External libraries that the application depends on. Also includes the plugins
260 | subdirectory. If the app has frozen rails, those gems also go here, under
261 | vendor/rails/. This directory is in the load path.
262 |
--------------------------------------------------------------------------------