Module: Angus::ProxyActions

Included in:
Base
Defined in:
lib/angus/proxy_actions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.remote_operation(service_code_name, operation_code_name) ⇒ Object



57
58
59
60
# File 'lib/angus/proxy_actions.rb', line 57

def self.remote_operation(service_code_name, operation_code_name)
  remote_service = Picasso::Remote::ServiceDirectory.service_definition(service_code_name)
  remote_service.operation_definition(operation_code_name)
end

Instance Method Details

#after_configureObject



4
5
6
7
# File 'lib/angus/proxy_actions.rb', line 4

def after_configure
  register_proxy_actions
  register_proxy_routes
end

#build_proxy_client(url) ⇒ Object



66
67
68
# File 'lib/angus/proxy_actions.rb', line 66

def build_proxy_client(url)
  Angus::Remote::ProxyClient.new(url, 60)
end

#get_proxy_client(url) ⇒ Object



62
63
64
# File 'lib/angus/proxy_actions.rb', line 62

def get_proxy_client(url)
  proxy_clients[url] ||= build_proxy_client(url)
end

#proxy_clientsObject



70
71
72
# File 'lib/angus/proxy_actions.rb', line 70

def proxy_clients
  @proxy_clients ||= {}
end

#register_proxy_actionsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/angus/proxy_actions.rb', line 15

def register_proxy_actions
  router.on(:get, File.join(doc_path, '/proxy/:service')) do |env, params|
    require 'picasso-remote'
    response = Response.new

    service = params[:service]

    remote_definition = Picasso::Remote::ServiceDirectory.service_definition(service)

    render(response, Picasso::SDoc::JsonFormatter.format_service(remote_definition),
           format: :json)
  end
end

#register_proxy_route(proxy_operation) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/angus/proxy_actions.rb', line 29

def register_proxy_route(proxy_operation)
  require 'picasso-remote'

  remote_api_uri = Picasso::Remote::ServiceDirectory.api_url(proxy_operation.service_name)

  proxy_client = get_proxy_client(remote_api_uri)

  op_path = "#{api_path}#{proxy_operation.path}"

  router.on(proxy_operation.http_method.to_sym, op_path) do |env, params|
    request = Rack::Request.new(env)
    request.body.rewind
    raw_body = request.body.read

    proxy_path =  env['PATH_INFO'].gsub(api_path, '')

    status, headers, body = proxy_client.make_request(
      proxy_operation.http_method,
      proxy_path,
      env['QUERY_STRING'],
      {},
      raw_body
    )

    Response.new(body, status, headers)
  end
end

#register_proxy_routesObject



9
10
11
12
13
# File 'lib/angus/proxy_actions.rb', line 9

def register_proxy_routes
  @definitions.proxy_operations.each do |proxy_operation|
    register_proxy_route(proxy_operation)
  end
end