class Crest::Resource

Overview

A class that can be instantiated for access to a RESTful resource, including authentication, proxy and logging.

Simple example:

resource = Crest::Resource.new("https://httpbin.org/get")
response = resource.get

Block style:

resource = Crest::Resource.new("http://httpbin.org") do |res|
  res.headers.merge!({"foo" => "bar"})
end

response = resource["/headers"].get

With HTTP basic authentication:

resource = Crest::Resource.new("https://httpbin.org/get", user: "user", password: "password")

Use the #[] syntax to allocate subresources:

resource = Crest::Resource.new("https://httpbin.org")
resource["/get"].get

You can pass advanced parameters like default #params, #headers, or #cookies:

resource = Crest::Resource.new(
  "https://httpbin.org",
  params: {"key" => "key"},
  headers: {"Content-Type" => "application/json"},
  cookies: {"lang"=> "ua"}
)
response = response["/post"].post(
  form: {:height => 100, "width" => "100"},
  params: {:secret => "secret"},
  cookies: {"locale"=> "en_US"}
)

If you want to stream the data from the response you can pass a block:

resource = Crest::Resource.new("http://httpbin.org")
resource["/stream/5"].get do |response|
  while line = response.body_io.gets
    puts line
  end
end

Defined in:

crest/resource.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(url : String, *, headers : Hash(String, String) = {} of String => String, params = {} of String => String, cookies = {} of String => String, **options, &) #

[View source]
def self.new(url : String, **args) #

When block is not given.


[View source]

Instance Method Detail

def [](suburl) #

[View source]
def close(*args, **options) #

[View source]
def close(*args, **options, &) #

[View source]
def close_connection : Bool #

[View source]
def closed? #

[View source]
def connect_timeout : Float32 | Int32 | Time::Span | Nil #

[View source]
def cookies : Hash(String, Bool | Float32 | Float64 | IO | Int32 | Int64 | String | Symbol | Nil) #

[View source]
def delete(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a DELETE request and returns a Crest::Response.


[View source]
def delete(form = {} of String => String, **args) : Crest::Response #

Execute a DELETE request and returns a Crest::Response.


[View source]
def delete(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a DELETE request and and yields the Crest::Response to the block.


[View source]
def delete(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a DELETE request and and yields the Crest::Response to the block.


[View source]
def get(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a GET request and returns a Crest::Response.


[View source]
def get(form = {} of String => String, **args) : Crest::Response #

Execute a GET request and returns a Crest::Response.


[View source]
def get(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a GET request and and yields the Crest::Response to the block.


[View source]
def get(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a GET request and and yields the Crest::Response to the block.


[View source]
def handle_errors : Bool #

[View source]
def head(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a HEAD request and returns a Crest::Response.


[View source]
def head(form = {} of String => String, **args) : Crest::Response #

Execute a HEAD request and returns a Crest::Response.


[View source]
def head(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a HEAD request and and yields the Crest::Response to the block.


[View source]
def head(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a HEAD request and and yields the Crest::Response to the block.


[View source]
def headers : Hash(String, String) #

[View source]
def http_client : HTTP::Client #

[View source]
def json : Bool #

[View source]
def logger : Crest::Logger #

[View source]
def logging : Bool #

[View source]
def options(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a OPTIONS request and returns a Crest::Response.


[View source]
def options(form = {} of String => String, **args) : Crest::Response #

Execute a OPTIONS request and returns a Crest::Response.


[View source]
def options(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a OPTIONS request and and yields the Crest::Response to the block.


[View source]
def options(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a OPTIONS request and and yields the Crest::Response to the block.


[View source]
def p_addr : String? #

[View source]
def p_pass : String? #

[View source]
def p_port : Int32? #

[View source]
def p_user : String? #

[View source]
def params : Hash(String, Bool | Float32 | Float64 | IO | Int32 | Int64 | String | Symbol | Nil) #

[View source]
def password : String? #

[View source]
def patch(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a PATCH request and returns a Crest::Response.


[View source]
def patch(form = {} of String => String, **args) : Crest::Response #

Execute a PATCH request and returns a Crest::Response.


[View source]
def patch(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a PATCH request and and yields the Crest::Response to the block.


[View source]
def patch(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a PATCH request and and yields the Crest::Response to the block.


[View source]
def post(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a POST request and returns a Crest::Response.


[View source]
def post(form = {} of String => String, **args) : Crest::Response #

Execute a POST request and returns a Crest::Response.


[View source]
def post(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a POST request and and yields the Crest::Response to the block.


[View source]
def post(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a POST request and and yields the Crest::Response to the block.


[View source]
def put(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String) : Crest::Response #

Execute a PUT request and returns a Crest::Response.


[View source]
def put(form = {} of String => String, **args) : Crest::Response #

Execute a PUT request and returns a Crest::Response.


[View source]
def put(suburl : String | Nil = nil, form = {} of String => String, *, headers = {} of String => String, params = {} of String => String, cookies = {} of String => String, &block : Crest::Response -> ) : Nil #

Execute a PUT request and and yields the Crest::Response to the block.


[View source]
def put(form = {} of String => String, **args, &block : Crest::Response -> ) : Nil #

Execute a PUT request and and yields the Crest::Response to the block.


[View source]
def read_timeout : Float32 | Int32 | Time::Span | Nil #

[View source]
def url : String #

[View source]
def user : String? #

[View source]
def user_agent : String? #

[View source]
def write_timeout : Float32 | Int32 | Time::Span | Nil #

[View source]