optimus_prime 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/config.ru +4 -0
- data/lib/optimus_prime/server.rb +52 -0
- data/lib/optimus_prime/version.rb +3 -0
- data/lib/optimus_prime.rb +44 -0
- data/optimus_prime.gemspec +27 -0
- data/optimus_prime.log +705 -0
- data/spec/lib/optimus_prime_spec.rb +92 -0
- data/spec/spec_helper.rb +29 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e9948403e82a31ee6872df5159c5bced232979b3
|
4
|
+
data.tar.gz: b93e43ddde2fc6c2d5b9359101d6435026dccf83
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7be92898c1a4891df828d50868a4bed82fd3f2c737e6b928f142003f649e764ca842406cbd79e4adfe12fc246958a4b079c187fad62c3751e0d4baab2ccf273
|
7
|
+
data.tar.gz: 92e2d929f0d35c431a6ef77c054ee65a999d3f7bcc8b8f1209b4c0b50a2d45f675016f7bf347c665ddf842628ac5787a9d78fb819083dc884fc2bd3d903e35d4
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
log/
|
12
|
+
lib/bundler/man
|
13
|
+
pkg
|
14
|
+
rdoc
|
15
|
+
spec/reports
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
*.bundle
|
20
|
+
*.so
|
21
|
+
*.o
|
22
|
+
*.a
|
23
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
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,73 @@
|
|
1
|
+
# OptimusPrime
|
2
|
+
####“It’s been an honor serving with you all.”
|
3
|
+
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
```ruby
|
9
|
+
gem 'optimus_prime'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
```bash
|
14
|
+
$ bundle
|
15
|
+
```
|
16
|
+
Or install it yourself as:
|
17
|
+
```bash
|
18
|
+
$ gem install optimus_prime
|
19
|
+
```
|
20
|
+
|
21
|
+
OptimusPrime allows developers to persist fake date and tell their API to talk
|
22
|
+
to it and get the desired response.
|
23
|
+
|
24
|
+
## Default configuration
|
25
|
+
* localhost:7002/get -> default endpoint
|
26
|
+
* returns 200 status code for GET,POST
|
27
|
+
* sets content-type to text
|
28
|
+
|
29
|
+
## HTTP allowed requests
|
30
|
+
* GET
|
31
|
+
* POST
|
32
|
+
|
33
|
+
# Usage
|
34
|
+
```ruby
|
35
|
+
OptimusPrime.start_server
|
36
|
+
op = OptimusPrime::Base.new
|
37
|
+
op.prime("path_name", response, options)
|
38
|
+
```
|
39
|
+
|
40
|
+
## Changing Content Type:
|
41
|
+
```ruby
|
42
|
+
op.prime("users", { users json... }, { content_type: :json })
|
43
|
+
response = Faraday.get("http://localhost:7002/get/users")
|
44
|
+
response.headers["content-type"] #=> "application/json"
|
45
|
+
```
|
46
|
+
|
47
|
+
## Changing HTTP response method:
|
48
|
+
```ruby
|
49
|
+
op.prime("users", " response... ", { status_code: 404 })
|
50
|
+
response = Faraday.get("http://localhost:7002/get/users")
|
51
|
+
response.status #=> 404
|
52
|
+
```
|
53
|
+
|
54
|
+
## POST requests:
|
55
|
+
```ruby
|
56
|
+
op.prime("users", " response... ", status_code: 201)
|
57
|
+
response = Faraday.post("http://localhost:7002/get/users", { some data })
|
58
|
+
response.body #=> " response... "
|
59
|
+
```
|
60
|
+
|
61
|
+
## TODO
|
62
|
+
* Move server initialisation into rake task in order to prevent it from initialising
|
63
|
+
from its directory.
|
64
|
+
* Support DELETE, HEAD, PUT http methods
|
65
|
+
* Support REGEX as a path
|
66
|
+
* Support html templates
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it ( https://github.com/[my-github-username]/optimus_prime/fork )
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "sinatra/base"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module OptimusPrime
|
6
|
+
|
7
|
+
class Server < ::Sinatra::Base
|
8
|
+
|
9
|
+
set :public_folder, __dir__ + "/server/public"
|
10
|
+
|
11
|
+
post "/get/*" do
|
12
|
+
path = self.env["REQUEST_URI"].sub("/get/", "")
|
13
|
+
response = responses[path]
|
14
|
+
return 404 if response.nil?
|
15
|
+
content_type(response[:content_type] || :html)
|
16
|
+
status(response[:status_code])
|
17
|
+
response[:body]
|
18
|
+
end
|
19
|
+
|
20
|
+
get "/get/*" do
|
21
|
+
path = self.env["REQUEST_URI"].sub("/get/", "")
|
22
|
+
response = responses[path]
|
23
|
+
return 404 if response.nil?
|
24
|
+
content_type(response[:content_type] || :html)
|
25
|
+
status(response[:status_code])
|
26
|
+
response[:body]
|
27
|
+
end
|
28
|
+
|
29
|
+
post "/prime" do
|
30
|
+
path = params["path_name"]
|
31
|
+
responses[path] = { content_type: params["content_type"], body: params["response"], status_code: (params["status_code"] || 200) }
|
32
|
+
201
|
33
|
+
end
|
34
|
+
|
35
|
+
get "/show" do
|
36
|
+
content_type :json
|
37
|
+
responses.to_json
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def responses
|
43
|
+
@@responses ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_boolean(boolean)
|
47
|
+
boolean == "true"
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "optimus_prime/version"
|
2
|
+
require "optimus_prime/server"
|
3
|
+
|
4
|
+
module OptimusPrime
|
5
|
+
|
6
|
+
def self.restart_server
|
7
|
+
self.stop_server
|
8
|
+
self.start_server
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.start_server
|
12
|
+
`mkdir -p ./tmp/pids` unless system("ls ./tmp/pids/")
|
13
|
+
return `echo 'Optimus is already priming :)'` if `ls ./tmp/pids`.include?("optimus_prime.pid")
|
14
|
+
path = `pwd`.chomp
|
15
|
+
if system("cd #{optimus_prime_path} && echo '\nStarting Optimus Prime\n' && thin start -p 7002 -P #{path}/tmp/pids/optimus_prime.pid -l #{path}/optimus_prime.log -d -D")
|
16
|
+
while :starting_server
|
17
|
+
sleep(2) and break if `ls ./tmp/pids`.include?("optimus_prime.pid")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.optimus_prime_path
|
23
|
+
File.expand_path('../', __dir__)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.current_path; `pwd`.chomp; end
|
27
|
+
|
28
|
+
def self.stop_server
|
29
|
+
path = `pwd`.chomp
|
30
|
+
system("cd #{optimus_prime_path} && echo '\nStoping Optimus Prime\n' && thin stop -P #{path}/tmp/pids/optimus_prime.pid")
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.full_path
|
34
|
+
File.expand_path(__dir__)
|
35
|
+
end
|
36
|
+
|
37
|
+
class Base
|
38
|
+
|
39
|
+
def prime(path_name, response="", options={})
|
40
|
+
::Faraday.post("http://localhost:7002/prime", { path_name: path_name, response: response }.merge!(options))
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'optimus_prime/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "optimus_prime"
|
8
|
+
spec.version = OptimusPrime::VERSION.dup
|
9
|
+
spec.authors = ["Antonio Nalesso"]
|
10
|
+
spec.email = ["acnalesso@yahoo.co.uk"]
|
11
|
+
spec.summary = %q{ Create endpoints and persists data }
|
12
|
+
spec.description = %q{ It allows developers to define endpoint, persist some data to be returned when called the endpoint defined.}
|
13
|
+
spec.homepage = ""
|
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_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
spec.add_dependency "thin"
|
26
|
+
spec.add_dependency "faraday"
|
27
|
+
end
|