uri library simple summary

URI.scheme Returns the scheme as a string.

URI.host Returns the host as a string.

URI.port Returns the port number as a string.

URI.query Returns the query as a string.

URI.path Returns the path as a string. URI.request_uri Returns path +'?' + Query as a string. If query is nil, it returns its own path.

Practice

Let's parse the url of the result screen of the result of searching ruby with Qiita to the URI object.

require 'uri'

uri = URI.parse('https://qiita.com/search?q=ruby')
puts uri.schem
#=> https
puts uri.host
#=> qiita.com
puts uri.port
#=> 443
puts uri.path
#=> /search
puts uri.query
#=> q=ruby
puts uri.request_uri
#=> /search?q=ruby

join Create a URI object by concatenating the character string (variable) passed as an argument.

encode_www_form Generate URL-encoded form data from the value passed as an argument. Private values can be arrays or hashes. Used when passing parameters to uri with GET.


uri = URI.join('https://qiita.com','/search')
puts uri
# => https://qiita.com/search
params = {
name: 'hogehoge',
email: '[email protected]'
}
uri.query = URI.encode_www_form(params)
puts uri
# => https://qiita.com/search?name=hogehoge&email=hogehoge%40hogehoge.com

Recommended Posts

uri library simple summary
[Note] MVC model simple summary
Access control library Casbin Usage summary
Summary