konekt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +1 -0
- data/bin/console +10 -0
- data/bin/setup +7 -0
- data/konekt.gemspec +27 -0
- data/lib/konekt.rb +6 -0
- data/lib/konekt/configuration.rb +33 -0
- data/lib/konekt/event.rb +56 -0
- data/lib/konekt/listeners.rb +15 -0
- data/lib/konekt/middleware.rb +48 -0
- data/lib/konekt/railtie.rb +11 -0
- data/lib/konekt/version.rb +3 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 716e607b786f4ee9afcdbb63571a53241765aed4
|
4
|
+
data.tar.gz: 4d198b49d12b58d74ea46fda61cd3ff73b31b1f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aaed9cae72fd76165fa284b048f7356503d4ac0c0663d626d593c8cca5d962d5eb5e1bd907e42e5801811810ceec7dcbd1c26b80bfd3147d77e33c44cd496339
|
7
|
+
data.tar.gz: da212237f49af87e37e8c1f25c5426b8af036ea34d22419c3c07e1ddaeca4d097c303c1bcac298f376284a7b8e59c6f6918c2f7651a85ffb3c76ce0a9c7aa661
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Robert Coleman
|
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,83 @@
|
|
1
|
+
# Konekt
|
2
|
+
|
3
|
+
A Ruby (and optionally Rails) library to interact with the Konekt cloud.
|
4
|
+
|
5
|
+
Current features:
|
6
|
+
|
7
|
+
* Webhook to receive messages - "Custom Webhook Url (your own App)"
|
8
|
+
* Automatic authentication to webhook key
|
9
|
+
* Decodes payload JSON
|
10
|
+
* Decodes data base64
|
11
|
+
* Basically, all data passed in to Konekt is passed out as an easy to consume object - per tag, where ever in your app you wish to subscribe.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'konekt'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
`$ bundle`
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
`$ gem install konekt`
|
28
|
+
|
29
|
+
#### Setup
|
30
|
+
|
31
|
+
The endpoint defaults to '/konekt_processor'. If you're app is at `https://example.com/` then you should set your webhook URL in Konekt to `https://example.com/konekt_processor`
|
32
|
+
|
33
|
+
`ENV['KONEKT_WEBHOOK_KEY']` should be set to to the "Webhook Key" value from the Konekt _applcation_ for this webhook.
|
34
|
+
|
35
|
+
Or you can override these in configuration:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# In an appropriate initializer
|
39
|
+
Konekt.configure do |config|
|
40
|
+
config.mount_point = '/some/other/path'
|
41
|
+
config.auth_token = ENV['KONEKT_WEBHOOK_KEY']
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
__Rack/Sinatra:__
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# config.ru
|
49
|
+
require 'konekt/middleware'
|
50
|
+
use Konekt::Middleware
|
51
|
+
```
|
52
|
+
|
53
|
+
__Rails:__ This middleware is registered automatically.
|
54
|
+
|
55
|
+
|
56
|
+
### Subscribing to Tags
|
57
|
+
|
58
|
+
Subscribe to the tags you care about:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Konekt.subscribe 'parcel_shipped' do |event|
|
62
|
+
puts event
|
63
|
+
# Do something with event
|
64
|
+
# You probably want to fire a background task
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
In Rails this could live at `config/initalizers/konekt.rb`
|
69
|
+
|
70
|
+
|
71
|
+
## Development
|
72
|
+
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it ( https://github.com/[my-github-username]/konekt/fork )
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/konekt.gemspec
ADDED
@@ -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 'konekt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'konekt'
|
8
|
+
spec.version = Konekt::VERSION
|
9
|
+
spec.authors = ['Robert Coleman']
|
10
|
+
spec.email = ['github@robert.net.nz']
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby library to interact with Konekt Cloud via Ruby and Rails.}
|
13
|
+
spec.description = %q{A Ruby library to interact with Konekt Cloud via Ruby and Rails.}
|
14
|
+
spec.homepage = 'https://github.com/rjocoleman/konekt'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.8'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.3'
|
26
|
+
spec.add_development_dependency 'rack-test', '~> 0.6'
|
27
|
+
end
|
data/lib/konekt.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Konekt
|
2
|
+
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Configuration
|
16
|
+
attr_accessor *[
|
17
|
+
:logger,
|
18
|
+
:mount_point,
|
19
|
+
:auth_token,
|
20
|
+
:raise_exceptions,
|
21
|
+
]
|
22
|
+
|
23
|
+
alias_method :raise_exceptions?, :raise_exceptions
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@mount_point = '/konekt_processor'
|
27
|
+
@logger = Logger.new STDOUT
|
28
|
+
@auth_token = ENV['KONEKT_WEBHOOK_KEY']
|
29
|
+
@raise_exceptions = true unless ENV['RACK_ENV'] == 'production'
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/konekt/event.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Konekt
|
5
|
+
class Event
|
6
|
+
class InvalidEventData < StandardError; end
|
7
|
+
|
8
|
+
attr_reader *[
|
9
|
+
:tags,
|
10
|
+
:data,
|
11
|
+
:received_at,
|
12
|
+
:source,
|
13
|
+
:device_name,
|
14
|
+
:user_id,
|
15
|
+
:payload,
|
16
|
+
:properties
|
17
|
+
]
|
18
|
+
|
19
|
+
def self.from_params(params)
|
20
|
+
attrs = JSON.parse params['payload']
|
21
|
+
tags = attrs.fetch('tags')
|
22
|
+
data = Base64.decode64(attrs.fetch('data'))
|
23
|
+
received_at = Time.parse(attrs.fetch('received'))
|
24
|
+
source = attrs.fetch('source')
|
25
|
+
device_name = attrs.fetch('device_name')
|
26
|
+
user_id = params['userid']
|
27
|
+
properties = JSON.parse params['properties']
|
28
|
+
|
29
|
+
event = { tags: tags,
|
30
|
+
data: data,
|
31
|
+
received_at: received_at,
|
32
|
+
source: source,
|
33
|
+
device_name: device_name,
|
34
|
+
user_id: user_id,
|
35
|
+
payload: attrs,
|
36
|
+
properties: properties,
|
37
|
+
}
|
38
|
+
new event
|
39
|
+
|
40
|
+
rescue ArgumentError, KeyError, JSON::JSONError => e
|
41
|
+
raise InvalidEventData, e
|
42
|
+
end
|
43
|
+
|
44
|
+
def initialize(opts={})
|
45
|
+
@tags = opts[:tags]
|
46
|
+
@data = opts[:data]
|
47
|
+
@received_at = opts[:received_at] || Time.now.utc
|
48
|
+
@source = opts[:source]
|
49
|
+
@device_name = opts[:device_name]
|
50
|
+
@user_id = opts[:user_id]
|
51
|
+
@payload = opts[:payload]
|
52
|
+
@properties = opts[:properties]
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Konekt
|
2
|
+
LISTENERS = Hash.new
|
3
|
+
|
4
|
+
def self.subscribe(event_tag, &block)
|
5
|
+
LISTENERS[event_tag] ||= []
|
6
|
+
LISTENERS[event_tag] << block
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.propagate(event)
|
10
|
+
event.tags.each do |tag|
|
11
|
+
Array(LISTENERS[tag]).each { |block| block.call event }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'konekt/configuration'
|
2
|
+
require 'konekt/event'
|
3
|
+
require 'konekt/listeners'
|
4
|
+
|
5
|
+
module Konekt
|
6
|
+
class Middleware
|
7
|
+
|
8
|
+
def initialize(app=nil)
|
9
|
+
@app = app
|
10
|
+
end
|
11
|
+
|
12
|
+
def config
|
13
|
+
Konekt.configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
req = Rack::Request.new(env)
|
18
|
+
return @app.call(env) unless req.path_info == config.mount_point
|
19
|
+
return response 405 unless req.post?
|
20
|
+
return response 403 unless req.params['key'] == config.auth_token
|
21
|
+
|
22
|
+
parse_and_respond req.params
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse_and_respond(params)
|
28
|
+
event = Event.from_params params
|
29
|
+
Konekt.propagate event
|
30
|
+
response 200, 'ACK'
|
31
|
+
rescue Event::InvalidEventData => e
|
32
|
+
config.logger.error e
|
33
|
+
response 200, 'Ignored invalid event'
|
34
|
+
rescue => e
|
35
|
+
raise e if config.raise_exceptions?
|
36
|
+
response 500, 'NAK'
|
37
|
+
end
|
38
|
+
|
39
|
+
def response(status, body='', headers={})
|
40
|
+
[
|
41
|
+
status,
|
42
|
+
{'Content-Type' => 'text/plain'}.merge(headers),
|
43
|
+
[body],
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konekt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Coleman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
83
|
+
description: A Ruby library to interact with Konekt Cloud via Ruby and Rails.
|
84
|
+
email:
|
85
|
+
- github@robert.net.nz
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- konekt.gemspec
|
100
|
+
- lib/konekt.rb
|
101
|
+
- lib/konekt/configuration.rb
|
102
|
+
- lib/konekt/event.rb
|
103
|
+
- lib/konekt/listeners.rb
|
104
|
+
- lib/konekt/middleware.rb
|
105
|
+
- lib/konekt/railtie.rb
|
106
|
+
- lib/konekt/version.rb
|
107
|
+
homepage: https://github.com/rjocoleman/konekt
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.4.5
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: A Ruby library to interact with Konekt Cloud via Ruby and Rails.
|
131
|
+
test_files: []
|