Module: IMW::Schemes::HTTP

Defined in:
lib/imw/schemes/http.rb

Overview

Defines methods for accessing a resource over HTTP. Uses RestClient to implement the basic HTTP verbs (GET, POST, PUT, DELETE, HEAD).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object

Many websites have HTML content without an .html extension so automatically extend obj with IMW::Resources::Formats::HTML in this case.



12
13
14
# File 'lib/imw/schemes/http.rb', line 12

def self.extended obj
  obj.extend(IMW::Formats::Html) if obj.extension.blank?
end

Instance Method Details

#cp(new_uri) ⇒ IMW::Resource

Copy this resource to the new_uri.

Parameters:

Returns:



27
28
29
# File 'lib/imw/schemes/http.rb', line 27

def cp new_uri
  IMW::Tools::Transferer.new(:cp, self, new_uri).transfer!
end

#delete(headers = {}) {|RestClient::Response| ... } ⇒ RestClient::Response

Send a DELETE request to this resource’s URI.

If the response doesn’t have HTTP code 2xx, a RestClient error will be raised.

If a block is given then the response will be passed to the block, even in case of a non-2xx code.

See the documentation for RestClient for more information.

Parameters:

  • headers (Hash) (defaults to: {})

    the headers to include in the request

Yields:

  • (RestClient::Response)

    the response from the server

Returns:

  • (RestClient::Response)

    the response from the server

Raises:

  • (RestClient::NotModified, RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::RequestFailed)

    error from RestClient on non-2xx response codes



125
126
127
128
129
# File 'lib/imw/schemes/http.rb', line 125

def delete headers={}, &block
  make_restclient_request do
    RestClient.delete(uri.to_s, headers, &block)
  end
end

#effective_basenameString

Return the basename of the URI or _index if it’s blank, as in the case of http://www.google.com.

Returns:



35
36
37
# File 'lib/imw/schemes/http.rb', line 35

def effective_basename
  (basename.blank? || basename =~ %r{^/*$}) ? "_index" : basename
end

#get(headers = {}) {|RestClient::Response| ... } ⇒ RestClient::Response

Send a GET request to this resource’s URI.

If the response doesn’t have HTTP code 2xx, a RestClient error will be raised.

If a block is given then the response will be passed to the block, even in case of a non-2xx code.

See the documentation for RestClient for more information.

Parameters:

  • headers (Hash) (defaults to: {})

    the headers to include in the request

Yields:

  • (RestClient::Response)

    the response from the server

Returns:

  • (RestClient::Response)

    the response from the server

Raises:

  • (RestClient::NotModified, RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::RequestFailed)

    error from RestClient on non-2xx response codes



55
56
57
58
59
# File 'lib/imw/schemes/http.rb', line 55

def get headers={}, &block
  make_restclient_request do
    RestClient.get(uri.to_s, headers, &block)
  end
end

#head(headers = {}) {|RestClient::Response| ... } ⇒ RestClient::Response

Send a HEAD request to this resource’s URI.

If the response doesn’t have HTTP code 2xx, a RestClient error will be raised.

If a block is given then the response will be passed to the block, even in case of a non-2xx code.

See the documentation for RestClient for more information.

Parameters:

  • headers (Hash) (defaults to: {})

    the headers to include in the request

Yields:

  • (RestClient::Response)

    the response from the server

Returns:

  • (RestClient::Response)

    the response from the server

Raises:

  • (RestClient::NotModified, RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::RequestFailed)

    error from RestClient on non-2xx response codes



147
148
149
150
151
# File 'lib/imw/schemes/http.rb', line 147

def head headers={}, &block
  make_restclient_request do
    RestClient.head(uri.to_s, headers, &block)
  end
end

#post(payload, headers = {}) {|RestClient::Response| ... } ⇒ RestClient::Response

Send a POST request to this resource’s URI with data payload.

If the response doesn’t have HTTP code 2xx, a RestClient error will be raised.

If a block is given then the response will be passed to the block, even in case of a non-2xx code.

See the documentation for RestClient for more information.

Parameters:

  • payload (Hash, String)

    the data to send

  • headers (Hash) (defaults to: {})

    the headers to include in the request

Yields:

  • (RestClient::Response)

    the response from the server

Returns:

  • (RestClient::Response)

    the response from the server

Raises:

  • (RestClient::NotModified, RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::RequestFailed)

    error from RestClient on non-2xx response codes



79
80
81
82
83
# File 'lib/imw/schemes/http.rb', line 79

def post payload, headers={}, &block
  make_restclient_request do
    RestClient.post(uri.to_s, payload, headers, &block)
  end
end

#put(payload, headers = {}) {|RestClient::Response| ... } ⇒ RestClient::Response

Send a PUT request to this resource’s URI with data payload.

If the response doesn’t have HTTP code 2xx, a RestClient error will be raised.

If a block is given then the response will be passed to the block, even in case of a non-2xx code.

See the documentation for RestClient for more information.

Parameters:

  • payload (Hash, String)

    the data to send

  • headers (Hash) (defaults to: {})

    the headers to include in the request

Yields:

  • (RestClient::Response)

    the response from the server

Returns:

  • (RestClient::Response)

    the response from the server

Raises:

  • (RestClient::NotModified, RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::RequestFailed)

    error from RestClient on non-2xx response codes



103
104
105
106
107
# File 'lib/imw/schemes/http.rb', line 103

def put payload, headers={}, &block
  make_restclient_request do
    RestClient.put(uri.to_s, payload, headers, &block)
  end
end

#via_http?true, false

Is this resource being accessed via HTTP?

Returns:

  • (true, false)


19
20
21
# File 'lib/imw/schemes/http.rb', line 19

def via_http?
  true
end