Class: WEB_API::WebApiMethod
- Inherits:
-
Object
- Object
- WEB_API::WebApiMethod
- Defined in:
- lib/web_api/web_api_method.rb
Defined Under Namespace
Classes: ResponseError, UnsupportedMethodError
Constant Summary collapse
- OK =
200..299
Instance Method Summary collapse
-
#escape(value) ⇒ Object
Escapes value’s string representation for query string use.
- #http_response(request) ⇒ Object
- #http_response_body(request) ⇒ Object
-
#initialize(target, type, data, header, dumper, parser, &block) ⇒ WebApiMethod
constructor
A new instance of WebApiMethod.
- #net_http(request) ⇒ Object
- #query_string(ah) ⇒ Object
- #run(extension, type, data, header, dumper, parser, &block) ⇒ Object
- #uri_parse(request) ⇒ Object
Constructor Details
#initialize(target, type, data, header, dumper, parser, &block) ⇒ WebApiMethod
Returns a new instance of WebApiMethod.
30 31 32 33 |
# File 'lib/web_api/web_api_method.rb', line 30 def initialize(target, type, data, header, dumper, parser, &block) @target, @type, @data, @header, @dumper, @parser, @block = target, type, data, header, dumper, parser, block end |
Instance Method Details
#escape(value) ⇒ Object
Escapes value’s string representation for query string use.
11 12 13 14 |
# File 'lib/web_api/web_api_method.rb', line 11 def escape(value) #http://rosettacode.org/wiki/URL_encoding#Ruby CGI.escape(value.to_s).gsub("+", "%20") end |
#http_response(request) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/web_api/web_api_method.rb', line 81 def http_response(request) http = net_http(request) case request.type when :get, :delete, :head, :copy, :move, :options, :trace http.method(request.type).call request.uri, request.header when :post, :patch, :lock, :unlock, :mkcol, :propfind, :proppatch http.method(request.type).call request.uri, request.payload, request.header else raise UnsupportedMethodError, "Method type #{type}(#{type.class}) not supported by WEB_API::WebApi." end end |
#http_response_body(request) ⇒ Object
75 76 77 78 79 |
# File 'lib/web_api/web_api_method.rb', line 75 def http_response_body(request) response = http_response(request) raise ResponseError, response. unless OK === response.code.to_i return response.body, response['content-type'].sub(/;.*$/,'') end |
#net_http(request) ⇒ Object
93 94 95 96 97 |
# File 'lib/web_api/web_api_method.rb', line 93 def net_http(request) http = Net::HTTP.new(request.host, request.port) http.use_ssl = request.scheme=='https' return http end |
#query_string(ah) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/web_api/web_api_method.rb', line 16 def query_string(ah) case ah when Array ah.select{|h| !h.empty?}.map{|h| query_string(h)}.join('&') when Hash ah.map do |k,v| "#{k}=#{escape v}" end .join('&') else ah.to_s end end |
#run(extension, type, data, header, dumper, parser, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/web_api/web_api_method.rb', line 55 def run(extension, type, data, header, dumper, parser, &block) request = CRStruct::Open.new request.url = @target+extension request.type = type || @type request.data = @data.merge(data) request.header = @header.merge(header) request.dumper = dumper || @dumper parser = parser || @parser block = block || @block uri_parse(request) block.call(request) if block body, content = http_response_body(request) parser = parser[content] if parser.is_a? Hash body = parser.call body if parser and parser!=:none return body end |
#uri_parse(request) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/web_api/web_api_method.rb', line 35 def uri_parse(request) uri = URI.parse(request.url) request_uri = uri.request_uri payload = '' unless request.data.empty? if request.type == :post if dumper=request.dumper and (dumper!=:none) payload = dumper.call(request.data) else payload = query_string(request.data) end else payload = query_string(request.data) request_uri += (uri.query ? '&' : '?') + payload end end request.scheme, request.host, request.port, request.uri, request.payload = uri.scheme, uri.host, uri.port, request_uri, payload end |