Class: Patchboard::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/patchboard/action.rb

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patchboard, name, definition) ⇒ Action

Returns a new instance of Action.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/patchboard/action.rb', line 9

def initialize(patchboard, name, definition)
  @name = name
  @patchboard = patchboard
  @api = @patchboard.api
  @schema_manager = @patchboard.schema_manager
  @method = definition[:method]


  @headers = {}

  request, response = definition[:request], definition[:response]

  if request
    if schemes = request[:authorization]
      @auth_schemes = schemes.is_a?(String) ? [schemes] : schemes
    end
    if request[:type]
      @headers["Content-Type"] = request[:type]
      @request_schema = @schema_manager.find :media_type => request[:type]
    end
  end

  if response && response[:type]
    @headers["Accept"] = response[:type]
    @response_schema = @schema_manager.find :media_type => response[:type]
  end
  @status = response[:status] || 200
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/patchboard/action.rb', line 7

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/patchboard/action.rb', line 7

def method
  @method
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/patchboard/action.rb', line 7

def status
  @status
end

Instance Method Details

#httpObject



38
39
40
# File 'lib/patchboard/action.rb', line 38

def http
  @http ||= @patchboard.http
end

#prepare_request(resource, url, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/patchboard/action.rb', line 55

def prepare_request(resource, url, *args)
  context = resource.context
  headers = {}.merge(@headers)
  options = {
    :url => url, :method => @method, :headers => headers
  }

  input_options = self.process_args(args)

  if @auth_schemes && context.respond_to?(:authorizer)
    # Ugly hack to maintain backwards compatibility with existing 
    # implementations of context.authorizer
    scheme, credential = begin
      context.authorizer(
        :schemes => @auth_schemes, 
        :resource => resource, :action => @name,
        :request => input_options
      )
    rescue ArgumentError
      context.authorizer(@auth_schemes, resource, @name)
    end

    headers["Authorization"] = "#{scheme} #{credential}"
  end

  if input_options[:body]
    options[:body] = input_options[:body]
  end
  # This code looks forward to the time when we have figured out
  # how we want Patchboard clients to take extra arguments for
  # requests.  Leaving it here now to show why process_args returns a Hash,
  # not just the body.
  if input_options[:headers]
    options[:headers].merge!(input_options[:headers])
  end
  options
end

#process_args(args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/patchboard/action.rb', line 93

def process_args(args)
  options = {}
  signature = args.map {|arg| arg.class.to_s }.join(".")
  if @request_schema
    case signature
    when "String"
      options[:body] = args[0]
    when "Hash", "Array"
      options[:body] = args[0].to_json
    else
      raise "Invalid arguments for action: request content is required"
    end
  else
    case signature
    when ""
    else
      raise "Invalid arguments for action"
    end
  end
  options
end

#request(resource, url, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/patchboard/action.rb', line 42

def request(resource, url, *args)
  options = self.prepare_request(resource, url, *args)
  raw = self.http.request @method, url, options.merge(:response => :object)
  response = Response.new(raw)
  if response.status != @status
    raise ResponseError.new(response),
      "Unexpected response status: #{response.status} - #{response.body}"
  end
  out = @api.decorate(resource.context, @response_schema, response.data)
  out.response = response
  out
end