Class: WEB_API::WebApiMethod
- Inherits:
-
Object
- Object
- WEB_API::WebApiMethod
- Defined in:
- lib/web_api/web_api.rb
Direct Known Subclasses
Constant Summary collapse
- SCHEMES =
['http', 'https']
- TYPES =
[:post, :get]
- OK =
'200'- SSL =
{'http' => false, 'https' => true}
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#path ⇒ Object
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#arg_map(arg) ⇒ Object
arg is a hash k=>v,….
-
#args_map(args) ⇒ Object
(also: #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…”.
- #call(args) ⇒ Object
-
#escape(value) ⇒ Object
Escapes value’s string representation for query string use.
- #get(args) ⇒ Object
- #headers(args) ⇒ Object
- #http ⇒ Object
-
#initialize(uri, type) ⇒ WebApiMethod
constructor
A new instance of WebApiMethod.
-
#kv_map(kv) ⇒ Object
kv is a key, value pair [k, v].
- #parse(response) ⇒ Object
- #pathquery(args) ⇒ Object
- #post(args) ⇒ Object
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
#base ⇒ Object
Returns the value of attribute base.
10 11 12 |
# File 'lib/web_api/web_api.rb', line 10 def base @base end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/web_api/web_api.rb', line 10 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/web_api/web_api.rb', line 11 def type @type end |
#uri ⇒ Object (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,…
32 33 34 |
# File 'lib/web_api/web_api.rb', line 32 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..."
40 41 42 43 44 |
# File 'lib/web_api/web_api.rb', line 40 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
83 84 85 |
# File 'lib/web_api/web_api.rb', line 83 def call(args) self.method(@type).call(args) end |
#escape(value) ⇒ Object
Escapes value’s string representation for query string use.
20 21 22 |
# File 'lib/web_api/web_api.rb', line 20 def escape(value) URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |
#get(args) ⇒ Object
79 80 81 |
# File 'lib/web_api/web_api.rb', line 79 def get(args) parse http.get(pathquery(args), headers(args)) end |
#headers(args) ⇒ Object
48 49 50 51 52 |
# File 'lib/web_api/web_api.rb', line 48 def headers(args) headers = {'User-Agent' => "ruby gem web_api #{VERSION}"} WebApi.trace.puts "headers:\n#{headers}" if WebApi.trace return headers end |
#http ⇒ Object
61 62 63 64 65 |
# File 'lib/web_api/web_api.rb', line 61 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]
26 27 28 |
# File 'lib/web_api/web_api.rb', line 26 def kv_map(kv) kv.map{|value| escape(value)}.join('=') end |
#parse(response) ⇒ Object
55 56 57 58 |
# File 'lib/web_api/web_api.rb', line 55 def parse(response) raise ResponseError, response. unless response.code == OK response.body end |
#pathquery(args) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/web_api/web_api.rb', line 71 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
67 68 69 |
# File 'lib/web_api/web_api.rb', line 67 def post(args) parse http.post(@uri.path, data(args), headers(args)) end |