Class: Farscape::TransitionAgent

Inherits:
Object
  • Object
show all
Includes:
BaseAgent
Defined in:
lib/farscape/transition.rb

Instance Method Summary collapse

Methods included from BaseAgent

#handle_extensions

Constructor Details

#initialize(transition, agent) ⇒ TransitionAgent

Returns a new instance of TransitionAgent.



11
12
13
14
15
# File 'lib/farscape/transition.rb', line 11

def initialize(transition, agent)
  @agent = agent
  @transition = transition
  handle_extensions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



68
69
70
# File 'lib/farscape/transition.rb', line 68

def method_missing(meth, *args, &block)
  @transition.send(meth, *args, &block)
end

Instance Method Details

#invoke(args = {}) {|opts| ... } ⇒ Object

Yields:

  • (opts)


17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/farscape/transition.rb', line 17

def invoke(args = {})
  opts = OpenStruct.new
  yield opts if block_given?
  options = match_params(args, opts)
  params = args.merge(options.parameters || {})

  call_options = {}
  call_options[:method] = @transition.interface_method
  call_options[:headers] = @agent.get_accept_header(@agent.media_type).merge(options.headers || {})
  call_options[:body] = options.attributes if options.attributes.present?

  if call_options[:method].downcase == 'get'
    # delegate the URL building to representors so we can use templated URIs

    # We are in another unfortunate situation in which Mauth-client might not be able to validate
    # if a request query string contains uppercase characters.
    # Somebody, probably Nginx, converts the query string to lowercase, and Mauth-client uses it
    # to compare with the signature which is generated using the original query string.
    # https://github.com/mdsol/mauth-client-ruby/blob/v4.0.1/lib/mauth/client.rb#L333
    call_options[:url] = @transition.uri(params).downcase
    # still need to use this for extra params... (e.g. "conditions=can_do_anything")
    if params.present?
      if @transition.templated?
        # exclude the parameters that have been consumed by Addressable (e.g. path segments) so
        # we don't repeat those in the final URL (ex: /api{/uuid} => /api/123456, not /api/123456?uuid=123456)
        # TODO: make some "variables" method in representors/transition.rb so we don't deal with this here
        Addressable::Template.new(@transition.templated_uri).variables.each do |param|
          params.delete(param.to_sym)
        end
      end
      call_options[:params] = params
    end
  else
    # Farscape handles "parameters" as query string, and "attributes" as request body.
    # However, in many API documents, only "parameters" is used regardless of methods.
    # Since changing API documents must have a huge impact on existing systems,
    # we use parameters as the request body if the method is not GET.
    # This makes it impossible to use URIs with parameters.
    call_options[:url] = @transition.uri
    call_options[:body] = (call_options[:body] || {}).merge(params) if params.present?
  end

  response = @agent.client.invoke(call_options)

  @agent.find_exception(response)
end