Class: ApiTransformer::Server

Inherits:
Goliath::API
  • Object
show all
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

Instance Method Summary collapse

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 = "", options = {}, &block)
  add_route(:delete, path, options, block)
end

.get(path = "", options = {}, &block) ⇒ Object



97
98
99
# File 'lib/api_transformer.rb', line 97

def get(path = "", options = {}, &block)
  add_route(:get, path, options, 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 = "", options = {}, &block)
  add_route(:post, path, options, block)
end

.put(path = "", options = {}, &block) ⇒ Object



105
106
107
# File 'lib/api_transformer.rb', line 105

def put(path = "", options = {}, &block)
  add_route(:put, path, options, block)
end

.reset_routesObject



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