recurly_event 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 367bf8594ffdbe43736320bb78109eb9ece66b2a
4
+ data.tar.gz: ac68290ea4c65fc053da003bc7f07bc16ab4f75d
5
+ SHA512:
6
+ metadata.gz: fafc05450d5623ed878004ae4c68ea99bfd16785c46131d82174e85a39858685f18e9b3367a9a440a785f48dc7d96c69b23ebe38d781527aae8767d3bc8d6bcf
7
+ data.tar.gz: 191eec00bda36c9f273793ec26141caa35654f71ebf37d3ee117baee662a7da11096443874e6b978df25f29f9b98e6a76b885abb28ec3f72c5cf7d78464dbc7a
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ addons:
5
+ code_climate:
6
+ repo_token: c9aa4c7c36f23faa189a65c4414bbc553d60320b488a6cde301a267b30c49409
7
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in recurly_event.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ejay Canaria
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # RecurlyEvent
2
+ [![Build Status](https://travis-ci.org/ejaypcanaria/recurly_event.svg)](https://travis-ci.org/ejaypcanaria/recurly_event) [![Code Climate](https://codeclimate.com/github/ejaypcanaria/recurly_event/badges/gpa.svg)](https://codeclimate.com/github/ejaypcanaria/recurly_event) [![Test Coverage](https://codeclimate.com/github/ejaypcanaria/recurly_event/badges/coverage.svg)](https://codeclimate.com/github/ejaypcanaria/recurly_event/coverage)
3
+
4
+ RecurlyEvent is a simple Ruby DSL for managing [Recurly Webhooks](https://recurly.readme.io/v2.0/page/webhooks) inside a Rails application. This project is inspired by the [StripeEvent](https://github.com/integrallis/stripe_event/) gem and is built using pub/sub API of [ActiveSupport::Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'recurly_event'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Mount the Rails Engine to your route:
19
+ ```ruby
20
+ # config/routes.rb
21
+ mount RecurlyEvent::Engine, at: "/your-chosen-path"
22
+ ```
23
+
24
+ ## Usage
25
+ Add an initializer to your Rails application:
26
+ ```ruby
27
+ # config/initializers/recurly.rb
28
+ RecurlyEvent.configure do |events|
29
+ # To subscribe to a webhook, just pass in the event name and
30
+ # an instance of a class that responds to `call(recurly_object)`
31
+ events.subscribe "account.new", AccountCreated.new
32
+
33
+ # You can also subscribe to a namespace of events.
34
+ # In this case, you will be notified of all the invoice actions webhook
35
+ events.subscribe "invoice.", InvoiceEventHandler.new
36
+
37
+ # Subscribe to all the events
38
+ events.all RecurlyEventLogger.new
39
+ end
40
+ ```
41
+
42
+ ### Event name convention
43
+ All Recurly webhooks are listed [here](https://recurly.readme.io/v2.0/page/webhooks). Here's a sample `new_account_notification` XML response from their website:
44
+ ```xml
45
+ <?xml version="1.0" encoding="UTF-8"?>
46
+ <new_account_notification>
47
+ <account>
48
+ <account_code>1</account_code>
49
+ <username nil="true"></username>
50
+ <email>verena@example.com</email>
51
+ <first_name>Verena</first_name>
52
+ <last_name>Example</last_name>
53
+ <company_name nil="true"></company_name>
54
+ </account>
55
+ </new_account_notification>
56
+ ```
57
+ As you can see above the top level node is the event name from Recurly, in this case `new_account_notification`. The convention in this gem follows this pattern for the event name: `[noun].[verb]`. So, if you want to subscribe to this event, the event name is `account.new`.
58
+
59
+ ### Callable object
60
+ The second parameter in the `subscribe` method is an instance of a class that responds to `call(recurly_object)`. This will be triggered once Recurly fires the webhook.
61
+
62
+ ```ruby
63
+ # Plain old Ruby object
64
+ class AccountCreated
65
+ def call(recurly_object)
66
+ # recurly_object is an OpenStruct that has the same structure as the XML without the parent node (the event name).
67
+ # You can get specific attributes by traversing the object like this:
68
+ puts recurly_object.account.username
69
+ end
70
+ end
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ejaypcanaria/recurly_event. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](CODE_OF_CONDUCT.md) code of conduct.
82
+
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/app/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ module RecurlyEvent
2
+ class WebhookController < ActionController::Base
3
+ def event
4
+ RecurlyEvent.process_request(request)
5
+ head :ok
6
+ end
7
+ end
8
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "recurly_event"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ RecurlyEvent::Engine.routes.draw do
2
+ root "webhook#event", via: :post
3
+ end
@@ -0,0 +1,5 @@
1
+ module RecurlyEvent
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RecurlyEvent
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ module RecurlyEvent
2
+ class Namespace < Struct.new(:namespace, :delimeter)
3
+ RESOURCES_REGEXP = /account|billing_info|invoice|subscription|payment|refund/
4
+
5
+ def with_namespace(name)
6
+ "#{namespace}#{delimeter}#{name}"
7
+ end
8
+
9
+ def parse_with_namespace(name)
10
+ with_namespace(parse_name(name))
11
+ end
12
+
13
+ def regexp_wrap(name)
14
+ /^#{Regexp.escape(with_namespace(name))}/
15
+ end
16
+
17
+ private
18
+
19
+ def parse_name(name)
20
+ noun = RESOURCES_REGEXP.match(name).to_s
21
+ splitted = name.gsub("#{noun}_", "").split("_")
22
+ splitted.pop
23
+ verb = splitted.join("_")
24
+ "#{noun}.#{verb}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ require "json"
2
+ require "active_support/core_ext/hash"
3
+
4
+ module RecurlyEvent
5
+ class Parser
6
+ def self.parse(request)
7
+ new(request)
8
+ end
9
+
10
+ def self.from_payload(payload)
11
+ JSON.parse(payload.to_json, object_class: OpenStruct)
12
+ end
13
+
14
+ def initialize(request)
15
+ @request = request
16
+ end
17
+
18
+ def event_name
19
+ hash_from_request.first.first
20
+ end
21
+
22
+ def payload
23
+ hash_from_request.first.last
24
+ end
25
+
26
+ private
27
+
28
+ def hash_from_request
29
+ # see https://recurly.readme.io/v2.0/page/webhooks for the xml structure
30
+ @hash_from_request ||= Hash.from_xml(@request.body.string)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module RecurlyEvent
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,42 @@
1
+ require "active_support/notifications"
2
+ require "recurly_event/version"
3
+ require "recurly_event/namespace"
4
+ require "recurly_event/parser"
5
+ require "recurly_event/engine" if defined? Rails
6
+
7
+ module RecurlyEvent
8
+ NAMESPACE = "recurly".freeze
9
+
10
+ class << self
11
+ attr_accessor :notifications, :namespace, :parser
12
+
13
+ def configure(&block)
14
+ raise ArgumentError, "missing block" unless block_given?
15
+ yield self
16
+ end
17
+
18
+ def all(callable=Proc.new {})
19
+ subscribe nil, callable
20
+ end
21
+
22
+ def process_request(request)
23
+ parsed_request = parser.parse(request)
24
+ publish(parsed_request.event_name, parsed_request.payload)
25
+ end
26
+
27
+ def publish(event_name, payload)
28
+ notifications.instrument(namespace.parse_with_namespace(event_name), payload)
29
+ end
30
+
31
+ def subscribe(event_name, callable=Proc.new {})
32
+ notifications.subscribe namespace.regexp_wrap(event_name) do |*args|
33
+ recurly_object = parser.from_payload(args.last)
34
+ callable.call(recurly_object)
35
+ end
36
+ end
37
+ end
38
+
39
+ self.parser = Parser
40
+ self.namespace = Namespace.new(NAMESPACE, ".")
41
+ self.notifications = ActiveSupport::Notifications
42
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'recurly_event/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "recurly_event"
8
+ spec.version = RecurlyEvent::VERSION
9
+ spec.authors = ["Ejay Canaria"]
10
+ spec.email = ["ejaypcanaria@gmail.com"]
11
+
12
+ spec.summary = %q{Simple DSL to utilize Recurly webhook inside a Rails application}
13
+ spec.homepage = "https://github.com/ejaypcanaria/recurly_event"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">= 3.1"
22
+
23
+ spec.add_development_dependency "codeclimate-test-reporter"
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recurly_event
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ejay Canaria
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - ejaypcanaria@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - app/.DS_Store
99
+ - app/controllers/.DS_Store
100
+ - app/controllers/recurly_event/.DS_Store
101
+ - app/controllers/recurly_event/webhook_controller.rb
102
+ - bin/console
103
+ - bin/setup
104
+ - config/routes.rb
105
+ - lib/recurly_event.rb
106
+ - lib/recurly_event/engine.rb
107
+ - lib/recurly_event/namespace.rb
108
+ - lib/recurly_event/parser.rb
109
+ - lib/recurly_event/version.rb
110
+ - recurly_event.gemspec
111
+ homepage: https://github.com/ejaypcanaria/recurly_event
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.8
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Simple DSL to utilize Recurly webhook inside a Rails application
135
+ test_files: []