harmony-service 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/harmony-service.gemspec +27 -0
- data/lib/harmony/service/action_list/action_request.rb +3 -0
- data/lib/harmony/service/action_list/item.rb +3 -0
- data/lib/harmony/service/action_list/item_request.rb +3 -0
- data/lib/harmony/service/action_list/list_request.rb +3 -0
- data/lib/harmony/service/calculator/request.rb +3 -0
- data/lib/harmony/service/calculator/response.rb +3 -0
- data/lib/harmony/service/chart/request.rb +3 -0
- data/lib/harmony/service/chart/response.rb +3 -0
- data/lib/harmony/service/error_response.rb +3 -0
- data/lib/harmony/service/message.rb +23 -0
- data/lib/harmony/service/request.rb +9 -0
- data/lib/harmony/service/response.rb +4 -0
- data/lib/harmony/service/rpc_service.rb +106 -0
- data/lib/harmony/service/version.rb +18 -0
- data/lib/harmony/service.rb +20 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 89bf205b49b089b6efc12ebdb0e91132b1ae0b27
|
4
|
+
data.tar.gz: 5b33365abe55dadeeff62276c044d548b545b820
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 595e1653de7f3f381e4f99fe5d19659de9e480f166fb31b06c07255b525dddf12536f3fe4778b0434bd1509ea78b7ae10fa3ee5b150a8566cb56539e51cd6d6d
|
7
|
+
data.tar.gz: cf1b3d953239ac5958afc720778534367426245cce561c8ca0efe4c271770106c90b07a0c51cdb9c9934e011b4ae65ddf626e431ec9d3a1f75c9c126ecb72ff2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
harmony-service
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Matt Brooke-Smith
|
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,41 @@
|
|
1
|
+
# Harmony::Service
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/harmony/service`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'harmony-service'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install harmony-service
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/harmony-service.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "harmony/service"
|
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,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'harmony/service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "harmony-service"
|
8
|
+
spec.version = Harmony::Service::VERSION
|
9
|
+
spec.authors = ["Matt Brooke-Smith"]
|
10
|
+
spec.email = ["matt@futureworkshops.com"]
|
11
|
+
spec.summary = "Gem which helps you to build Harmony services"
|
12
|
+
spec.homepage = "https://github.com/HarmonyMobile/harmony-service"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
15
|
+
spec.bindir = "exe"
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "sneakers", '~> 2.4'
|
20
|
+
spec.add_dependency 'bunny', '~> 2.7.0.pre'
|
21
|
+
spec.add_dependency "oj", '~> 2.17.4'
|
22
|
+
spec.add_dependency 'rollbar', '~> 2.12'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Harmony::Service::Message
|
2
|
+
def self.attr_accessor(*vars)
|
3
|
+
@attributes ||= []
|
4
|
+
@attributes.concat vars
|
5
|
+
super(*vars)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.attributes
|
9
|
+
@attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(h = {})
|
13
|
+
h.each {|k,v| instance_variable_set("@#{k}",v)}
|
14
|
+
end
|
15
|
+
|
16
|
+
def attributes
|
17
|
+
self.class.attributes || []
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.json_create(o)
|
21
|
+
new(*o['data'])
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'sneakers'
|
2
|
+
require 'sneakers/metrics/logging_metrics'
|
3
|
+
require 'sneakers/handlers/maxretry'
|
4
|
+
require 'json'
|
5
|
+
require 'oj'
|
6
|
+
require 'rollbar'
|
7
|
+
|
8
|
+
opts = {
|
9
|
+
amqp: ENV['ampq_address'] || 'amqp://localhost:5672',
|
10
|
+
vhost: ENV['ampq_vhost'] || '/',
|
11
|
+
exchange: 'sneakers',
|
12
|
+
exchange_type: :direct,
|
13
|
+
metrics: Sneakers::Metrics::LoggingMetrics.new,
|
14
|
+
handler: Sneakers::Handlers::Maxretry
|
15
|
+
}
|
16
|
+
|
17
|
+
Sneakers.configure(opts)
|
18
|
+
Sneakers.logger.level = ENV['log_level'] == 'debug' ? Logger::DEBUG : Logger::INFO
|
19
|
+
|
20
|
+
Rollbar.configure do |config|
|
21
|
+
config.access_token = ENV['rollbar_access_token']
|
22
|
+
config.environment = ENV['RACK_ENV']
|
23
|
+
config.enabled = ENV['RACK_ENV'] == 'staging' || ENV['RACK_ENV'] == 'production'
|
24
|
+
end
|
25
|
+
|
26
|
+
module Harmony
|
27
|
+
module Service
|
28
|
+
class RpcService
|
29
|
+
|
30
|
+
include Sneakers::Worker
|
31
|
+
|
32
|
+
def work_with_params(message, delivery_info, metadata)
|
33
|
+
begin
|
34
|
+
logger.debug "Request: #{message}"
|
35
|
+
request = Oj.load(message)
|
36
|
+
request_class = request.class
|
37
|
+
response_class = request_response_mapping[request_class]
|
38
|
+
raise "Unacceptable request class: #{request_class}" if response_class.nil?
|
39
|
+
|
40
|
+
result = work_with_request(request)
|
41
|
+
raise "Unacceptable response class: #{result.class}" unless response_class === result
|
42
|
+
|
43
|
+
json = Oj.dump(result)
|
44
|
+
logger.debug "Response: #{json}"
|
45
|
+
send_response(json, metadata.reply_to, metadata.correlation_id)
|
46
|
+
ack!
|
47
|
+
rescue StandardError => error
|
48
|
+
logger.error error.message
|
49
|
+
logger.error error.backtrace.join("\n")
|
50
|
+
|
51
|
+
Rollbar.error(error)
|
52
|
+
|
53
|
+
error_response = ErrorResponse.new
|
54
|
+
error_response.message = "An error occured."
|
55
|
+
error_response.detailed_message = error.message
|
56
|
+
json = Oj.dump(error_response)
|
57
|
+
logger.debug "Response: #{json}"
|
58
|
+
|
59
|
+
send_response(json, metadata.reply_to, metadata.correlation_id)
|
60
|
+
reject!
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def stop
|
65
|
+
super
|
66
|
+
#reply_to_exchange.close # not working
|
67
|
+
reply_to_connection.close
|
68
|
+
end
|
69
|
+
|
70
|
+
def reply_to_connection
|
71
|
+
@reply_to_connection ||= create_reply_to_connection
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_reply_to_connection
|
75
|
+
opts = Sneakers::CONFIG
|
76
|
+
conn = Bunny.new(opts[:amqp], :vhost => opts[:vhost], :heartbeat => opts[:heartbeat], :logger => Sneakers::logger)
|
77
|
+
conn.start
|
78
|
+
conn
|
79
|
+
end
|
80
|
+
|
81
|
+
def reply_to_exchange
|
82
|
+
@reply_to_queue ||= create_reply_to_exchange
|
83
|
+
end
|
84
|
+
|
85
|
+
def create_reply_to_exchange
|
86
|
+
ch = reply_to_connection.create_channel
|
87
|
+
ch.exchange(AMQ::Protocol::EMPTY_STRING, :auto_delete => true)
|
88
|
+
end
|
89
|
+
|
90
|
+
def send_response(result, reply_to, correlation_id)
|
91
|
+
reply_to_exchange.publish(result, :routing_key => reply_to, :correlation_id => correlation_id)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
def request_response_mapping
|
96
|
+
{
|
97
|
+
Calculator::Request => Calculator::Response,
|
98
|
+
ActionList::ListRequest => Array,
|
99
|
+
ActionList::ItemRequest => ActionList::Item,
|
100
|
+
ActionList::ActionRequest => NilClass,
|
101
|
+
Chart::Request => Chart::Response
|
102
|
+
}
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "harmony/service/version"
|
2
|
+
require 'harmony/service/message'
|
3
|
+
|
4
|
+
require 'harmony/service/request'
|
5
|
+
require 'harmony/service/response'
|
6
|
+
require 'harmony/service/error_response'
|
7
|
+
|
8
|
+
require 'harmony/service/calculator/request'
|
9
|
+
require 'harmony/service/calculator/response'
|
10
|
+
|
11
|
+
require 'harmony/service/chart/request'
|
12
|
+
require 'harmony/service/chart/response'
|
13
|
+
|
14
|
+
require 'harmony/service/action_list/action_request'
|
15
|
+
require 'harmony/service/action_list/list_request'
|
16
|
+
require 'harmony/service/action_list/item_request'
|
17
|
+
require 'harmony/service/action_list/item'
|
18
|
+
|
19
|
+
require "harmony/service/rpc_service"
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harmony-service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Brooke-Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sneakers
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bunny
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.7.0.pre
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.7.0.pre
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oj
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.17.4
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.17.4
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rollbar
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.12'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.10'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- matt@futureworkshops.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".ruby-gemset"
|
121
|
+
- ".ruby-version"
|
122
|
+
- ".travis.yml"
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- harmony-service.gemspec
|
130
|
+
- lib/harmony/service.rb
|
131
|
+
- lib/harmony/service/action_list/action_request.rb
|
132
|
+
- lib/harmony/service/action_list/item.rb
|
133
|
+
- lib/harmony/service/action_list/item_request.rb
|
134
|
+
- lib/harmony/service/action_list/list_request.rb
|
135
|
+
- lib/harmony/service/calculator/request.rb
|
136
|
+
- lib/harmony/service/calculator/response.rb
|
137
|
+
- lib/harmony/service/chart/request.rb
|
138
|
+
- lib/harmony/service/chart/response.rb
|
139
|
+
- lib/harmony/service/error_response.rb
|
140
|
+
- lib/harmony/service/message.rb
|
141
|
+
- lib/harmony/service/request.rb
|
142
|
+
- lib/harmony/service/response.rb
|
143
|
+
- lib/harmony/service/rpc_service.rb
|
144
|
+
- lib/harmony/service/version.rb
|
145
|
+
homepage: https://github.com/HarmonyMobile/harmony-service
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.4.5.1
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Gem which helps you to build Harmony services
|
169
|
+
test_files: []
|