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 or #headers:

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

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 : Params = {} 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?(*args, **options) #

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

[View source]
def delete(suburl : String | Nil = nil, *, form = {} of String => String, headers = {} of String => String, params = {} of String => String) : 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, &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) : 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, &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) : 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, &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 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) : 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, &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 | Symbol, Int32 | String) | Hash(String, Int32 | String) | Hash(String, Int32) | Hash(String, String) | Hash(Symbol, Int32 | String) | Hash(Symbol, Int32) | Hash(Symbol, String) #

[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) : 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, &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) : 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, &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) : 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, &block : Crest::Response -> ) : Nil #

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


[View source]
def url : String #

[View source]
def user : String? #

[View source]