Class: Nestful::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nestful/connection.rb

Constant Summary collapse

HTTP_FORMAT_HEADER_NAMES =
{  
  :get => 'Accept',
  :put => 'Content-Type',
  :post => 'Content-Type',
  :delete => 'Accept',
  :head => 'Accept'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, format = Formats::XmlFormat.new) ⇒ Connection

The site parameter is required and will set the site attribute to the URI for the remote resource service.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
# File 'lib/nestful/connection.rb', line 28

def initialize(site, format = Formats::XmlFormat.new)
  raise ArgumentError, 'Missing site URI' unless site
  @user = @password = nil
  @uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
  self.site = site
  self.format = format
end

Instance Attribute Details

#auth_typeObject

Returns the value of attribute auth_type.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def auth_type
  @auth_type
end

#formatObject

Returns the value of attribute format.



18
19
20
# File 'lib/nestful/connection.rb', line 18

def format
  @format
end

#passwordObject

Returns the value of attribute password.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def password
  @password
end

#proxyObject

Returns the value of attribute proxy.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def proxy
  @proxy
end

#siteObject

Returns the value of attribute site.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def site
  @site
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def timeout
  @timeout
end

#userObject

Returns the value of attribute user.



17
18
19
# File 'lib/nestful/connection.rb', line 17

def user
  @user
end

Class Method Details

.requestsObject



21
22
23
# File 'lib/nestful/connection.rb', line 21

def requests
  @@requests ||= []
end

Instance Method Details

#delete(path, headers = {}, &block) ⇒ Object

Executes a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.



81
82
83
# File 'lib/nestful/connection.rb', line 81

def delete(path, headers = {}, &block)
  with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path)), &block) }
end

#get(path, headers = {}, &block) ⇒ Object

Executes a GET request. Used to get (find) resources.



75
76
77
# File 'lib/nestful/connection.rb', line 75

def get(path, headers = {}, &block)
  with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path)), &block) }
end

#head(path, headers = {}, &block) ⇒ Object

Executes a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).



99
100
101
# File 'lib/nestful/connection.rb', line 99

def head(path, headers = {}, &block)
  with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path)), &block) }
end

#post(path, body = '', headers = {}, &block) ⇒ Object

Executes a POST request. Used to create new resources.



93
94
95
# File 'lib/nestful/connection.rb', line 93

def post(path, body = '', headers = {}, &block)
  with_auth { request(:post, path, body, build_request_headers(headers, :post, self.site.merge(path)), &block) }
end

#put(path, body = '', headers = {}, &block) ⇒ Object

Executes a PUT request (see HTTP protocol documentation if unfamiliar). Used to update resources.



87
88
89
# File 'lib/nestful/connection.rb', line 87

def put(path, body = '', headers = {}, &block)
  with_auth { request(:put, path, body, build_request_headers(headers, :put, self.site.merge(path)), &block) }
end