Class: ApiResource::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/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 = ApiResource::Formats::JsonFormat, headers) ⇒ Connection

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

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/api_resource/connection.rb', line 33

def initialize(site, format = ApiResource::Formats::JsonFormat, headers)
  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
  self.headers = headers
end

Instance Attribute Details

#auth_typeObject (readonly)

Returns the value of attribute auth_type.



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

def auth_type
  @auth_type
end

#formatObject

Returns the value of attribute format.



23
24
25
# File 'lib/api_resource/connection.rb', line 23

def format
  @format
end

#headersObject

Returns the value of attribute headers.



23
24
25
# File 'lib/api_resource/connection.rb', line 23

def headers
  @headers
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#proxyObject (readonly)

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

#ssl_optionsObject (readonly)

Returns the value of attribute ssl_options.



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

def ssl_options
  @ssl_options
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.requestsObject



26
27
28
# File 'lib/api_resource/connection.rb', line 26

def requests
  @@requests ||= []
end

Instance Method Details

#delete(path, headers = self.headers) ⇒ Object



69
70
71
72
# File 'lib/api_resource/connection.rb', line 69

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

#get(path, headers = self.headers) ⇒ String

make a put request

Returns:

  • (String)

    response.body raises an ApiResource::ConnectionError if we have a timeout, general exception, or if result.code is not within 200..399



59
60
61
62
63
64
65
66
67
# File 'lib/api_resource/connection.rb', line 59

def get(path, headers = self.headers)
  # our site and headers for this request
  site = self.site.merge(path)
  headers = build_request_headers(headers, :get, site)

  self.with_caching(path, headers) do
    format.decode(request(:get, path, headers))
  end
end

#head(path, headers = self.headers) ⇒ Object



74
75
76
# File 'lib/api_resource/connection.rb', line 74

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

#post(path, body = {}, headers = self.headers) ⇒ String

make a post request

Returns:

  • (String)

    response.body raises an ApiResource::ConnectionError if we have a timeout, general exception, or if result.code is not within 200..399



99
100
101
102
103
104
105
106
107
108
# File 'lib/api_resource/connection.rb', line 99

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

#put(path, body = {}, headers = self.headers) ⇒ String

make a put request

Returns:

  • (String)

    response.body raises an ApiResource::ConnectionError if we have a timeout, general exception, or if result.code is not within 200..399



83
84
85
86
87
88
89
90
91
92
# File 'lib/api_resource/connection.rb', line 83

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