Class: WebApiMethod
- Inherits:
-
Object
- Object
- 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.
26 27 28 29 |
# File 'lib/web_api/web_api_method.rb', line 26 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.
10 11 12 13 |
# File 'lib/web_api/web_api_method.rb', line 10 def escape(value) #http://rosettacode.org/wiki/URL_encoding#Ruby CGI.escape(value.to_s).gsub("+", "%20") end |
#http_response(request) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/web_api/web_api_method.rb', line 77 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 WebApi." end end |
#http_response_body(request) ⇒ Object
71 72 73 74 75 |
# File 'lib/web_api/web_api_method.rb', line 71 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
91 92 93 94 95 |
# File 'lib/web_api/web_api_method.rb', line 91 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
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/web_api/web_api_method.rb', line 15 def query_string(ah) case ah when Array ah.select{!_1.empty?}.map{query_string _1}.join('&') when Hash ah.map{"#{_1}=#{escape _2}"}.join('&') else ah.to_s end end |
#run(extension, type, data, header, dumper, parser, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/web_api/web_api_method.rb', line 51 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/web_api/web_api_method.rb', line 31 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 |