class ApiTransformer::Server
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
Public Class Methods
base_url(url)
click to toggle source
# File lib/api_transformer.rb, line 62 def base_url(url) @@base_url = url end
delete(path = "", options = {}, &block)
click to toggle source
# File lib/api_transformer.rb, line 111 def delete(path = "", options = {}, &block) add_route(:delete, path, options, block) end
get(path = "", options = {}, &block)
click to toggle source
# File lib/api_transformer.rb, line 99 def get(path = "", options = {}, &block) add_route(:get, path, options, block) end
helpers(&block)
click to toggle source
# File lib/api_transformer.rb, line 79 def helpers(&block) helper_blocks.unshift(block) end
include_endpoints(name)
click to toggle source
# 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)
click to toggle source
# File lib/api_transformer.rb, line 58 def inherited(klass) klass.use Goliath::Rack::Params end
namespace(path) { || ... }
click to toggle source
# 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)
click to toggle source
# File lib/api_transformer.rb, line 103 def post(path = "", options = {}, &block) add_route(:post, path, options, block) end
put(path = "", options = {}, &block)
click to toggle source
# File lib/api_transformer.rb, line 107 def put(path = "", options = {}, &block) add_route(:put, path, options, block) end
reset()
click to toggle source
# File lib/api_transformer.rb, line 115 def reset @@routes = @failure_handlers = @helper_blocks = @shared_endpoints = nil end
unhandled_failures(&block)
click to toggle source
# File lib/api_transformer.rb, line 75 def unhandled_failures(&block) failure_handlers.unshift(block) end
Private Class Methods
add_route(method, path, options, block)
click to toggle source
# File lib/api_transformer.rb, line 121 def add_route(method, path, options, block) full_path = "#{@namespace}#{path}" @@routes ||= Routes.new @@routes.add(method: method, path: full_path, options: options, block: block, failure_handlers: failure_handlers, helper_blocks: helper_blocks) end
failure_handlers()
click to toggle source
# File lib/api_transformer.rb, line 130 def failure_handlers @failure_handlers ||= [] end
helper_blocks()
click to toggle source
# File lib/api_transformer.rb, line 134 def helper_blocks @helper_blocks ||= [] end
Public Instance Methods
on_headers(env, headers)
click to toggle source
# File lib/api_transformer.rb, line 40 def on_headers(env, headers) env["client-headers"] = headers end
response(env)
click to toggle source
# 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