stubify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +0 -0
- data/README.md +0 -0
- data/bin/stubify +5 -0
- data/lib/stubify.rb +30 -0
- data/lib/stubify/io.rb +56 -0
- data/lib/stubify/server.rb +106 -0
- data/lib/stubify/version.rb +6 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3024ecac437551c37cfd1b61ec9432ae25a95d68
|
4
|
+
data.tar.gz: 1ea224476a63df693848a3d76b80b65bbf99c758
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27a60208d4c7f65d600da4f6e6fc93d6d38f69de24bc298249ad657e30bc976ac6ec8c051143a64cf35cbc8175cd447979c101892230c430fb8d415d23222cdb
|
7
|
+
data.tar.gz: 0e7cf755418ed007539e1b079d9f8bde8aaa8df20e5f61292984f43b4a6b8fc22222a65fa44db4aba4e73891bbb849948b10e7ad120b08bda62684582bd4fe9f
|
data/LICENSE
ADDED
File without changes
|
data/README.md
ADDED
File without changes
|
data/bin/stubify
ADDED
data/lib/stubify.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'commander'
|
2
|
+
require 'stubify/version'
|
3
|
+
|
4
|
+
module Stubify
|
5
|
+
class << self
|
6
|
+
|
7
|
+
include Commander::Methods
|
8
|
+
|
9
|
+
attr_accessor :options
|
10
|
+
|
11
|
+
def run
|
12
|
+
program :name, 'stubify'
|
13
|
+
program :version, Stubify::VERSION
|
14
|
+
program :description, Stubify::DESCRIPTION
|
15
|
+
|
16
|
+
command :server do |c|
|
17
|
+
c.syntax = 'server'
|
18
|
+
c.description = ''
|
19
|
+
c.option '--directory STRING', String, 'Path where fixtures will be stored. i.e. fixtures/'
|
20
|
+
c.option '--host STRING', String, 'Host the requests will be redirected to. i.e. https://easy-peasy.io'
|
21
|
+
c.action do |args, options|
|
22
|
+
Stubify.options = options
|
23
|
+
require 'stubify/server'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
run!
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/stubify/io.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'pathname'
|
3
|
+
require 'net/http'
|
4
|
+
require 'digest'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module Stubify
|
8
|
+
class IO
|
9
|
+
|
10
|
+
def self.write_on_disk(request, body, response)
|
11
|
+
path = IO.path_from_request(request)
|
12
|
+
FileUtils.mkdir_p(path)
|
13
|
+
file_name = IO.file_name_from_request(request, body)
|
14
|
+
|
15
|
+
data = {
|
16
|
+
'status_code': response.code,
|
17
|
+
'content_type': response.content_type,
|
18
|
+
'body': response.body.to_s
|
19
|
+
}
|
20
|
+
|
21
|
+
File.open(File.join(path, file_name), "wb") do |file|
|
22
|
+
file.puts(data.to_json)
|
23
|
+
end
|
24
|
+
|
25
|
+
return data
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.read_from_disk(request, body)
|
29
|
+
path = IO.path_from_request(request)
|
30
|
+
file_name = IO.file_name_from_request(request, body)
|
31
|
+
file = File.read(File.join(path, file_name))
|
32
|
+
return JSON.parse(file)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.cached?(request, body)
|
36
|
+
path = IO.path_from_request(request)
|
37
|
+
file_name = IO.file_name_from_request(request, body)
|
38
|
+
return Pathname.new(File.join(path, file_name)).file?
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.path_from_request(request)
|
42
|
+
return File.join(Stubify.options.directory, request.path)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.file_name_from_request(request, body)
|
46
|
+
file_name = request.request_method.dup
|
47
|
+
file_name << request.query_string.dup unless request.query_string.nil?
|
48
|
+
unless body.nil?
|
49
|
+
file_name << Digest::SHA256.hexdigest(body.to_s)
|
50
|
+
end
|
51
|
+
file_name = "#{Digest::SHA256.hexdigest(file_name)}.json"
|
52
|
+
return file_name
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'stubify/io'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module Stubify
|
6
|
+
class Server < Sinatra::Base
|
7
|
+
|
8
|
+
set :app_file, __FILE__
|
9
|
+
set :run, Proc.new { true }
|
10
|
+
|
11
|
+
if run?
|
12
|
+
puts 'Launching Sinatra app...'
|
13
|
+
end
|
14
|
+
|
15
|
+
at_exit { Server.run! if $!.nil? && Server.run? }
|
16
|
+
|
17
|
+
get // do
|
18
|
+
response = Server.on_request(request)
|
19
|
+
status response['status_code']
|
20
|
+
return response[:body] unless response[:body].nil?
|
21
|
+
return response['body'] unless response['body'].nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
post // do
|
25
|
+
response = Server.on_request(request)
|
26
|
+
status response['status_code']
|
27
|
+
return response[:body] unless response[:body].nil?
|
28
|
+
return response['body'] unless response['body'].nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
delete // do
|
32
|
+
response = Server.on_request(request)
|
33
|
+
status response['status_code']
|
34
|
+
return response[:body] unless response[:body].nil?
|
35
|
+
return response['body'] unless response['body'].nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
put // do
|
39
|
+
response = Server.on_request(request)
|
40
|
+
status response['status_code']
|
41
|
+
return response[:body] unless response[:body].nil?
|
42
|
+
return response['body'] unless response['body'].nil?
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.on_request(request)
|
46
|
+
body = nil
|
47
|
+
body = request.body.read unless request.body.nil?
|
48
|
+
|
49
|
+
if IO.cached?(request, body)
|
50
|
+
puts "Loading \"#{request.path}\" from cache"
|
51
|
+
response_map = IO.read_from_disk(request, body)
|
52
|
+
return response_map
|
53
|
+
else
|
54
|
+
puts "Requesting \"#{request.path}\""
|
55
|
+
response = Server.forward(request, request.request_method, body)
|
56
|
+
response_map = IO.write_on_disk(request, body, response)
|
57
|
+
return response_map
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.forward(request, method, body)
|
62
|
+
# Build url
|
63
|
+
uri = URI(Stubify.options.host)
|
64
|
+
uri.path = request.path
|
65
|
+
uri.query = request.query_string
|
66
|
+
|
67
|
+
# Create new request
|
68
|
+
new_req = nil
|
69
|
+
if method == 'DELETE'
|
70
|
+
new_req = Net::HTTP::Delete.new(uri)
|
71
|
+
new_req.body = body
|
72
|
+
elsif method == 'POST'
|
73
|
+
new_req = Net::HTTP::Post.new(uri)
|
74
|
+
new_req.body = body
|
75
|
+
elsif method == 'PUT'
|
76
|
+
new_req = Net::HTTP::Put.new(uri)
|
77
|
+
new_req.body = body
|
78
|
+
else
|
79
|
+
new_req = Net::HTTP::Get.new(uri)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Set headers
|
83
|
+
request.env.each do |key, value|
|
84
|
+
if key.include?('HTTP_')
|
85
|
+
new_key = key.dup
|
86
|
+
new_key.slice!('HTTP_')
|
87
|
+
new_req[new_key] = value unless ['HOST', 'VERSION'].include?(new_key)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Perform request
|
92
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
93
|
+
http.request(new_req)
|
94
|
+
end
|
95
|
+
|
96
|
+
return response
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
extend Sinatra::Delegator
|
103
|
+
|
104
|
+
class Rack::Builder
|
105
|
+
include Sinatra::Delegator
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stubify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carlos Vidal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.8
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: commander
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.4.0
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 5.0.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 4.4.0
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 5.0.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: terminal-table
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: bundler
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
description: The program description
|
90
|
+
email:
|
91
|
+
- nakioparkour@gmail.com
|
92
|
+
executables:
|
93
|
+
- stubify
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files: []
|
96
|
+
files:
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- bin/stubify
|
100
|
+
- lib/stubify.rb
|
101
|
+
- lib/stubify/io.rb
|
102
|
+
- lib/stubify/server.rb
|
103
|
+
- lib/stubify/version.rb
|
104
|
+
homepage: https://github.com/nakiostudio/stubify
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 2.0.0
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.8
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: The program description
|
128
|
+
test_files: []
|