Class: Patchboard::Action
- Inherits:
-
Object
- Object
- Patchboard::Action
- Defined in:
- lib/patchboard/action.rb
Defined Under Namespace
Classes: ResponseError
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #http ⇒ Object
-
#initialize(patchboard, name, definition) ⇒ Action
constructor
A new instance of Action.
- #prepare_request(resource, url, *args) ⇒ Object
- #process_args(args) ⇒ Object
- #request(resource, url, *args) ⇒ Object
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/patchboard/action.rb', line 7 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/patchboard/action.rb', line 7 def method @method end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/patchboard/action.rb', line 7 def status @status end |
Instance Method Details
#http ⇒ Object
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) = { :url => url, :method => @method, :headers => headers } = 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.( :schemes => @auth_schemes, :resource => resource, :action => @name, :request => ) rescue ArgumentError context.(@auth_schemes, resource, @name) end headers["Authorization"] = "#{scheme} #{credential}" end if [:body] [:body] = [: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 [:headers] [:headers].merge!([:headers]) end 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) = {} signature = args.map {|arg| arg.class.to_s }.join(".") if @request_schema case signature when "String" [:body] = args[0] when "Hash", "Array" [: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 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) = self.prepare_request(resource, url, *args) raw = self.http.request @method, url, .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 |