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
- .include_endpoints(name) ⇒ Object
- .inherited(klass) ⇒ Object
- .namespace(path) ⇒ Object
- .post(path = "", options = {}, &block) ⇒ Object
- .put(path = "", options = {}, &block) ⇒ Object
- .reset ⇒ Object
- .shared_endpoints(name, &block) ⇒ Object
- .unhandled_failures(&block) ⇒ Object
Instance Method Summary collapse
Class Method Details
.base_url(url) ⇒ Object
62 63 64 |
# File 'lib/api_transformer.rb', line 62 def base_url(url) @@base_url = url end |
.delete(path = "", options = {}, &block) ⇒ Object
111 112 113 |
# File 'lib/api_transformer.rb', line 111 def delete(path = "", = {}, &block) add_route(:delete, path, , block) end |
.get(path = "", options = {}, &block) ⇒ Object
99 100 101 |
# File 'lib/api_transformer.rb', line 99 def get(path = "", = {}, &block) add_route(:get, path, , block) end |
.helpers(&block) ⇒ Object
79 80 81 |
# File 'lib/api_transformer.rb', line 79 def helpers(&block) helper_blocks.unshift(block) end |
.include_endpoints(name) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/api_transformer.rb', line 91 def include_endpoints(name) if shared_endpoint_blocks[name] instance_eval(&shared_endpoint_blocks[name]) else fail "missing shared_endpoints for \"#{name}\"" end end |
.inherited(klass) ⇒ Object
58 59 60 |
# File 'lib/api_transformer.rb', line 58 def inherited(klass) klass.use Goliath::Rack::Params end |
.namespace(path) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/api_transformer.rb', line 66 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
103 104 105 |
# File 'lib/api_transformer.rb', line 103 def post(path = "", = {}, &block) add_route(:post, path, , block) end |
.put(path = "", options = {}, &block) ⇒ Object
107 108 109 |
# File 'lib/api_transformer.rb', line 107 def put(path = "", = {}, &block) add_route(:put, path, , block) end |
.reset ⇒ Object
115 116 117 |
# File 'lib/api_transformer.rb', line 115 def reset @@routes = @failure_handlers = @helper_blocks = @shared_endpoints = nil end |
.shared_endpoints(name, &block) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/api_transformer.rb', line 83 def shared_endpoints(name, &block) if shared_endpoint_blocks[name] fail "shared_endpoints already defined for \"#{name}\"" else shared_endpoint_blocks[name] = block end end |
.unhandled_failures(&block) ⇒ Object
75 76 77 |
# File 'lib/api_transformer.rb', line 75 def unhandled_failures(&block) failure_handlers.unshift(block) end |
Instance Method Details
#on_headers(env, headers) ⇒ Object
40 41 42 |
# File 'lib/api_transformer.rb', line 40 def on_headers(env, headers) env["client-headers"] = headers end |
#response(env) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/api_transformer.rb', line 44 def response(env) path = env[Goliath::Request::REQUEST_PATH] route, path_params = @@routes.find(env["REQUEST_METHOD"], path) if route all_params = params.merge(path_params) response = route.run(@@base_url, all_params, env) streaming_response(response.status, response.headers) else [404, {}, "nope"] end end |