rack-mock_json 0.0.2 → 0.0.3

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: 1c23dad1181a8b3596f04ec2926685da58f949ec
4
- data.tar.gz: e079e442284e3d1880de1ec08ea6c532147e0a95
3
+ metadata.gz: 3400442311feca9764c53c44ae1ce24b4b905724
4
+ data.tar.gz: 4fc30cc2f3a7938afc32ca58e931002ca193c62f
5
5
  SHA512:
6
- metadata.gz: d1230e746bbdffd32b8b56addd990c91c63ba737aaaa0c3db28651e10120973859db06a4ed965c86ca7010d54dba0a0cba662540b386caface171ae171581618
7
- data.tar.gz: d7c6c31251d3a26f561e7778e38a60f0bcf8e5e06cefa4b9bd41574979260611af15118ffd8fe4657a6b57ce74ea1a348bc269e4859f8ec7e05ef8cc0668da8e
6
+ metadata.gz: df1807053a8e19fc4b1d8253013becb47af491e37697d797b3855d7339ba824a7f6a49a3c11997d2f46bd16941a1e386fa881da28f8074899576a24236265214
7
+ data.tar.gz: 4d4f19560c84c77acac482366ee65452c2101f7c144c6bf1587e1c4c4841638e882fc07cbfccf628337e530d72b74c1f5cf0b12a22b55e610fc9107a58214fe3
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  rack-mock_json
2
2
  ====================
3
3
  [![Gem Version](https://badge.fury.io/rb/rack-mock_json.svg)](http://badge.fury.io/rb/rack-mock_json)
4
-
4
+ [![Circle CI](https://circleci.com/gh/fukuiretu/rack-mock_json.svg?style=svg)](https://circleci.com/gh/fukuiretu/rack-mock_json)
5
5
 
6
6
 
7
7
  ## Overview
@@ -38,30 +38,34 @@ end
38
38
  ### Config
39
39
 
40
40
  ```yaml
41
- - request_path: 'GET /hoge/.*/foo'
42
- content: '{ "hoge": "foo", "bar" : "fuga" }'
41
+ - request_path: 'GET /users'
42
+ contents:
43
+ - '{ "name": "retu", "age" : "20" }'
44
+ - '{ "name": "jane", "age" : "25" }'
43
45
  - request_path: 'POST /user'
44
46
  status: 201
45
- content: '{ "name": "taro", "age" : 17 }'
47
+ contents:
48
+ - '{ "name": "taro", "age" : 17 }'
46
49
  ```
47
50
 
48
- [Examples For more information, click here](example/rails-example/config/mock_json.yml)
49
-
50
- | Propety | Required | Default |
51
- | :------------- | :------------| :-----------|
52
- | request_path | ○ | - |
53
- | status | × | 200 |
54
- | content | ○ | - |
55
-
51
+ If you have multiple elements specified in the `contents`, and then picked up at random. Also, if you specify the `mock_element_index=i` in parameter, and it returns a particular element.
56
52
 
53
+ Ex.
57
54
 
58
- ## Future
59
- - randomly pick the content you specify multiple.
60
-
55
+ ```sh
56
+ $ curl http://localhost:3000/users -X GET -d "mock_element_index=0"
57
+ { "name": "retu", "age" : "20" }
58
+ $ curl http://localhost:3000/users -X GET -d "mock_element_index=1"
59
+ { "name": "jane", "age" : "25" }
60
+ ```
61
61
 
62
+ | Propety | Required | Default | Remarks |
63
+ | :------------- | :------------| :-----------| :------------------|
64
+ | request_path | ○ | - | Use the regular expression |
65
+ | status | × | 200 | HTTP Status |
66
+ | contents | ○ | - | Body Content |
62
67
 
63
- ## ToDo
64
- - spec & CI
68
+ [Examples For more information, click here](example/rails-example/config/mock_json.yml)
65
69
 
66
70
 
67
71
 
@@ -1,5 +1,8 @@
1
- - request_path: 'GET /hoge/.*/foo'
2
- content: '{ "hoge": "foo", "bar" : "fuga" }'
1
+ - request_path: 'GET /users'
2
+ contents:
3
+ - '{ "name": "retu", "age" : "20" }'
4
+ - '{ "name": "jane", "age" : "25" }'
3
5
  - request_path: 'POST /user'
4
6
  status: 201
5
- content: '{ "name": "taro", "age" : 17 }'
7
+ contents:
8
+ - '{ "name": "taro", "age" : 17 }'
@@ -6,4 +6,5 @@ require "hashie"
6
6
 
7
7
  require "rack/mock_json/middleware"
8
8
  require "rack/mock_json/mock"
9
+ require "rack/mock_json/element"
9
10
  require "rack/mock_json/version"
@@ -0,0 +1,14 @@
1
+ module Rack
2
+ module MockJson
3
+ class Element < Hashie::Dash
4
+ property :request_path, required: true
5
+ property :status, default: 200
6
+ property :contents, required: true
7
+
8
+ def pick_content(i = nil)
9
+ return contents.sample if i.nil?
10
+ contents[i]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -8,19 +8,21 @@ module Rack
8
8
 
9
9
  def call(env)
10
10
  @request = Rack::Request.new(env)
11
+
11
12
  mock_element = @mock.mock_element(request_path)
12
13
  return @app.call(env) if mock_element.nil?
13
14
 
15
+ content = mock_element.pick_content(mock_element_index)
14
16
  [
15
17
  mock_element.status,
16
18
  {
17
19
  "Content-Type" => "application/json",
18
- "Content-Length" => mock_element.content.bytesize.to_s,
20
+ "Content-Length" => content.bytesize.to_s,
19
21
  "X-XSS-Protection" => "1; mode=block",
20
22
  "X-Content-Type-Options" => "nosniff",
21
23
  "X-Frame-Options" => "SAMEORIGIN"
22
24
  },
23
- [mock_element.content]
25
+ [content]
24
26
  ]
25
27
  end
26
28
 
@@ -29,6 +31,10 @@ module Rack
29
31
  def request_path
30
32
  "#{@request.request_method} #{@request.path_info}"
31
33
  end
34
+
35
+ def mock_element_index
36
+ @request.params["mock_element_index"] ? @request.params["mock_element_index"].to_i : nil
37
+ end
32
38
  end
33
39
  end
34
40
  end
@@ -9,9 +9,11 @@ module Rack
9
9
  element = @config.find { |e| path.match(/#{e["request_path"]}/).present? }
10
10
  return nil if element.blank?
11
11
 
12
- element = Hashie::Mash.new(element)
13
- element.status ||= 200
14
- element
12
+ Element.new(
13
+ request_path: element['request_path'],
14
+ contents: element['contents'],
15
+ status: element['status'] ? element['status'] : 200
16
+ )
15
17
  end
16
18
  end
17
19
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module MockJson
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-mock_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - fukuiretu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-29 00:00:00.000000000 Z
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -184,6 +184,7 @@ files:
184
184
  - example/rails-example/vendor/assets/stylesheets/.keep
185
185
  - lib/rack-mock_json.rb
186
186
  - lib/rack/mock_json.rb
187
+ - lib/rack/mock_json/element.rb
187
188
  - lib/rack/mock_json/middleware.rb
188
189
  - lib/rack/mock_json/mock.rb
189
190
  - lib/rack/mock_json/version.rb