Class: OldApiResource::Connection

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

Overview

Class to handle connections to remote web services. This class is used by ActiveResource::Base to interface with REST services.

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 = OldApiResource::Formats::JsonFormat) ⇒ Connection

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

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
# File 'lib/old_api_resource/connection.rb', line 32

def initialize(site, format = OldApiResource::Formats::JsonFormat)
  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 (readonly)

Returns the value of attribute auth_type.



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

def auth_type
  @auth_type
end

#formatObject

Returns the value of attribute format.



22
23
24
# File 'lib/old_api_resource/connection.rb', line 22

def format
  @format
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#proxyObject (readonly)

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

#ssl_optionsObject (readonly)

Returns the value of attribute ssl_options.



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

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.requestsObject



25
26
27
# File 'lib/old_api_resource/connection.rb', line 25

def requests
  @@requests ||= []
end

Instance Method Details

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



56
57
58
59
# File 'lib/old_api_resource/connection.rb', line 56

def delete(path, headers = {})
  request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path)))
  return true
end

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



52
53
54
# File 'lib/old_api_resource/connection.rb', line 52

def get(path, headers = {})
  format.decode(request(:get, path, build_request_headers(headers, :get, self.site.merge(path))))
end

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



61
62
63
# File 'lib/old_api_resource/connection.rb', line 61

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

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



75
76
77
78
79
80
81
# File 'lib/old_api_resource/connection.rb', line 75

def post(path, body = {}, headers = {})
  if !body.is_a?(String) && RestClient::Payload.has_file?(body)
    format.decode(request(:post, path, body, build_request_headers(headers, :post, self.site.merge(path))))
  else
    format.decode(request(:post, path, body, build_request_headers(headers, :post, self.site.merge(path))))
  end
end

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



66
67
68
69
70
71
72
73
# File 'lib/old_api_resource/connection.rb', line 66

def put(path, body = {}, headers = {})
  # If there's a file to send then we can't use JSON or XML
  if !body.is_a?(String) && RestClient::Payload.has_file?(body)
    format.decode(request(:put, path, body, build_request_headers(headers, :put, self.site.merge(path))))
  else
    format.decode(request(:put, path, body, build_request_headers(headers, :put, self.site.merge(path))))
  end
end