watchdocs-rails 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d4ce6cd6e1a40a6fd07f03b15182856b0cd506f
4
- data.tar.gz: 9a138b39de57b4c8af88dfa30fd07eb0e310cf4f
3
+ metadata.gz: c3219efab0c6110f67ca59f59d3707f894e79717
4
+ data.tar.gz: eacf3d9fe251bef0d9c0f5866dad515233cfa73b
5
5
  SHA512:
6
- metadata.gz: 3677a73a2f29bf78d007620a6abb8115a4cb8dfb7943de3935530cffc53cbff3fb0e8714142b5e30a8221edd848923219bafa09319121480777328400026aadd
7
- data.tar.gz: 274be20d49c111a90fce4ad19da7eed5447e85942eec912ba85dfe7249d34b7acb5d726958f17d33c3d49a7444c70aae476870ea2a9a636bdeed9c25a7165a6d
6
+ metadata.gz: 804a2ff175b8e28e96599dd8ac877f4b1723babd9b844e0b88d39433321aa102925b5977cdd3483d2964d28a95d4a0d4f8da30484d6a52a7d11f52bcf14d2f61
7
+ data.tar.gz: 1a0b2de9e71e4807820d7e45bfbd444c26f8fe8cff63d17e0ca4e4febb8c5e406add509b0da64302440eefb381b8fe970c899ab8f4eed5f469cc5fbfd181dab8
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -9,7 +9,7 @@ It captures every JSON response, stores request and response details in temporar
9
9
  Add to your Gemfile. It is recommended to add to `:test, :development` group.
10
10
 
11
11
  ```ruby
12
- gem 'watchdocs-rails', '~> 0.1.1'
12
+ gem 'watchdocs-rails'
13
13
  ```
14
14
 
15
15
  and run
@@ -24,56 +24,36 @@ Create `config/initializers/watchdocs.rb` and configure the gem if you need to c
24
24
 
25
25
  ```ruby
26
26
  Watchdocs::Rails.configure do |c|
27
- c.store_class = Watchdocs::Rails::Store::MemoryStore
27
+ c.buffer_size = 50
28
28
  c.temp_directory = 'tmp'
29
- c.sync_url = 'http://demo8792890.mockable.io/requests'
29
+ c.export_url = 'http://demo8792890.mockable.io/requests'
30
30
  end
31
31
  ```
32
32
 
33
- ### store_class
33
+ ### buffer_size
34
34
 
35
- This option allows you to specify class that is responsible for storing recordings.
35
+ **Default:** 50
36
36
 
37
- You can select from provided options:
37
+ Buffer is a place where the gem stores recorderd requests. Buffer size is maximum number of requests that can be stored in a buffer. When limit is reached, buffer content is being exported and cleared. *In other words: Buffer is being exported and cleared every `buffer_size` reqests.*
38
38
 
39
- - `Watchdocs::Rails::Store::MemoryStore` **(default)** - stores recordings in memory
40
- - `Watchdocs::Rails::Store::JsonFileStore` - stores in temporary json file
41
-
42
- or you can implement you own store for recordings. Just create module that implements the following methods:
43
-
44
- ```ruby
45
- # Params
46
- # content - is a Ruby Array of Hashes
47
- def write(content)
48
- ...
49
- end
50
-
51
- # Returns Ruby Array of Hashes
52
- def read
53
- ...
54
- end
55
-
56
- def delete!
57
- ...
58
- end
59
-
60
- # Returns true if store already initialized
61
- def exists?
62
- ...
63
- end
64
- ```
39
+ While executing specs buffer is a memory, otherwise it's a temporary file stored in `temp_directory`.
65
40
 
66
41
  ### temp_directory
67
42
 
68
- **Applicable only when `JsonFileStore` enabled as `store_class`**
43
+ **Default:** tmp
44
+
69
45
  Directory to store temporary file with recordings.
70
46
 
71
- ### sync_url
47
+ ### export_url
48
+
49
+ **Default:** http://demo8792890.mockable.io/requests
72
50
 
73
- URL for syncing with your Watchdocs project.
51
+ URL for exporting recorgings to your Watchdocs project.
74
52
 
75
53
  ## Usage
76
54
 
55
+ You can enable Watchdocs to record request while executing specs or making manual tests. You can of course do both at the same time if you want.
56
+
77
57
  ### Tests
78
58
 
79
59
  If you have some requests specs or features specs that call JSON API then add this line to your `config/environments/test.rb`.
@@ -96,7 +76,7 @@ In `specs/rails_helper.rb`:
96
76
 
97
77
  config.after(:suite) do
98
78
  ....
99
- Watchdocs::Rails::Recordings.send
79
+ Watchdocs::Rails::Recordings.export
100
80
  end
101
81
  ```
102
82
 
@@ -105,21 +85,21 @@ In `specs/rails_helper.rb`:
105
85
 
106
86
  ```
107
87
  Minitest.after_run do
108
- Watchdocs::Rails::Recordings.send
88
+ Watchdocs::Rails::Recordings.export
109
89
  end
110
90
  ```
111
91
 
112
- ### Development
92
+ ### Development (manual tests)
113
93
 
114
- If you don't have any specs yet. You can add the following line to `config/environments/development.rb`.
94
+ If you don't have any request specs yet. You can add the following line to `config/environments/development.rb`.
115
95
 
116
96
  ```ruby
117
97
  config.middleware.insert(0, Watchdocs::Rails::Middleware)
118
98
  ```
119
99
 
120
- Then the only option is to send recordings manually from `rails c` by running `Watchdocs::Rails::Recordings.send`.
100
+ If your app doesn't make a lot of JSON requests please set `buffer_size` to lower number (f.e. 10, it's 50 by default). Watchdocs will be recording all requests in your development environment during manual tests.
121
101
 
122
- **IMPORTANT NOTE: You need to select `JsonFileStore` as storage option in that case.**
102
+ You can of course enable the middleware in any other environment like dev or staging
123
103
 
124
104
 
125
105
  ## Versioning
@@ -141,7 +121,6 @@ as even minimal functionality is not guaranteed to be implemented yet.
141
121
 
142
122
  - httparty
143
123
  - configurations
144
- - mini_memory_store
145
124
 
146
125
  ## Contributing
147
126
 
@@ -1,7 +1,7 @@
1
1
  module Watchdocs
2
2
  module Rails
3
3
  module Store
4
- module JsonFileStore
4
+ module FileBuffer
5
5
  class StorageError < StandardError; end
6
6
 
7
7
  class << self
@@ -40,7 +40,7 @@ module Watchdocs
40
40
  end
41
41
 
42
42
  def temp_local_path
43
- Watchdocs::Rails.configuration.temp_directory
43
+ Rails.configuration.temp_directory
44
44
  end
45
45
  end
46
46
  end
@@ -1,7 +1,7 @@
1
1
  module Watchdocs
2
2
  module Rails
3
3
  module Store
4
- module MemoryStore
4
+ module MemoryBuffer
5
5
  class << self
6
6
  def write(content)
7
7
  @store = content
@@ -0,0 +1,9 @@
1
+ module Watchdocs
2
+ module Rails
3
+ module Buffer
4
+ end
5
+ end
6
+ end
7
+
8
+ require 'watchdocs/rails/buffer/file_buffer'
9
+ require 'watchdocs/rails/buffer/memory_buffer'
@@ -3,14 +3,14 @@ require 'configurations'
3
3
  module Watchdocs
4
4
  module Rails
5
5
  include Configurations
6
- configurable :store_class,
6
+ configurable :buffer_size,
7
7
  :temp_directory,
8
8
  :sync_url
9
9
 
10
10
  configuration_defaults do |c|
11
- c.store_class = Watchdocs::Rails::Store::MemoryStore
11
+ c.buffer_size = 50
12
12
  c.temp_directory = 'tmp'
13
- c.sync_url = 'http://demo8792890.mockable.io/requests'
13
+ c.export_url = 'http://demo8792890.mockable.io/requests'
14
14
  end
15
15
  end
16
16
  end
@@ -61,7 +61,13 @@ module Watchdocs
61
61
  end
62
62
 
63
63
  def record_call
64
- Watchdocs::Rails::Recordings.record_call(report)
64
+ Rails::Recordings.record(
65
+ report, from_specs: from_specs?
66
+ )
67
+ end
68
+
69
+ def from_specs?
70
+ ::Rails.env.test?
65
71
  end
66
72
 
67
73
  def request_headers(env)
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+
3
+ module Watchdocs
4
+ module Rails
5
+ module Recordings
6
+ module Exporter
7
+ class WatchdocsApiError < StandardError; end
8
+
9
+ DEFAULT_ERROR = 'Unknown API Error occured.'.freeze
10
+
11
+ class << self
12
+ def export(payload)
13
+ response = HTTParty.post(
14
+ api_url,
15
+ body: payload.to_json,
16
+ headers: { 'Content-Type' => 'application/json' }
17
+ )
18
+ check_response(response)
19
+ end
20
+
21
+ private
22
+
23
+ def check_response(response)
24
+ case response.code.to_s.chars.first
25
+ when '2'
26
+ true
27
+ when '4', '5'
28
+ raise WatchdocsApiError, get_error(response.body)
29
+ else
30
+ raise WatchdocsApiError, DEFAULT_ERROR
31
+ end
32
+ end
33
+
34
+ def get_error(response_body)
35
+ JSON.parse(response_body)['errors'].join(', ')
36
+ rescue
37
+ DEFAULT_ERROR
38
+ end
39
+
40
+ def api_url
41
+ Watchdocs::Rails.configuration.export_url
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,57 @@
1
+ module Watchdocs
2
+ module Rails
3
+ module Recordings
4
+ class Recorder
5
+ attr_reader :store, :output
6
+
7
+ def initialize(from_specs: true)
8
+ set_store(from_specs)
9
+ end
10
+
11
+ def call(new_call)
12
+ record_new
13
+ save_recordings
14
+ send_recordings if buffer_full?
15
+ end
16
+
17
+ private
18
+
19
+ def record_new
20
+ @output = if current_recordings
21
+ current_recordings << new_call
22
+ else
23
+ [new_call]
24
+ end
25
+ end
26
+
27
+ def current_recordings
28
+ @current ||= store.read
29
+ end
30
+
31
+ def save_recordings
32
+ store.write(output)
33
+ end
34
+
35
+ def send_recordings
36
+ Recordings.send(output)
37
+ end
38
+
39
+ def set_store(from_specs)
40
+ @store = if from_specs
41
+ Rails::Buffer::MemoryBuffer
42
+ else
43
+ Rails::Buffer::FileBuffer
44
+ end
45
+ end
46
+
47
+ def buffer_full?
48
+ current_recordings.count > buffer_size
49
+ end
50
+
51
+ def buffer_size
52
+ Rails.configuration.buffer_size
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,45 +1,49 @@
1
+ require 'watchdocs/rails/recordings/recorder'
2
+ require 'watchdocs/rails/recordings/exporter'
3
+
1
4
  module Watchdocs
2
5
  module Rails
3
6
  module Recordings
7
+ attr_reader :store
8
+
4
9
  class << self
5
- def record_call(new_call)
6
- output = if recordings_exists?
7
- current_recordings << new_call
8
- else
9
- [new_call]
10
- end
11
- save_recordings(output)
10
+ def record(new_call, from_specs)
11
+ Recorder.new(
12
+ from_specs: from_specs
13
+ ).call(new_call)
12
14
  end
13
15
 
14
- def clear!
16
+ def clear!(from_specs: true)
17
+ set_store(from_specs)
15
18
  clear_recordings
16
19
  end
17
20
 
18
- def send
19
- Watchdocs::Rails::Bridge.send(current_recordings) &&
20
- clear_recordings
21
+ def export(recordings = nil, from_specs: true)
22
+ recordings ||= current_recordings
23
+ set_store(from_specs)
24
+ send_recordings(recordings) && clear!(from_specs)
21
25
  end
22
26
 
23
27
  private
24
28
 
25
- def recordings_exists?
26
- store.exists?
27
- end
28
-
29
29
  def current_recordings
30
30
  store.read
31
31
  end
32
32
 
33
- def save_recordings(content)
34
- store.write(content)
35
- end
36
-
37
33
  def clear_recordings
38
34
  store.delete!
39
35
  end
40
36
 
41
- def store
42
- Watchdocs::Rails.configuration.store_class
37
+ def export_recorings(recordings)
38
+ Exporter.export(recordings)
39
+ end
40
+
41
+ def set_store(from_specs)
42
+ @store = if from_specs
43
+ Rails::Buffer::MemoryBuffer
44
+ else
45
+ Rails::Buffer::FileBuffer
46
+ end
43
47
  end
44
48
  end
45
49
  end
@@ -1,5 +1,5 @@
1
1
  module Watchdocs
2
2
  module Rails
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -1,8 +1,7 @@
1
1
  require 'watchdocs/rails/core_extensions'
2
- require 'watchdocs/rails/store'
2
+ require 'watchdocs/rails/buffer'
3
3
  require 'watchdocs/rails/configuration'
4
4
  require 'watchdocs/rails/middleware'
5
- require 'watchdocs/rails/bridge'
6
5
  require 'watchdocs/rails/recordings'
7
6
 
8
7
  module Watchdocs
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watchdocs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mazikwyry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-10 00:00:00.000000000 Z
11
+ date: 2017-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,7 +109,9 @@ files:
109
109
  - lib/watchdocs-rails.rb
110
110
  - lib/watchdocs/.DS_Store
111
111
  - lib/watchdocs/rails.rb
112
- - lib/watchdocs/rails/bridge.rb
112
+ - lib/watchdocs/rails/buffer.rb
113
+ - lib/watchdocs/rails/buffer/file_buffer.rb
114
+ - lib/watchdocs/rails/buffer/memory_buffer.rb
113
115
  - lib/watchdocs/rails/configuration.rb
114
116
  - lib/watchdocs/rails/core_extensions.rb
115
117
  - lib/watchdocs/rails/core_extensions/enumerable.rb
@@ -117,9 +119,8 @@ files:
117
119
  - lib/watchdocs/rails/core_extensions/object.rb
118
120
  - lib/watchdocs/rails/middleware.rb
119
121
  - lib/watchdocs/rails/recordings.rb
120
- - lib/watchdocs/rails/store.rb
121
- - lib/watchdocs/rails/store/file_store.rb
122
- - lib/watchdocs/rails/store/memory_store.rb
122
+ - lib/watchdocs/rails/recordings/exporter.rb
123
+ - lib/watchdocs/rails/recordings/recorder.rb
123
124
  - lib/watchdocs/rails/version.rb
124
125
  - watchdocs-rails-0.1.0.gem
125
126
  - watchdocs-rails-0.1.1.gem
@@ -1,45 +0,0 @@
1
- require 'httparty'
2
-
3
- module Watchdocs
4
- module Rails
5
- module Bridge
6
- class WatchdocsApiError < StandardError; end
7
-
8
- DEFAULT_ERROR = 'Unknown API Error occured.'.freeze
9
-
10
- class << self
11
- def send(payload)
12
- response = HTTParty.post(
13
- api_url,
14
- body: payload.to_json,
15
- headers: { 'Content-Type' => 'application/json' }
16
- )
17
- check_response(response)
18
- end
19
-
20
- private
21
-
22
- def check_response(response)
23
- case response.code.to_s.chars.first
24
- when '2'
25
- true
26
- when '4', '5'
27
- raise WatchdocsApiError, get_error(response.body)
28
- else
29
- raise WatchdocsApiError, DEFAULT_ERROR
30
- end
31
- end
32
-
33
- def get_error(response_body)
34
- JSON.parse(response_body)['errors'].join(', ')
35
- rescue
36
- DEFAULT_ERROR
37
- end
38
-
39
- def api_url
40
- Watchdocs::Rails.configuration.sync_url
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,31 +0,0 @@
1
- module Watchdocs
2
- module Rails
3
- module Store
4
- # You can implement you own store for recordings
5
- # Just create module that implements following methods
6
-
7
- ## Params
8
- ## content - is a Ruby Array of Hashes
9
- # def write(content)
10
- # ...
11
- # end
12
-
13
- ## Returns Ruby Array of Hashes
14
- # def read
15
- # ...
16
- # end
17
-
18
- # def delete!
19
- # ...
20
- # end
21
-
22
- ## Returns true if store already initialized
23
- # def exists?
24
- # ...
25
- # end
26
- end
27
- end
28
- end
29
-
30
- require 'watchdocs/rails/store/file_store'
31
- require 'watchdocs/rails/store/memory_store'