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
73
74
75
76
77
# 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 get 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
    request(:get, path, {}, headers)
  end
end

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



79
80
81
82
83
84
85
86
87
# File 'lib/api_resource/connection.rb', line 79

def head(path, headers = self.headers)
  request(
    :head,
    path,
    {},
    build_request_headers(headers, :head, self.site.merge(path))
  )
  return true
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



120
121
122
123
124
125
126
127
# File 'lib/api_resource/connection.rb', line 120

def post(path, body = {}, headers = self.headers)
  request(
    :post,
    path,
    format.encode(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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/api_resource/connection.rb', line 94

def put(path, body = {}, headers = self.headers)
  response = request(
    :put,
    path,
    format.encode(body),
    build_request_headers(headers, :put, self.site.merge(path))
  )
  # handle blank response and return true
  if response.blank?
    return {}
  # we used to decode JSON in the response, but we don't want to
  # do that anymore - we will issue a warning but keep the behavior
  else
    ApiResource.logger.warn(
      "[DEPRECATION] Returning a response body from a PUT " +
      "is deprecated. \n#{response.pretty_inspect} was returned."
    )
    return response
  end
end