Returns the index for the given header, if it exists; otherwise returns nil
.
With the single argument header
, returns the index of the first-found field with the given header
:
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row.index('Name') # => 0 row.index('NAME') # => nil
With arguments header
and offset
, returns the index of the first-found field with given header
, but ignoring the first offset
fields:
row.index('Name', 1) # => 1 row.index('Name', 3) # => nil
Returns an ASCII-compatible String showing:
Class
CSV::Row.
Header-value pairs.
Example:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) row = table[0] row.inspect # => "#<CSV::Row \"Name\":\"foo\" \"Value\":\"0\">"
Returns a US-ASCII
-encoded String showing table:
Class: CSV::Table
.
Access mode: :row
, :col
, or :col_or_row
.
Size: Row
count, including the header row.
Example:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) table.inspect # => "#<CSV::Table mode:col_or_row row_count:4>\nName,Value\nfoo,0\nbar,1\nbaz,2\n"
Winds back to the beginning
Get the URI
of the remote object.
Get the URI
of the remote object.
Posts data to a host; returns a Net::HTTPResponse
object.
Argument url
must be a URL; argument data
must be a string:
_uri = uri.dup _uri.path = '/posts' data = '{"title": "foo", "body": "bar", "userId": 1}' headers = {'content-type': 'application/json'} res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true> puts res.body
Output:
{ "title": "foo", "body": "bar", "userId": 1, "id": 101 }
Related:
Net::HTTP::Post
: request class for HTTP method POST
.
Net::HTTP#post
: convenience method for HTTP method POST
.
Creates a new Net::HTTP object, http
, via Net::HTTP.new:
For arguments address
and port
, see Net::HTTP.new
.
For proxy-defining arguments p_addr
through p_pass
, see Proxy Server.
For argument opts
, see below.
With no block given:
Calls http.start
with no block (see start
), which opens a TCP connection and HTTP session.
Returns http
.
The caller should call finish
to close the session:
http = Net::HTTP.start(hostname) http.started? # => true http.finish http.started? # => false
With a block given:
Calls http.start
with the block (see start
), which:
Opens a TCP connection and HTTP session.
Calls the block, which may make any number of requests to the host.
Closes the HTTP session and TCP connection on block exit.
Returns the block’s value object
.
Returns object
.
Example:
hostname = 'jsonplaceholder.typicode.com' Net::HTTP.start(hostname) do |http| puts http.get('/todos/1').body puts http.get('/todos/2').body end
Output:
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } { "userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false }
If the last argument given is a hash, it is the opts
hash, where each key is a method or accessor to be called, and its value is the value to be set.
The keys may include:
Note: If port
is nil
and opts[:use_ssl]
is a truthy value, the value passed to new
is Net::HTTP.https_default_port
, not port
.
Returns a string representation of self
:
Net::HTTP.new(hostname).inspect # => "#<Net::HTTP jsonplaceholder.typicode.com:80 open=false>"
Returns true
if the HTTP session has been started:
http = Net::HTTP.new(hostname) http.started? # => false http.start http.started? # => true http.finish # => nil http.started? # => false Net::HTTP.start(hostname) do |http| http.started? end # => true http.started? # => false
Starts an HTTP session.
Without a block, returns self
:
http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.start # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=true> http.started? # => true http.finish
With a block, calls the block with self
, finishes the session when the block exits, and returns the block’s value:
http.start do |http| http end # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.started? # => false
Finishes the HTTP session:
http = Net::HTTP.new(hostname) http.start http.started? # => true http.finish # => nil http.started? # => false
Raises IOError
if not in a session.
Sends a POST request to the server; returns an instance of a subclass of Net::HTTPResponse
.
The request is based on the Net::HTTP::Post
object created from string path
, string data
, and initial headers hash initheader
.
With a block given, calls the block with the response body:
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.post('/todos', data) do |res| p res end # => #<Net::HTTPCreated 201 Created readbody=true>
Output:
"{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
With no block given, simply returns the response object:
http.post('/todos', data) # => #<Net::HTTPCreated 201 Created readbody=true>
Related:
Net::HTTP::Post
: request class for HTTP method POST.
Net::HTTP.post
: sends POST request, returns response body.
Sends a PROPFIND request to the server; returns an instance of a subclass of Net::HTTPResponse
.
The request is based on the Net::HTTP::Propfind
object created from string path
, string body
, and initial headers hash initheader
.
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.propfind('/todos/1', data)
Sends a TRACE request to the server; returns an instance of a subclass of Net::HTTPResponse
.
The request is based on the Net::HTTP::Trace
object created from string path
and initial headers hash initheader
.
http = Net::HTTP.new(hostname) http.trace('/todos/1')
Sends the given request req
to the server; forms the response into a Net::HTTPResponse
object.
The given req
must be an instance of a subclass of Net::HTTPRequest. Argument body
should be given only if needed for the request.
With no block given, returns the response object:
http = Net::HTTP.new(hostname) req = Net::HTTP::Get.new('/todos/1') http.request(req) # => #<Net::HTTPOK 200 OK readbody=true> req = Net::HTTP::Post.new('/todos') http.request(req, 'xyzzy') # => #<Net::HTTPCreated 201 Created readbody=true>
With a block given, calls the block with the response and returns the response:
req = Net::HTTP::Get.new('/todos/1') http.request(req) do |res| p res end # => #<Net::HTTPOK 200 OK readbody=true>
Output:
#<Net::HTTPOK 200 OK readbody=false>
Returns a string representation of the request:
Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"