Class: WEB_API::WebApiMethod

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

Direct Known Subclasses

AuthMethod, ClientMethod

Constant Summary collapse

SCHEMES =
['http', 'https']
TYPES =
[:post,  :get]
OK =
200..299
SSL =
{'http' => false, 'https' => true}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, type) ⇒ WebApiMethod

Returns a new instance of WebApiMethod.



13
14
15
16
17
# File 'lib/web_api/web_api.rb', line 13

def initialize(uri, type)
  "Invalid scheme, #{uri.scheme}." unless SCHEMES.include?(uri.scheme)
  "Invalid method type, #{type}."  unless WebApiMethod::TYPES.include?(type)
  @uri, @type, @base, @path = uri, type, "#{uri.scheme}://#{uri.host}", uri.path[1,-1]
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



10
11
12
# File 'lib/web_api/web_api.rb', line 10

def base
  @base
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/web_api/web_api.rb', line 10

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/web_api/web_api.rb', line 11

def type
  @type
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/web_api/web_api.rb', line 11

def uri
  @uri
end

Instance Method Details

#arg_map(arg) ⇒ Object

arg is a hash k=>v,…



33
34
35
# File 'lib/web_api/web_api.rb', line 33

def arg_map(arg)
  arg.map{|kv| kv_map(kv) }.join('&')
end

#args_map(args) ⇒ Object Also known as: data, query_string

args is an array of hashes

[ {k=>v,...}, {k=>v,...}, ...]

The net result of this is a flattened query string

"k=v&k=v..."


41
42
43
44
45
# File 'lib/web_api/web_api.rb', line 41

def args_map(args)
  string = args.select{|arg| arg.length>0}.map{|arg| arg_map(arg)}.join('&')
  WebApi.trace.puts "args_map: #{string}" if WebApi.trace
  return string
end

#call(args) ⇒ Object



84
85
86
# File 'lib/web_api/web_api.rb', line 84

def call(args)
  self.method(@type).call(args)
end

#escape(value) ⇒ Object

Escapes value’s string representation for query string use.



20
21
22
23
# File 'lib/web_api/web_api.rb', line 20

def escape(value)
  #http://rosettacode.org/wiki/URL_encoding#Ruby
  CGI.escape(value.to_s).gsub("+", "%20")
end

#get(args) ⇒ Object



80
81
82
# File 'lib/web_api/web_api.rb', line 80

def get(args)
  parse http.get(pathquery(args), headers(args))
end

#headers(args) ⇒ Object



49
50
51
52
53
# File 'lib/web_api/web_api.rb', line 49

def headers(args)
  headers = {'User-Agent' => "ruby gem web_api #{VERSION}"}
  WebApi.trace.puts "headers:\n#{headers}" if WebApi.trace
  return headers
end

#httpObject



62
63
64
65
66
# File 'lib/web_api/web_api.rb', line 62

def http
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = SSL[@uri.scheme]
  return http
end

#kv_map(kv) ⇒ Object

kv is a key, value pair

[k, v]


27
28
29
# File 'lib/web_api/web_api.rb', line 27

def kv_map(kv)
  kv.map{|value| escape(value)}.join('=')
end

#parse(response) ⇒ Object

Raises:



56
57
58
59
# File 'lib/web_api/web_api.rb', line 56

def parse(response)
  raise ResponseError, response.message unless OK === response.code.to_i
  response.body
end

#pathquery(args) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/web_api/web_api.rb', line 72

def pathquery(args)
  p, q0, q1 = @uri.path, @uri.query, query_string(args)
  q = [q0, q1].select{|q| q && q.length>0}.join('&')
  pq = (q.length>0)? p + '?' + q : p
  WebApi.trace.puts "pathquery #{pq}" if WebApi.trace
  return pq
end

#post(args) ⇒ Object



68
69
70
# File 'lib/web_api/web_api.rb', line 68

def post(args)
  parse http.post(@uri.path, data(args), headers(args))
end