Module: Angus::Remote::Builder

Defined in:
lib/angus/remote/builder.rb

Class Method Summary collapse

Class Method Details

.apply_glossary(glossary, params) ⇒ Hash

Applies glossary to params.

Converts the params that are long names to short names

Parameters:

  • glossary (Glossary)

    of terms

  • params (Hash)

Returns:

  • (Hash)

    params with long names



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/angus/remote/builder.rb', line 141

def self.apply_glossary(glossary, params)
  terms_hash = glossary.terms_hash_with_long_names

  applied_params = {}

  params.each do |name, value|
    if terms_hash.include?(name.to_s)
      term = terms_hash[name.to_s]
      applied_params[term.short_name.to_sym] = value
    else
      applied_params[name] = value
    end
  end

  applied_params
end

.build(code_name, service_definition, api_url, options) ⇒ Remote::Client

Builds a client for a specific service.

Parameters:

  • code_name (String)

    The service’s name known to the service directory

  • service_definition (Angus::SDoc::Definitions::Service)
  • api_url

    Base service api url

  • options (Hash)

Returns:

  • (Remote::Client)

    object that implements each method specified as operation in the service metadata



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/angus/remote/builder.rb', line 21

def self.build(code_name, service_definition, api_url, options)
  remote_service_class = build_client_class(service_definition.name)

  # TODO: define how to use the namespace in the remote client.
  if service_definition.operations.is_a?(Hash)
    service_definition.operations.each do |namespace, operations|
      operations.each do |operation|
        self.define_operation(remote_service_class, namespace, operation, code_name,
                              service_definition)
      end
    end
  else
    service_definition.operations.each do |operation|
      self.define_operation(remote_service_class, code_name, operation, code_name,
                            service_definition)
    end
  end

  service_definition.proxy_operations.each do |operation|
    self.define_proxy_operation(remote_service_class, operation.service_name, operation,
                                code_name, service_definition)
  end

  remote_service_class.new(api_url, Settings.default_timeout, options)
end

.build_client_class(service_name) ⇒ Class

Build a client class for the service

Parameters:

  • service_name (String)

    the name of the service

  • api_url (String)

    the url for consuming the service’s api

Returns:

  • (Class)

    A class client, that inherits from Client



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/angus/remote/builder.rb', line 116

def self.build_client_class(service_name)
  remote_service_class = Class.new(Angus::Remote::Client)

  remote_service_class.class_eval <<-END
    def self.name
      "#<Client_#{service_name}>"
    end

    def self.to_s
      name
    end
  END

  remote_service_class
end

.define_operation(client_class, namespace, operation, service_code_name, service_definition) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/angus/remote/builder.rb', line 47

def self.define_operation(client_class, namespace, operation, service_code_name,
                          service_definition)
  client_class.send :define_method, operation.code_name do |encode_as_json = false,
                                                            path_params = nil,
                                                            request_params = nil|

    args = [encode_as_json, path_params, request_params]

    request_params = Angus::Remote::Builder.extract_var_arg!(args, Hash) || {}
    path_params = Angus::Remote::Builder.extract_var_arg!(args, Array) || []
    encode_as_json = Angus::Remote::Builder.extract_var_arg!(args, TrueClass) || false

    request_params = Angus::Remote::Builder.apply_glossary(service_definition.glossary,
                                                           request_params)

    request_params = Angus::Remote::Builder.escape_request_params(request_params)

    response = make_request(operation.path, operation.http_method, encode_as_json,
                            path_params, request_params)

    Angus::Remote::Response::Builder.build_from_remote_response(response,
                                                                  service_code_name,
                                                                  service_definition.version,
                                                                  namespace,
                                                                  operation.code_name)
  end
end

.define_proxy_operation(client_class, namespace, operation, service_code_name, service_definition) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/angus/remote/builder.rb', line 75

def self.define_proxy_operation(client_class, namespace, operation, service_code_name,
                                service_definition)
  client_class.send :define_method, operation.code_name do |encode_as_json = false,
                                                            path_params = nil,
                                                            request_params = nil|

    require 'picasso-remote'

    service_definition = Picasso::Remote::ServiceDirectory.join_proxy(
      service_code_name,
      service_definition.version,
      operation.service_name
    )

    args = [encode_as_json, path_params, request_params]

    request_params = Picasso::Remote::Builder.extract_var_arg!(args, Hash) || {}
    path_params = Picasso::Remote::Builder.extract_var_arg!(args, Array) || []
    encode_as_json = Picasso::Remote::Builder.extract_var_arg!(args, TrueClass) || false

    request_params = Picasso::Remote::Builder.apply_glossary(service_definition.glossary,
                                                             request_params)

    request_params = Picasso::Remote::Builder.escape_request_params(request_params)

    response = make_request(operation.path, operation.http_method, encode_as_json,
                            path_params, request_params)

    Picasso::Remote::Response::Builder.build_from_remote_response(response,
                                                                  service_code_name,
                                                                  service_definition.version,
                                                                  operation.code_name)
  end
end

.escape_request_params(request_params) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/angus/remote/builder.rb', line 181

def self.escape_request_params(request_params)
  encoded = {}
  request_params.each do |name, value|
    encoded_name = URI.escape(name.to_s)
    if value.is_a? Hash
      value = self.escape_request_params(value)
    end
    encoded[encoded_name] = value
  end

  encoded
end

.extract_var_arg!(args, klass) ⇒ Object

Extract an argument of class klass from an array of args

Returns the first value from args (starting from the end of args) whose class matches klass

Parameters:

  • args

    Array of arguments

  • klass

    Class that should match the returned value



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/angus/remote/builder.rb', line 164

def self.extract_var_arg!(args, klass)
  arg = nil
  arg_found = false

  i = args.length
  while !arg_found && i > 0
    i -= 1
    arg = args[i]
    arg_found = true if arg.is_a?(klass)
  end

  if arg_found
    args.delete_at(i)
    arg
  end
end