Class: WebApiMethod

Inherits:
Object
  • Object
show all
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

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(s) ⇒ 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(s)
  # Contraction of ERB#url_encode method
  s.to_s.b.gsub(/[^\w\-.~]/n){sprintf("%%%02X",_1.unpack1("C"))}
end

#http_response(request) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/web_api/web_api_method.rb', line 84

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



74
75
76
77
78
79
80
81
82
# File 'lib/web_api/web_api_method.rb', line 74

def http_response_body(request)
  response = http_response(request)
  unless OK === response.code.to_i
    $stderr.puts response.code
    $stderr.puts response.body
    raise ResponseError, response.message
  end
  return response.body, response['content-type'].sub(/;.*$/,'')
end

#net_http(request) ⇒ Object



98
99
100
101
102
# File 'lib/web_api/web_api_method.rb', line 98

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/web_api/web_api_method.rb', line 54

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
  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

uri_parse(request: CRStruct) -> N/A Expects request to have url, data, type, and dumper. Sets the request’s scheme, host, port, uri, and payload



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/web_api/web_api_method.rb', line 34

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