mocapi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0af82132fc3457706c9ad20ce4e9c6910158824f
4
+ data.tar.gz: 57708a422ed77688cfeab3bb04bde5445c4ef919
5
+ SHA512:
6
+ metadata.gz: b4fb09546731960550047baad95f86581355c6763b468fbcec983332ee9458bb9fab3b381225179dc38c709f2072734ad9193afa8bf9d387085394c673917dea
7
+ data.tar.gz: ae0dedc76d2cfec8cc2030cd5a36c43e977072ef90204ac811f400409f76551e94fe2afd162ba2becc9d0e79de4666df15e38e019311ae774cb481accb3eb4a8
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mocapi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mitsutaka Mimura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Mocapi
2
+
3
+ Easy Mock Server.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mocapi'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mocapi
18
+
19
+ ## Usage
20
+
21
+ write config.ru with set resposne_map.yaml(see example `sample/response_map.yaml`), and rackup.
22
+
23
+ ```
24
+ # sample config.ru
25
+ require './lib/mocapi'
26
+
27
+ use Mocapi::ShowRequest
28
+
29
+ Mocapi::MockResponse.load_response_map('./sample/response_map.yaml')
30
+
31
+ run Mocapi::MockResponse.new
32
+
33
+ ```
34
+
35
+ so, you can use mock server, and show request/response.
36
+
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/[my-github-username]/mocapi/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
45
+
46
+ ## TODO
47
+
48
+ - add command line tool
49
+ - use erb template
50
+ - ignore path
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require './lib/mocapi'
2
+
3
+ use Mocapi::ShowRequest
4
+
5
+ Mocapi::MockResponse.load_response_map('./sample/response_map.yaml')
6
+
7
+ run Mocapi::MockResponse.new
data/lib/mocapi.rb ADDED
@@ -0,0 +1,105 @@
1
+ require "mocapi/version"
2
+ require 'rack/request'
3
+ require 'yaml'
4
+
5
+ class Mocapi
6
+ class MockResponse
7
+ class << self
8
+ def load_response_map(response_map_yaml)
9
+ @response_map = YAML.load(File.read(response_map_yaml))
10
+ end
11
+
12
+ def response_map
13
+ @response_map || {}
14
+ end
15
+ end
16
+
17
+ def initialize(app = nil)
18
+ @app = app
19
+ end
20
+
21
+ def call(env)
22
+ request = Rack::Request.new(env)
23
+
24
+ response = if @app
25
+ @app.call(env)
26
+ else
27
+ [200, {}, ['']]
28
+ end
29
+
30
+ if mock_response = response_map[request.path]
31
+ [
32
+ mock_response['status_code'],
33
+ mock_response['headers'],
34
+ mock_response['body']
35
+ ]
36
+ else
37
+ response
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def response_map
44
+ @response_map ||= MockResponse.response_map
45
+ end
46
+ end
47
+
48
+ class ShowRequest
49
+ def initialize(app = nil)
50
+ @app = app
51
+ end
52
+
53
+ def call(env)
54
+ request = Rack::Request.new(env)
55
+ response = if @app
56
+ @app.call(env)
57
+ else
58
+ Rack::Response.new(env)
59
+ end
60
+
61
+ show_request_detail request
62
+ show_response_detail response
63
+
64
+ response
65
+ end
66
+
67
+ def show_request_detail(request)
68
+ show <<-EOS
69
+
70
+ Request URI : #{request.fullpath}
71
+ HTTP Method : #{request.request_method}
72
+ Path : #{request.path}
73
+ Content Type : #{request.content_type}
74
+ Query : #{request.query_string}
75
+ Request Body :
76
+ #{request.body.read}
77
+ EOS
78
+
79
+ request.body.rewind
80
+ end
81
+
82
+ def show_response_detail(response)
83
+ status_code, headers, body = *response
84
+ max_length = (headers.keys + ['Response Body']).map(&:length).max
85
+
86
+ show <<-EOS
87
+ #{'Status Code'.ljust(max_length)} : #{status_code}
88
+ EOS
89
+
90
+ headers.each do |key, value|
91
+ show " #{key.ljust(max_length)} : #{value}"
92
+ end
93
+
94
+ show <<-EOS
95
+ #{'Response Body'.ljust(max_length)} :
96
+ #{body.join('\n')}
97
+
98
+ EOS
99
+ end
100
+
101
+ def show(str)
102
+ STDOUT.puts str
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ class Mocapi
2
+ VERSION = "0.0.1"
3
+ end
data/mocapi.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mocapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mocapi"
8
+ spec.version = Mocapi::VERSION
9
+ spec.authors = ["Mitsutaka Mimura"]
10
+ spec.email = ["takkanm@gmail.com"]
11
+ spec.summary = %q{easy mock server with yaml file}
12
+ spec.description = %q{easy mock server with yaml file}
13
+ spec.homepage = "https://github.com/takkanm/mocapi"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,13 @@
1
+ ---
2
+ /foo:
3
+ status_code: 200
4
+ headers:
5
+ Content-Type: 'application/json'
6
+ body:
7
+ - '{"buzz":1}'
8
+ /foo/bar:
9
+ status_code: 200
10
+ headers:
11
+ Content-Type: 'application/json'
12
+ body:
13
+ - '{"buzz":1}'
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mocapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mitsutaka Mimura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: easy mock server with yaml file
56
+ email:
57
+ - takkanm@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - config.ru
68
+ - lib/mocapi.rb
69
+ - lib/mocapi/version.rb
70
+ - mocapi.gemspec
71
+ - sample/response_map.yaml
72
+ homepage: https://github.com/takkanm/mocapi
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: easy mock server with yaml file
96
+ test_files: []
97
+ has_rdoc: