phlegethon 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,10 +8,8 @@
8
8
  Welcome to phlegethon
9
9
  =====================
10
10
 
11
- Phlegethon is an endpoint for
12
- [PayPal Webhooks](https://developer.paypal.com/docs/integration/direct/rest-webhooks-overview/)
13
- which forwards all requests to
14
- [RabbitMQ](https://www.rabbitmq.com/).
11
+ Phlegethon is a generic endpoint for Webhooks which forwards all
12
+ requests to [RabbitMQ](https://www.rabbitmq.com/).
15
13
 
16
14
  ## Installation
17
15
 
@@ -43,15 +41,20 @@ It will look for a config file in the following places
43
41
  A phlegethon config file may look like this; these are the defaults
44
42
 
45
43
  rabbitmq:
46
- extension: paypal
44
+ exchange: webhooks
47
45
  host: localhost
48
46
  server:
49
- port: 9292
50
- ssl: true
47
+ port: 3002
48
+ ssl: false
49
+
50
+
51
+ With the above config you can test you endpoint with curl
52
+
53
+ curl -d '{"hey":"ho"}' -H 'Content-type: application/json' http://localhost:3002
51
54
 
52
55
  ## Prerequisites
53
56
 
54
- To use phlegethon with https you have to have libssl-dev installed
57
+ To use phlegethon with https you have to have `libssl-dev` installed
55
58
  before installing eventmachine.
56
59
 
57
60
  apt-get install libssl-dev
@@ -4,11 +4,22 @@ require 'json'
4
4
  require 'thin'
5
5
  require 'bunny'
6
6
 
7
+ require 'pp'
8
+
7
9
  module Phlegethon
8
10
  module Exec
9
11
 
10
- # TODO make exchange or queue name configurable
11
- EXCHANGE = 'paypal'
12
+ DEFAULTS = {
13
+ 'rabbitmq' => {
14
+ 'exchange' => 'webhooks',
15
+ 'host' => 'localhost'
16
+ },
17
+ 'server' => {
18
+ 'ip' => '0.0.0.0',
19
+ 'port' => 3002,
20
+ 'ssl' => false
21
+ }
22
+ }
12
23
 
13
24
  extend self
14
25
 
@@ -16,18 +27,33 @@ module Phlegethon
16
27
 
17
28
  def run(args)
18
29
  init_bunny
19
- # TODO make port and bind address configurable
20
- server = Thin::Server.new('0.0.0.0', 3002, handler)
30
+ server = Thin::Server.new(config['server']['ip'],
31
+ config['server']['port'],
32
+ handler)
21
33
  server.start
22
34
  end
23
35
 
24
36
  def handler
25
37
  ->(env) {
26
- # TODO perform business logic on posted data
27
- output = deep_encode(env, 'UTF-8')
28
- exchange.publish(JSON.unparse(output))
38
+ pp env
39
+ req = Rack::Request.new(env)
40
+
41
+ # perform some business logic on posted data
42
+ message = deep_encode({
43
+ 'params' => req.params,
44
+ 'method' => req.request_method,
45
+ 'url' => req.url,
46
+ 'user_agent' => req.user_agent
47
+ })
48
+ case req.content_type
49
+ when 'application/json'
50
+ message['payload'] = JSON.parse(req.body.read)
51
+ else
52
+ message = deep_encode(env)
53
+ end
54
+ exchange.publish(JSON.unparse(message))
29
55
  # TODO make debug response configurable
30
- [200, {'Content-Type' => 'text/plain'}, output.to_yaml]
56
+ [200, {'Content-Type' => 'text/plain'}, message.to_yaml]
31
57
  }
32
58
  end
33
59
 
@@ -37,11 +63,11 @@ module Phlegethon
37
63
  bunny = Bunny.new read_timeout: 10, heartbeat: 10
38
64
  bunny.start
39
65
  bunny_channel = bunny.create_channel
40
- self.exchange = bunny_channel.fanout(EXCHANGE)
66
+ self.exchange = bunny_channel.fanout(config['rabbitmq']['exchange'])
41
67
  end
42
68
 
43
69
  # TODO move to gem trickery
44
- def deep_encode(data, enc)
70
+ def deep_encode(data, enc='UTF-8')
45
71
  case data
46
72
  when String
47
73
  data.encode(enc)
@@ -55,5 +81,25 @@ module Phlegethon
55
81
  end
56
82
  end
57
83
 
84
+ def config
85
+ @config ||= DEFAULTS.merge(YAML.load(File.read(config_path)))
86
+ end
87
+
88
+ def config_path
89
+ config_path_candidates.each do |f|
90
+ if File.exist?(f)
91
+ puts "Reading config from #{f}"
92
+ return f
93
+ end
94
+ end
95
+ warn 'config file not found'
96
+ exit
97
+ end
98
+
99
+ def config_path_candidates
100
+ [ './phlegethon.yml',
101
+ ENV['HOME'] + '/.phlegethon.yml' ]
102
+ end
103
+
58
104
  end
59
105
  end
@@ -1,3 +1,3 @@
1
1
  module Phlegethon
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,6 @@
1
+ rabbitmq:
2
+ exchange: webhooks
3
+ host: localhost
4
+ server:
5
+ port: 3002
6
+ ssl: false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlegethon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-24 00:00:00.000000000 Z
12
+ date: 2015-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -94,6 +94,7 @@ files:
94
94
  - lib/phlegethon/exec.rb
95
95
  - lib/phlegethon/version.rb
96
96
  - phlegethon.gemspec
97
+ - phlegethon.yml
97
98
  homepage: http://github.com/branch14/phlegethon
98
99
  licenses:
99
100
  - MIT
@@ -109,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  version: '0'
110
111
  segments:
111
112
  - 0
112
- hash: -3554166032137022905
113
+ hash: -278817303022163784
113
114
  required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  none: false
115
116
  requirements:
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  segments:
120
121
  - 0
121
- hash: -3554166032137022905
122
+ hash: -278817303022163784
122
123
  requirements: []
123
124
  rubyforge_project:
124
125
  rubygems_version: 1.8.23