Class: Recurly::API

Inherits:
Object
  • Object
show all
Extended by:
Net::HTTPAdapter
Defined in:
lib/recurly/api.rb,
lib/recurly/api/errors.rb,
lib/recurly/api/net_http_adapter.rb

Overview

The API class handles all requests to the Recurly API. While most of its functionality is leveraged by the Resource class, it can be used directly, as well.

Requests are made with methods named after the four main HTTP verbs recognized by the Recurly API.

Examples:

Recurly::API.get 'accounts'             # => #<Net::HTTPOK ...>
Recurly::API.post 'accounts', xml_body  # => #<Net::HTTPCreated ...>
Recurly::API.put 'accounts/1', xml_body # => #<Net::HTTPOK ...>
Recurly::API.delete 'accounts/1'        # => #<Net::HTTPNoContent ...>

Defined Under Namespace

Modules: Net Classes: BadRequest, ClientError, Forbidden, GatewayError, InternalServerError, MethodNotAllowed, NotAcceptable, NotFound, NotModified, PaymentRequired, PreconditionFailed, Redirection, ResponseError, ServerError, ServiceUnavailable, Unauthorized, UnprocessableEntity, UnsupportedMediaType

Constant Summary

RECURLY_API_VERSION =
'2.4'
FORMATS =
Helper.hash_with_indifferent_read_access(
  'pdf' => 'application/pdf',
  'xml' => 'application/xml'
)
ERRORS =

Error mapping by status code.

Hash.new { |hash, code|
  unless hash.key? code
    case code
      when 400...500 then ClientError
      when 500...600 then ServerError
      else                ResponseError
    end
  end
}.update(
  304 => NotModified,
  400 => BadRequest,
  401 => Unauthorized,
  402 => PaymentRequired,
  403 => Forbidden,
  404 => NotFound,
  406 => NotAcceptable,
  412 => PreconditionFailed,
  415 => UnsupportedMediaType,
  422 => UnprocessableEntity,
  500 => InternalServerError,
  502 => GatewayError,
  503 => ServiceUnavailable
).freeze
@@base_uri =
"https://api.recurly.com/v2/"

Instance Attribute Summary

Attributes included from Net::HTTPAdapter

#net_http

Class Method Summary collapse

Class Method Details

.accept_languageString?

Returns Accept-Language header value

Returns:

  • (String, nil)

    Accept-Language header value



34
35
36
# File 'lib/recurly/api.rb', line 34

def accept_language
  headers['Accept-Language']
end

.accept_language=(language) ⇒ Object

Parameters:

  • language (String)

    Accept-Language header value



39
40
41
# File 'lib/recurly/api.rb', line 39

def accept_language=(language)
  headers['Accept-Language'] = language
end

.base_uriURI::Generic

Returns:

  • (URI::Generic)


74
75
76
# File 'lib/recurly/api.rb', line 74

def base_uri
  URI.parse @@base_uri.sub('api', Recurly.subdomain)
end

.delete(uri, body = nil, options = {}) ⇒ Net::HTTPNoContent, Net::HTTPResponse

Returns:

  • (Net::HTTPNoContent, Net::HTTPResponse)

Raises:



69
70
71
# File 'lib/recurly/api.rb', line 69

def delete uri, body = nil, options = {}
  request :delete, uri, options
end

.get(uri, params = {}, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



51
52
53
# File 'lib/recurly/api.rb', line 51

def get uri, params = {}, options = {}
  request :get, uri, { :params => params || {} }.merge(options)
end

.head(uri, params = {}, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



45
46
47
# File 'lib/recurly/api.rb', line 45

def head uri, params = {}, options = {}
  request :head, uri, { :params => params || {} }.merge(options)
end

.headersHash{String => String}

Additional HTTP headers sent with each API call

Returns:

  • (Hash{String => String})


29
30
31
# File 'lib/recurly/api.rb', line 29

def headers
  @headers ||= { 'Accept' => accept, 'User-Agent' => user_agent, 'X-Api-Version' => RECURLY_API_VERSION }
end

.post(uri, body = nil, options = {}) ⇒ Net::HTTPCreated, Net::HTTPResponse

Returns:

  • (Net::HTTPCreated, Net::HTTPResponse)

Raises:



57
58
59
# File 'lib/recurly/api.rb', line 57

def post uri, body = nil, options = {}
  request :post, uri, { :body => body.to_s }.merge(options)
end

.put(uri, body = nil, options = {}) ⇒ Net::HTTPOK, Net::HTTPResponse

Returns:

  • (Net::HTTPOK, Net::HTTPResponse)

Raises:



63
64
65
# File 'lib/recurly/api.rb', line 63

def put uri, body = nil, options = {}
  request :put, uri, { :body => body.to_s }.merge(options)
end

.user_agentString

Returns:

  • (String)


79
80
81
# File 'lib/recurly/api.rb', line 79

def user_agent
  "Recurly/#{Version}; #{RUBY_DESCRIPTION}"
end