ripple-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,13 @@
1
+ Copyright © 2013, Stephen Paul Weber <singpolyma.net>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README ADDED
@@ -0,0 +1,12 @@
1
+ This gem provides a simple facility for integrating the Ripple payment system into a Rails project.
2
+
3
+ Create a file in your project at config/ripple.yml with two configuration keys:
4
+
5
+ :action: RippleController#notify
6
+ :address: ripple_address
7
+
8
+ The action key should specify a particular action on a controller in your app. When transactions are sent to the ripple address(es) specified, a request to that action will be simulated, with params[:transaction] set to an object representing the transaction data.
9
+
10
+ If you do not specify an address, then all transactions on the Ripple network will be reported to your app.
11
+
12
+ You may optionally specify the websocket address of a Ripple server as :websocket, otherwise the default will be the server at ripple.com. Only transactions that have been validated by the server are reported, so if you trust the server than you can trust that the transactions are real.
data/bin/ripple-rails ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'ripple-rails'
7
+ require 'eventmachine'
8
+ require 'faye/websocket'
9
+ require 'rippler'
10
+ require 'json'
11
+
12
+ load_environment
13
+ config = load_config
14
+ controller, action = config && config[:action] ? config[:action].split(/#/, 2) : []
15
+
16
+ err 'Ripple action not properly configured.' unless controller && action
17
+ err "No class #{controller} found for Ripple action." unless Module::const_defined?(controller)
18
+
19
+ controller = RippleController.new
20
+
21
+ err "Ripple action #{controller}##{action} found." unless controller.respond_to?(action)
22
+
23
+ EM.run {
24
+ ws = Faye::WebSocket::Client.new(config[:websocket] || Rippler::RIPPLE_URI)
25
+
26
+ ws.onopen = lambda do |event|
27
+ cmd = {'command' => 'subscribe', 'streams' => ['transactions']}
28
+ if config[:address]
29
+ adr = config[:address].is_a?(Array) ? config[:address] : [config[:address].to_s]
30
+ cmd.merge!('accounts' => adr)
31
+ warn "Monitoring for transactions to #{adr.inspect}..."
32
+ else
33
+ warn 'Monitoring for transactions...'
34
+ end
35
+
36
+ ws.send(cmd.to_json)
37
+ end
38
+
39
+ ws.onmessage = lambda do |event|
40
+ message = JSON.parse(event.data) rescue {}
41
+ if message['type'] == 'transaction' && message['validated'] == true && message['transaction']['TransactionType'] == 'Payment'
42
+ begin
43
+ run_action(controller, :notify, {:transaction => Rippler::Transaction.new(message)})
44
+ rescue Exception
45
+ warn $!.inspect
46
+ exit 2
47
+ end
48
+ end
49
+ end
50
+
51
+ ws.onerror = lambda do |event|
52
+ warn event["error"].inspect
53
+ EM.stop
54
+ exit 1
55
+ end
56
+
57
+ ws.onclose = lambda do |event|
58
+ ws = nil
59
+ warn 'Websocket connection closed'
60
+ EM.stop
61
+ exit 1
62
+ end
63
+ }
@@ -0,0 +1,40 @@
1
+ require 'yaml'
2
+
3
+ def err(s)
4
+ warn s
5
+ exit 1
6
+ end
7
+
8
+ def load_environment(file = '.')
9
+ if File.directory?(file) && File.exists?(File.expand_path("#{file}/config/environment.rb"))
10
+ require "rails"
11
+ require File.expand_path("#{file}/config/environment.rb")
12
+ if defined?(::Rails) && ::Rails.respond_to?(:application)
13
+ # Rails 3
14
+ ::Rails.application.eager_load!
15
+ elsif defined?(::Rails::Initializer)
16
+ # Rails 2.3
17
+ $rails_rake_task = false
18
+ ::Rails::Initializer.run :load_application_classes
19
+ end
20
+ elsif File.file?(file)
21
+ require File.expand_path(file)
22
+ end
23
+ end
24
+
25
+ def load_config(file = '.')
26
+ if File.directory?(file) && File.exists?(File.expand_path("#{file}/config/ripple.yml"))
27
+ YAML::load_file(File.expand_path("#{file}/config/ripple.yml"))
28
+ elsif File.file?(file)
29
+ YAML::load_file(File.expand_path(file))
30
+ end
31
+ end
32
+
33
+ def run_action(controller, action, params)
34
+ req = ActionDispatch::Request .new({'rack.input' => ''})
35
+ params.each do |(k,v)|
36
+ req[k] = v
37
+ end
38
+ controller.instance_variable_set(:@_request, req)
39
+ controller.send(action)
40
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "ripple-rails"
7
+ gem.version = '0.1.0'
8
+ gem.authors = ["singpolyma"]
9
+ gem.email = ["singpolyma@singpolyma.net"]
10
+ gem.description = %q{Rails integration for Ripple payment notifications.}
11
+ gem.summary = %q{Rails integration for Ripple payment notifications.}
12
+ gem.homepage = "https://github.com/singpolyma/ripple-rails"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'faye-websocket', '~> 0.4'
20
+ gem.add_dependency 'rippler', '>= 0.0.11'
21
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripple-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - singpolyma
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye-websocket
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rippler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.11
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.11
46
+ description: Rails integration for Ripple payment notifications.
47
+ email:
48
+ - singpolyma@singpolyma.net
49
+ executables:
50
+ - ripple-rails
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - COPYING
55
+ - Gemfile
56
+ - README
57
+ - bin/ripple-rails
58
+ - lib/ripple-rails.rb
59
+ - ripple-rails.gemspec
60
+ homepage: https://github.com/singpolyma/ripple-rails
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.23
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Rails integration for Ripple payment notifications.
84
+ test_files: []