Class: Req::Client
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data ⇒ Object
Returns the value of attribute data.
-
#files ⇒ Object
Returns the value of attribute files.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#auth(user, pass) ⇒ Object
basic authentication.
- #clear ⇒ Object (also: #reset)
- #get(limit = 4) ⇒ Object
- #header(option) ⇒ Object (also: #set)
-
#initialize(url) ⇒ Client
constructor
A new instance of Client.
- #multi(on = true) ⇒ Object
- #query(option) ⇒ Object
- #send(name, file = nil, filename = nil) ⇒ Object
- #type(t) ⇒ Object
- #upload(field, file, filename = nil) ⇒ Object (also: #attach)
- #use_ssl(use = true) ⇒ Object
- #write(body) ⇒ Object
Constructor Details
#initialize(url) ⇒ Client
12 13 14 15 16 17 18 19 20 |
# File 'lib/req/client.rb', line 12 def initialize(url) self.url = url @data = {} @headers = {} @files = [] @body = '' @client = Net::HTTP.new(uri.hostname, uri.port) use_ssl if uri.scheme == "https" end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
9 10 11 |
# File 'lib/req/client.rb', line 9 def body @body end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/req/client.rb', line 10 def client @client end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/req/client.rb', line 9 def data @data end |
#files ⇒ Object
Returns the value of attribute files.
9 10 11 |
# File 'lib/req/client.rb', line 9 def files @files end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/req/client.rb', line 9 def headers @headers end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/req/client.rb', line 10 def url @url end |
Instance Method Details
#auth(user, pass) ⇒ Object
basic authentication
60 61 62 |
# File 'lib/req/client.rb', line 60 def auth user, pass set "Authorization" => "Basic #{Base64.encode64(user + ":" + pass).chop}" end |
#clear ⇒ Object Also known as: reset
107 108 109 110 111 112 113 |
# File 'lib/req/client.rb', line 107 def clear @data = {} @headers = {} @files = [] @body = '' self end |
#get(limit = 4) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/req/client.rb', line 22 def get limit=4 update_uri res = client.get(uri.request_uri, headers) limit.times do break unless res.is_a? Net::HTTPRedirection # reset url and http client in case of the url scheme changed # if no location found, Invalid response! CRUSH DOWN. self.url = res['location'] @client = Net::HTTP.new(uri.hostname, uri.port) use_ssl if uri.scheme == "https" res = client.get(res['location'], headers) end block_given? ? yield(res) : res end |
#header(option) ⇒ Object Also known as: set
84 85 86 87 |
# File 'lib/req/client.rb', line 84 def header option headers.merge! option self end |
#multi(on = true) ⇒ Object
102 103 104 105 |
# File 'lib/req/client.rb', line 102 def multi on=true @multi = on self end |
#query(option) ⇒ Object
79 80 81 82 |
# File 'lib/req/client.rb', line 79 def query option data.merge! option self end |
#send(name, file = nil, filename = nil) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/req/client.rb', line 64 def send name, file=nil, filename=nil if file upload name, file, filename else query name end end |
#type(t) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/req/client.rb', line 95 def type t # Set `Content-Type` header tp = t.to_sym t = TYPE[tp] if TYPE[tp] set "Content-Type" => t end |
#upload(field, file, filename = nil) ⇒ Object Also known as: attach
72 73 74 75 76 |
# File 'lib/req/client.rb', line 72 def upload field, file, filename = nil file = File.open(file) @files << [field, file, filename || file.path] self end |
#use_ssl(use = true) ⇒ Object
54 55 56 57 |
# File 'lib/req/client.rb', line 54 def use_ssl use=true client.use_ssl = use self end |
#write(body) ⇒ Object
90 91 92 93 |
# File 'lib/req/client.rb', line 90 def write body @body << body self end |