Class: ApiTransformer::Server
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- ApiTransformer::Server
- Defined in:
- lib/api_transformer.rb
Overview
Inherit from this class to implement an API transformation server:
class ExampleServer < ApiTransformer::Server
base_url "http://ip.jsontest.com/"
get "/ip" do |params|
request :ip do
path "/"
method :get
end
response do |data|
success do
status 200
attribute :your_ip, data[:ip][:ip]
end
end
end
end
Class Method Summary collapse
- .base_url(url) ⇒ Object
- .delete(path = "", options = {}, &block) ⇒ Object
- .get(path = "", options = {}, &block) ⇒ Object
- .helpers(&block) ⇒ Object
- .inherited(klass) ⇒ Object
- .namespace(path) ⇒ Object
- .post(path = "", options = {}, &block) ⇒ Object
- .put(path = "", options = {}, &block) ⇒ Object
- .reset_routes ⇒ Object
- .unhandled_failures(&block) ⇒ Object
Instance Method Summary collapse
- #on_headers(env, headers) ⇒ Object
- #response(env) ⇒ Object
- #run_route(route, path_params, env) ⇒ Object
Class Method Details
.base_url(url) ⇒ Object
76 77 78 |
# File 'lib/api_transformer.rb', line 76 def base_url(url) @@base_url = url end |
.delete(path = "", options = {}, &block) ⇒ Object
109 110 111 |
# File 'lib/api_transformer.rb', line 109 def delete(path = "", = {}, &block) add_route(:delete, path, , block) end |
.get(path = "", options = {}, &block) ⇒ Object
97 98 99 |
# File 'lib/api_transformer.rb', line 97 def get(path = "", = {}, &block) add_route(:get, path, , block) end |
.helpers(&block) ⇒ Object
93 94 95 |
# File 'lib/api_transformer.rb', line 93 def helpers(&block) helper_blocks.unshift(block) end |
.inherited(klass) ⇒ Object
72 73 74 |
# File 'lib/api_transformer.rb', line 72 def inherited(klass) klass.use Goliath::Rack::Params end |
.namespace(path) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/api_transformer.rb', line 80 def namespace(path) prior = [@namespace, failure_handlers.dup, helper_blocks.dup] @namespace = @namespace.to_s + path yield @namespace, @failure_handlers, @helper_blocks = prior end |
.post(path = "", options = {}, &block) ⇒ Object
101 102 103 |
# File 'lib/api_transformer.rb', line 101 def post(path = "", = {}, &block) add_route(:post, path, , block) end |
.put(path = "", options = {}, &block) ⇒ Object
105 106 107 |
# File 'lib/api_transformer.rb', line 105 def put(path = "", = {}, &block) add_route(:put, path, , block) end |
.reset_routes ⇒ Object
113 114 115 |
# File 'lib/api_transformer.rb', line 113 def reset_routes @@routes = Routes.new end |
.unhandled_failures(&block) ⇒ Object
89 90 91 |
# File 'lib/api_transformer.rb', line 89 def unhandled_failures(&block) failure_handlers.unshift(block) end |
Instance Method Details
#on_headers(env, headers) ⇒ Object
39 40 41 |
# File 'lib/api_transformer.rb', line 39 def on_headers(env, headers) env["client-headers"] = headers end |
#response(env) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/api_transformer.rb', line 43 def response(env) path = env[Goliath::Request::REQUEST_PATH] route, path_params = @@routes.find(env["REQUEST_METHOD"], path) if route run_route(route, path_params, env) else [404, {}, "nope"] end end |
#run_route(route, path_params, env) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/api_transformer.rb', line 54 def run_route(route, path_params, env) indifferent_params = Params.try_convert(params.merge(path_params)) endpoint = Endpoint.new(@@base_url, env, route) headers = env["client-headers"] route[:helper_blocks].each { |block| endpoint.instance_eval(&block) } endpoint.instance_exec(indifferent_params, headers, &route[:block]) status, headers = endpoint.run EM.next_tick do endpoint.complete end streaming_response(status, headers) end |