chillout 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -14,11 +14,11 @@ And then execute:
14
14
 
15
15
  $ bundle
16
16
 
17
- And configure in config/environments/production.rb:
17
+ And run Rails generator:
18
18
 
19
- ```ruby
20
- config.chillout = { secret: '<YOUR_SECRET>' }
21
- ```
19
+ $ rails g chillout:install EMAIL EMAIL2...
20
+
21
+ And that's all! You can find whole configuration in config/environments/production.rb under ```chillout``` key.
22
22
 
23
23
  Check if everything is ok:
24
24
 
@@ -35,7 +35,7 @@ module Chillout
35
35
  end
36
36
 
37
37
  def enqueue(creations)
38
- ensure_worker_running
38
+ start_worker
39
39
  @logger.info "Creations were enqueued."
40
40
  @queue << creations
41
41
  end
@@ -44,22 +44,23 @@ module Chillout
44
44
  @worker_thread && @worker_thread.alive?
45
45
  end
46
46
 
47
- private
48
47
  def start_worker
49
- @worker_thread = Thread.new do
50
- worker = Worker.new(@dispatcher, @queue, @logger)
51
- worker.run
52
- end
53
- end
54
-
55
- def ensure_worker_running
56
48
  return if worker_running?
57
49
  @worker_mutex.synchronize do
58
50
  return if worker_running?
59
- start_worker
51
+ @worker_thread = Thread.new do
52
+ begin
53
+ worker = Worker.new(@dispatcher, @queue, @logger)
54
+ worker.run
55
+ ensure
56
+ @logger.flush
57
+ end
58
+ end
60
59
  end
61
60
  end
62
61
 
62
+ private
63
+
63
64
  def build_config(config_or_api_key, options)
64
65
  case config_or_api_key
65
66
  when Config
@@ -0,0 +1,26 @@
1
+ require 'chillout/registration'
2
+
3
+ module Chillout
4
+ class Install < ::Rails::Generators::Base
5
+ argument :emails, :required => true, :type => :array, :banner => "EMAIL1 EMAIL2..."
6
+
7
+ desc "Installs chillout in your app"
8
+ def install_chillout
9
+ project_name = File.basename(Rails.root)
10
+ registration = Registration.new
11
+ api_key = registration.register(project_name, emails)
12
+ application nil, :env => :production do
13
+ "config.chillout = { :secret => '#{api_key}' }"
14
+ end
15
+ puts "Chillout installed - you can find its configuration in config/environments/production.rb."
16
+ rescue Registration::NotRegisteredByLimit
17
+ puts "Chillout not installed - we're currently out of limit for new projects. Please try again later or send us email on info@chillout.io."
18
+ rescue Registration::NotRegisteredByInvalidData
19
+ puts "Chillout not installed - you've passed incorrect data."
20
+ rescue Registration::NotRegisteredByAccident
21
+ puts "Chillout not installed - our API returned a silly response. Please send us email on info@chillout.io."
22
+ rescue Registration::NotRegisteredByCommunicationError
23
+ puts "Chillout not installed - we couldn't get response from our API. Please check if you can access https://chillout.io/ with your browser."
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,11 @@
1
+ require 'forwardable'
2
+
1
3
  module Chillout
2
4
  class PrefixedLogger
5
+ extend Forwardable
6
+
7
+ def_delegators :@logger, :flush
8
+
3
9
  attr_reader :prefix
4
10
 
5
11
  def initialize(prefix, logger)
@@ -13,6 +13,10 @@ module Chillout
13
13
  rake_tasks do
14
14
  load "chillout/tasks.rb"
15
15
  end
16
+
17
+ generators do
18
+ require "chillout/generators/install"
19
+ end
16
20
  end
17
21
 
18
22
  class RailsInitializer
@@ -30,6 +34,7 @@ module Chillout
30
34
  @rails_logger.info "[Chillout] Creation listener attached"
31
35
  @rails_app.middleware.use Middleware::CreationsMonitor, client
32
36
  @rails_logger.info "[Chillout] Creation monitor enabled"
37
+ client.start_worker
33
38
  end
34
39
 
35
40
  def options
@@ -0,0 +1,33 @@
1
+ require 'chillout/server_side/plain_http_client'
2
+
3
+ module Chillout
4
+ class Registration
5
+ class NotRegisteredByLimit < StandardError; end
6
+ class NotRegisteredByInvalidData < StandardError; end
7
+ class NotRegisteredByAccident < StandardError; end
8
+ class NotRegisteredByCommunicationError < StandardError; end
9
+
10
+ def initialize(http_client=PlainHttpClient.new)
11
+ @http_client = http_client
12
+ end
13
+
14
+ def register(name, emails)
15
+ data = {
16
+ :name => name,
17
+ :emails => emails
18
+ }
19
+ response_body = @http_client.post("/projects", data)
20
+ response_body["api_key"].strip
21
+ rescue PlainHttpClient::NotCreated => e
22
+ if e.code == "403"
23
+ raise NotRegisteredByLimit.new
24
+ elsif e.code == "400"
25
+ raise NotRegisteredByInvalidData.new
26
+ else
27
+ raise NotRegisteredByAccident.new
28
+ end
29
+ rescue PlainHttpClient::CommunicationError => e
30
+ raise NotRegisteredByCommunicationError.new
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ require 'net/https'
2
+
3
+ module Chillout
4
+ class PlainHttpClient
5
+ class NotCreated < StandardError
6
+ attr_reader :response
7
+
8
+ def initialize(response)
9
+ @response = response
10
+ end
11
+
12
+ def code
13
+ @response.code
14
+ end
15
+ end
16
+
17
+ class CommunicationError < StandardError
18
+ attr_reader :original_exception
19
+
20
+ def initialize(exception)
21
+ @original_exception = exception
22
+ end
23
+ end
24
+
25
+ MEDIA_TYPE = "application/vnd.chillout.v1+json"
26
+
27
+ def post(path, data)
28
+ begin
29
+ http = Net::HTTP.new("api.chillout.io", 443)
30
+ http.use_ssl = true
31
+ request_spec = Net::HTTP::Post.new(path)
32
+ request_spec.body = MultiJson.dump(data)
33
+ request_spec.content_type = MEDIA_TYPE
34
+ response = http.start do
35
+ http.request(request_spec)
36
+ end
37
+ raise NotCreated.new(response) if response.code != "201"
38
+ MultiJson.load(response.body)
39
+ rescue NotCreated
40
+ raise
41
+ rescue => e
42
+ raise CommunicationError.new(e)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Chillout
2
- VERSION = "0.5.4"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -53,6 +53,7 @@ module Chillout
53
53
  containers_to_merge = get_all_containers_to_process
54
54
  creations_container = merge_containers(containers_to_merge)
55
55
  send_creations(creations_container)
56
+ logger.flush
56
57
  sleep 5
57
58
  end
58
59
  end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+ require 'chillout/server_side/plain_http_client'
3
+
4
+ module Chillout
5
+ class PlainHttpClientTest < ChilloutTestCase
6
+ def setup
7
+ @client = PlainHttpClient.new
8
+ WebMock.reset!
9
+ end
10
+
11
+ def test_post_returns_response_body_json_as_hash
12
+ stub_request(:post, "https://api.chillout.io/projects").to_return(:status => 201, :body => MultiJson.dump(:api_key => "123-123-123"))
13
+ assert_equal "123-123-123", @client.post("/projects", {
14
+ :name => "name"
15
+ })["api_key"]
16
+ end
17
+
18
+ def test_post_raises_exception_when_status_code_is_not_201
19
+ stub_request(:post, "https://api.chillout.io/projects").to_return(:status => 403, :body => MultiJson.dump({}))
20
+ assert_raises PlainHttpClient::NotCreated do
21
+ @client.post("/projects", {
22
+ :name => "name"
23
+ })
24
+ end
25
+ end
26
+
27
+ def test_post_raises_exception_when_there_is_unpredicted_exception
28
+ stub_request(:post, "https://api.chillout.io/projects").to_raise("great cornholio ate api")
29
+ assert_raises PlainHttpClient::CommunicationError do
30
+ @client.post("/projects", {
31
+ :name => "name"
32
+ })
33
+ end
34
+ end
35
+ end
36
+ end
@@ -9,5 +9,12 @@ module Chillout
9
9
  prefixed_logger.error "Someone is wrong on the internet"
10
10
  assert_includes output.string, "[Chillout] Someone is wrong on the internet"
11
11
  end
12
+
13
+ def test_responds_to_flush
14
+ logger = mock("logger")
15
+ logger.expects(:flush)
16
+ prefixed_logger = PrefixedLogger.new("Chillout", logger)
17
+ prefixed_logger.flush
18
+ end
12
19
  end
13
20
  end
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+ require 'chillout/registration'
3
+
4
+ module Chillout
5
+ class RegistrationTest < ChilloutTestCase
6
+ def setup
7
+ @http_client = mock("http_client")
8
+ @registration = Registration.new(@http_client)
9
+ end
10
+
11
+ def test_register_send_name_and_emails
12
+ @http_client.expects(:post).with("/projects", has_entries(:name => "name of project", :emails => ["kaka@dada.com", "dada@kaka.com"])).returns({"api_key" => "123-123-123"})
13
+ @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
14
+ end
15
+
16
+ def test_register_returns_stripped_response_body_as_result
17
+ @http_client.stubs(:post).returns({"api_key" => " 123-123-123 "})
18
+ assert_equal "123-123-123", @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
19
+ end
20
+
21
+ def test_raises_exception_when_it_receives_info_about_crossed_limit
22
+ exception = stub_not_created("403")
23
+ @http_client.stubs(:post).raises(exception)
24
+ assert_raises Registration::NotRegisteredByLimit do
25
+ @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
26
+ end
27
+ end
28
+
29
+ def test_raises_exception_when_it_receives_info_about_invalid_data
30
+ exception = stub_not_created("400")
31
+ @http_client.stubs(:post).raises(exception)
32
+ assert_raises Registration::NotRegisteredByInvalidData do
33
+ @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
34
+ end
35
+ end
36
+
37
+ def test_raises_exception_when_it_receives_other_failure_response
38
+ exception = stub_not_created("402")
39
+ @http_client.stubs(:post).raises(exception)
40
+ assert_raises Registration::NotRegisteredByAccident do
41
+ @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
42
+ end
43
+ end
44
+
45
+ def test_raises_exception_when_commucation_error_occures
46
+ exception = PlainHttpClient::CommunicationError.new(:e)
47
+ @http_client.stubs(:post).raises(exception)
48
+ assert_raises Registration::NotRegisteredByCommunicationError do
49
+ @registration.register("name of project", ["kaka@dada.com", "dada@kaka.com"])
50
+ end
51
+ end
52
+
53
+ def stub_not_created(code)
54
+ response = stub(:code => code)
55
+ PlainHttpClient::NotCreated.new(response)
56
+ end
57
+ end
58
+ end
metadata CHANGED
@@ -1,140 +1,148 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: chillout
3
- version: !ruby/object:Gem::Version
4
- version: 0.5.4
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
6
11
  platform: ruby
7
- authors:
8
- - Michał Łomnicki
12
+ authors:
13
+ - "Micha\xC5\x82 \xC5\x81omnicki"
9
14
  - Jan Filipowski
10
- - Paweł Pacana
15
+ - "Pawe\xC5\x82 Pacana"
11
16
  autorequire:
12
17
  bindir: bin
13
18
  cert_chain: []
14
- date: 2013-04-08 00:00:00.000000000 Z
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
19
+
20
+ date: 2013-04-12 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
17
23
  name: multi_json
18
- requirement: !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ~>
22
- - !ruby/object:Gem::Version
23
- version: '1.4'
24
- type: :runtime
25
24
  prerelease: false
26
- version_requirements: !ruby/object:Gem::Requirement
25
+ requirement: &id001 !ruby/object:Gem::Requirement
27
26
  none: false
28
- requirements:
27
+ requirements:
29
28
  - - ~>
30
- - !ruby/object:Gem::Version
31
- version: '1.4'
32
- - !ruby/object:Gem::Dependency
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 1
33
+ - 4
34
+ version: "1.4"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
33
38
  name: minitest
34
- requirement: !ruby/object:Gem::Requirement
35
- none: false
36
- requirements:
37
- - - ~>
38
- - !ruby/object:Gem::Version
39
- version: 3.2.0
40
- type: :development
41
39
  prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: &id002 !ruby/object:Gem::Requirement
43
41
  none: false
44
- requirements:
42
+ requirements:
45
43
  - - ~>
46
- - !ruby/object:Gem::Version
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 3
48
+ - 2
49
+ - 0
47
50
  version: 3.2.0
48
- - !ruby/object:Gem::Dependency
49
- name: rake
50
- requirement: !ruby/object:Gem::Requirement
51
- none: false
52
- requirements:
53
- - - ~>
54
- - !ruby/object:Gem::Version
55
- version: 0.9.2
56
51
  type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
57
55
  prerelease: false
58
- version_requirements: !ruby/object:Gem::Requirement
56
+ requirement: &id003 !ruby/object:Gem::Requirement
59
57
  none: false
60
- requirements:
58
+ requirements:
61
59
  - - ~>
62
- - !ruby/object:Gem::Version
60
+ - !ruby/object:Gem::Version
61
+ hash: 63
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 2
63
66
  version: 0.9.2
64
- - !ruby/object:Gem::Dependency
65
- name: mocha
66
- requirement: !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - '='
70
- - !ruby/object:Gem::Version
71
- version: 0.12.8
72
67
  type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
73
71
  prerelease: false
74
- version_requirements: !ruby/object:Gem::Requirement
72
+ requirement: &id004 !ruby/object:Gem::Requirement
75
73
  none: false
76
- requirements:
77
- - - '='
78
- - !ruby/object:Gem::Version
74
+ requirements:
75
+ - - "="
76
+ - !ruby/object:Gem::Version
77
+ hash: 63
78
+ segments:
79
+ - 0
80
+ - 12
81
+ - 8
79
82
  version: 0.12.8
80
- - !ruby/object:Gem::Dependency
81
- name: contest
82
- requirement: !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ~>
86
- - !ruby/object:Gem::Version
87
- version: 0.1.3
88
83
  type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: contest
89
87
  prerelease: false
90
- version_requirements: !ruby/object:Gem::Requirement
88
+ requirement: &id005 !ruby/object:Gem::Requirement
91
89
  none: false
92
- requirements:
90
+ requirements:
93
91
  - - ~>
94
- - !ruby/object:Gem::Version
92
+ - !ruby/object:Gem::Version
93
+ hash: 29
94
+ segments:
95
+ - 0
96
+ - 1
97
+ - 3
95
98
  version: 0.1.3
96
- - !ruby/object:Gem::Dependency
97
- name: rack-test
98
- requirement: !ruby/object:Gem::Requirement
99
- none: false
100
- requirements:
101
- - - ~>
102
- - !ruby/object:Gem::Version
103
- version: 0.6.2
104
99
  type: :development
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ name: rack-test
105
103
  prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
104
+ requirement: &id006 !ruby/object:Gem::Requirement
107
105
  none: false
108
- requirements:
106
+ requirements:
109
107
  - - ~>
110
- - !ruby/object:Gem::Version
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ - 6
113
+ - 2
111
114
  version: 0.6.2
112
- - !ruby/object:Gem::Dependency
113
- name: webmock
114
- requirement: !ruby/object:Gem::Requirement
115
- none: false
116
- requirements:
117
- - - ~>
118
- - !ruby/object:Gem::Version
119
- version: 1.8.11
120
115
  type: :development
116
+ version_requirements: *id006
117
+ - !ruby/object:Gem::Dependency
118
+ name: webmock
121
119
  prerelease: false
122
- version_requirements: !ruby/object:Gem::Requirement
120
+ requirement: &id007 !ruby/object:Gem::Requirement
123
121
  none: false
124
- requirements:
122
+ requirements:
125
123
  - - ~>
126
- - !ruby/object:Gem::Version
124
+ - !ruby/object:Gem::Version
125
+ hash: 33
126
+ segments:
127
+ - 1
128
+ - 8
129
+ - 11
127
130
  version: 1.8.11
131
+ type: :development
132
+ version_requirements: *id007
128
133
  description: Chillout.io Ruby client
129
- email:
134
+ email:
130
135
  - michal.lomnicki@gmail.com
131
136
  - jachuf@gmail.com
132
137
  - pawel.pacana@gmail.com
133
138
  - dev@arkency.com
134
139
  executables: []
140
+
135
141
  extensions: []
142
+
136
143
  extra_rdoc_files: []
137
- files:
144
+
145
+ files:
138
146
  - .gitignore
139
147
  - .travis.yml
140
148
  - Gemfile
@@ -149,11 +157,14 @@ files:
149
157
  - lib/chillout/creation_listener.rb
150
158
  - lib/chillout/creations_container.rb
151
159
  - lib/chillout/event_data_builder.rb
160
+ - lib/chillout/generators/install.rb
152
161
  - lib/chillout/middleware/creations_monitor.rb
153
162
  - lib/chillout/prefixed_logger.rb
154
163
  - lib/chillout/railtie.rb
164
+ - lib/chillout/registration.rb
155
165
  - lib/chillout/server_side/dispatcher.rb
156
166
  - lib/chillout/server_side/http_client.rb
167
+ - lib/chillout/server_side/plain_http_client.rb
157
168
  - lib/chillout/server_side/server_side.rb
158
169
  - lib/chillout/tasks.rb
159
170
  - lib/chillout/version.rb
@@ -169,41 +180,46 @@ files:
169
180
  - test/integration/creations_monitor_rack_test.rb
170
181
  - test/integration/worker_test.rb
171
182
  - test/middleware/creations_monitor_test.rb
183
+ - test/plain_http_client_test.rb
172
184
  - test/prefixed_logger_test.rb
185
+ - test/registration_test.rb
173
186
  - test/server_side_test.rb
174
187
  - test/test_helper.rb
175
188
  - test/worker_test.rb
176
189
  homepage: http://chillout.io/
177
190
  licenses: []
191
+
178
192
  post_install_message:
179
193
  rdoc_options: []
180
- require_paths:
194
+
195
+ require_paths:
181
196
  - lib
182
- required_ruby_version: !ruby/object:Gem::Requirement
197
+ required_ruby_version: !ruby/object:Gem::Requirement
183
198
  none: false
184
- requirements:
185
- - - ! '>='
186
- - !ruby/object:Gem::Version
187
- version: '0'
188
- segments:
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ hash: 3
203
+ segments:
189
204
  - 0
190
- hash: 3188055463774447782
191
- required_rubygems_version: !ruby/object:Gem::Requirement
205
+ version: "0"
206
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
207
  none: false
193
- requirements:
194
- - - ! '>='
195
- - !ruby/object:Gem::Version
196
- version: '0'
197
- segments:
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ hash: 3
212
+ segments:
198
213
  - 0
199
- hash: 3188055463774447782
214
+ version: "0"
200
215
  requirements: []
216
+
201
217
  rubyforge_project:
202
- rubygems_version: 1.8.25
218
+ rubygems_version: 1.8.15
203
219
  signing_key:
204
220
  specification_version: 3
205
221
  summary: Chillout.io Ruby client
206
- test_files:
222
+ test_files:
207
223
  - test/check_result_test.rb
208
224
  - test/client_test.rb
209
225
  - test/config_test.rb
@@ -215,7 +231,9 @@ test_files:
215
231
  - test/integration/creations_monitor_rack_test.rb
216
232
  - test/integration/worker_test.rb
217
233
  - test/middleware/creations_monitor_test.rb
234
+ - test/plain_http_client_test.rb
218
235
  - test/prefixed_logger_test.rb
236
+ - test/registration_test.rb
219
237
  - test/server_side_test.rb
220
238
  - test/test_helper.rb
221
239
  - test/worker_test.rb