Class: Motion::HTTP::Client
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #add_header(key, value) ⇒ Object
- #auth(header_value) ⇒ Object
- #basic_auth(username, password) ⇒ Object
- #delete(path, options = nil, &callback) ⇒ Object
- #get(path, options = nil, &callback) ⇒ Object
- #head(path, options = nil, &callback) ⇒ Object
- #header(key, value) ⇒ Object
- #headers(hash = nil) ⇒ Object
-
#initialize(base_url = nil, options = nil) ⇒ Client
constructor
A new instance of Client.
- #options(path, options = nil, &callback) ⇒ Object
- #patch(path, options = nil, &callback) ⇒ Object
- #post(path, options = nil, &callback) ⇒ Object
- #put(path, options = nil, &callback) ⇒ Object
- #request(http_method, path, options = nil, &callback) ⇒ Object
- #trace(path, options = nil, &callback) ⇒ Object
Constructor Details
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
4 5 6 |
# File 'lib/common/http/client.rb', line 4 def base_url @base_url end |
Instance Method Details
#add_header(key, value) ⇒ Object
16 17 18 |
# File 'lib/common/http/client.rb', line 16 def add_header(key, value) @headers.add(key, value) end |
#auth(header_value) ⇒ Object
35 36 37 38 |
# File 'lib/common/http/client.rb', line 35 def auth(header_value) @headers.set 'Authorization', header_value self end |
#basic_auth(username, password) ⇒ Object
29 30 31 32 33 |
# File 'lib/common/http/client.rb', line 29 def basic_auth(username, password) header_value = 'Basic ' + Base64.encode("#{username}:#{password}") auth(header_value) self end |
#delete(path, options = nil, &callback) ⇒ Object
56 57 58 |
# File 'lib/common/http/client.rb', line 56 def delete(path, = nil, &callback) request(:delete, path, , &callback) end |
#get(path, options = nil, &callback) ⇒ Object
40 41 42 |
# File 'lib/common/http/client.rb', line 40 def get(path, = nil, &callback) request(:get, path, , &callback) end |
#head(path, options = nil, &callback) ⇒ Object
60 61 62 |
# File 'lib/common/http/client.rb', line 60 def head(path, = nil, &callback) request(:head, path, , &callback) end |
#header(key, value) ⇒ Object
12 13 14 |
# File 'lib/common/http/client.rb', line 12 def header(key, value) @headers.set(key, value) end |
#headers(hash = nil) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/common/http/client.rb', line 20 def headers(hash = nil) if hash hash.each do |key, value| @headers.set(key, value) end end @headers end |
#options(path, options = nil, &callback) ⇒ Object
64 65 66 |
# File 'lib/common/http/client.rb', line 64 def (path, = nil, &callback) request(:options, path, , &callback) end |
#patch(path, options = nil, &callback) ⇒ Object
52 53 54 |
# File 'lib/common/http/client.rb', line 52 def patch(path, = nil, &callback) request(:patch, path, , &callback) end |
#post(path, options = nil, &callback) ⇒ Object
44 45 46 |
# File 'lib/common/http/client.rb', line 44 def post(path, = nil, &callback) request(:post, path, , &callback) end |
#put(path, options = nil, &callback) ⇒ Object
48 49 50 |
# File 'lib/common/http/client.rb', line 48 def put(path, = nil, &callback) request(:put, path, , &callback) end |
#request(http_method, path, options = nil, &callback) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/common/http/client.rb', line 72 def request(http_method, path, = nil, &callback) ||= {} headers_dup = headers.dup if [:headers] .delete(:headers).each {|key, value| headers_dup.set(key, value) } end [:headers] = headers_dup Request.new(http_method, base_url + path, ).perform(&callback) end |
#trace(path, options = nil, &callback) ⇒ Object
68 69 70 |
# File 'lib/common/http/client.rb', line 68 def trace(path, = nil, &callback) request(:trace, path, , &callback) end |