class

Mechanize

Inherits Reference / Object

This class is main class of Mechanize.cr, using this class' instance to start web interaction.

Examples:

# GET
agent = Mechanize.new
# send GET request to http://example.com/?foo=bar
agent.get("http://example.com",
  params: {"foo" => "bar"},
  headers: HTTP::Headers{"Foo" => "Bar"})
# POST
# send POST request whose post body is "foo=bar"
agent = Mechanize.new
agent.post("http://example.com",
  query: {"foo" => "bar"})

Constants

Log = ::Log.for(self)
USER_AGENT = {"Mechanize" => "Mechanize/#{VERSION} Crystal/#{Crystal::VERSION} (https://github.com/Kanezoh/mechanize.cr)"}
VERSION = "0.2.0"

Constructors

Instance methods

add_auth(uri : String, user : String, pass : String)

set basic auth credentials.

# set an auth credential with a specific url.
agent.add_auth("http://example.com", "username", "password")
Source
add_to_history(page)

add page to history (Mechanize::History).

if you send request, mechanize calls this method and records page, so you don't need to call this on your own.

Source
back

get the latest page recorded in history, and the page is deleted from history.

agent.back => #<Mechanize::Page>
Source
current_page

get the page mechanize last visited.

agent.current_page => #<Mechanize::Page>
Source
delete(uri : String | URI, body : String | Nil, headers = ::HTTP::Headers.new) : Mechanize::Page

Send DELETE request to specified uri with headers, and body.

Examples (send delete request whose body is "hello")

agent = Mechanize.new
agent.delete("http://example.com",
  body: "hello")
Source
download(uri : URI | String, filename : String, headers = ::HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String, String).new)

download page body from given uri.

# make download.html whose content is http://example.com's html.
agent.download("http://example.com", "download.html")
Source
get(uri : String | URI, headers = ::HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String, String).new) : Mechanize::Page

Send GET request to specified uri with headers, and parameters.

Examples (send request to http://example.com/?foo=bar)

agent = Mechanize.new
agent.get("http://example.com",
  params: {"foo" => "bar"},
  headers: HTTP::Headers{"Foo" => "Bar"})
Source
head(uri : String | URI, headers = ::HTTP::Headers.new) : Mechanize::Page

Send HEAD request to specified uri with headers.

Examples (send HEAD request to http://example.com/)

agent = Mechanize.new
agent.head("http://example.com")
Source
history

get the history (Mechanize::History). the requests mechanize send is recorded in this history.

agent.history => #<Mechanize::History>
Source
max_history

Get maximum number of pages allowed in the history. The default setting is 100 pages.

agent.max_history # => 100
Source
max_history=(length : Int32)

set maximum number of pages allowed in the history. the default value is 100.

agent.max_history = 150
Source
parse(uri, response, body)

parse response. it is used internally.

Source
post(uri : String | URI, headers = ::HTTP::Headers.new, query : Hash(String, String | Array(String)) = Hash(String, String).new) : Mechanize::Page

Send POST request to specified uri with headers, and query.

Examples (send post request whose post body is "foo=bar")

agent = Mechanize.new
agent.post("http://example.com",
  query: {"foo" => "bar"},
  headers: HTTP::Headers{"Foo" => "Bar"})
Source
put(uri : String | URI, body : String | Nil, headers = ::HTTP::Headers.new) : Mechanize::Page

Send PUT request to specified uri with headers, and body.

Examples (send put request whose body is "hello")

agent = Mechanize.new
agent.put("http://example.com",
  body: "hello")
Source
request_headers

get the value of request headers.

agent.request_headers # => HTTP::Headers{"Foo" => "Bar"}
Source
request_headers=(request_headers : ::HTTP::Headers)

set the value of request headers.

agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
Source
submit(form, button = nil) : Mechanize::Page | Nil

send request from form. 'form' args is necessary, but 'button' is optional. you can specify button if you want to use other button which is not in the form. TODO: now only supports POST. other HTTP methods should be supported.

page = agent.get("http://example.com")
form = page.forms[0]
# set field value
form.field_with("foo").value = "bar"
agent.submit(form)
Source
user_agent

get the value of user agent.

agent.user_agent # => "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko"
Source
user_agent=(user_agent : String)

set the value of user agent.

agent.user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko"
Source

Nested types