Class: Esendex::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xednese/client.rb

Overview

Client is a wrapper for Net::HTTP for calling through to the Esendex REST API.

Constant Summary collapse

ROOT =
URI('https://api.esendex.com/')
USER_AGENT =
"ruby/xednese-#{VERSION}"
CONTENT_TYPE =
"application/xml"

Class Method Summary collapse

Class Method Details

.delete(credentials, path) ⇒ Integer, String

Returns the status code and the response body.

Parameters:

  • credentials (Credentials)
  • path (String)

    Path to DELETE

Returns:

  • (Integer, String)

    Returns the status code and the response body.



30
31
32
33
34
35
36
37
# File 'lib/xednese/client.rb', line 30

def self.delete(credentials, path)
  uri = url(path)
  request = Net::HTTP::Delete.new(uri)

  code, body = execute(credentials, request)

  block_given? ? yield(code, body) : [code, body]
end

.get(credentials, path, args = {}) ⇒ Integer, String

Returns the status code and the response body.

Parameters:

  • credentials (Credentials)
  • path (String)

    Path to GET (after the api.esendex.com/ part)

  • args (Hash) (defaults to: {})

    An optional hash of query parameters to be appended to the url.

Returns:

  • (Integer, String)

    Returns the status code and the response body.



16
17
18
19
20
21
22
23
24
# File 'lib/xednese/client.rb', line 16

def self.get(credentials, path, args={})
  uri = url(path)
  uri.query = URI.encode_www_form(args)
  request = Net::HTTP::Get.new(uri)

  code, body = execute(credentials, request)

  block_given? ? yield(code, body) : [code, body]
end

.post(credentials, path, object) ⇒ Integer, String

Returns the status code and the response body.

Parameters:

  • credentials (Credentials)
  • path (String)

    Path to POST to

  • object (#serialise)

    The object that represents the content to be posted. This must be an object with a method #serialise that returns a string of xml.

Returns:

  • (Integer, String)

    Returns the status code and the response body.



46
47
48
49
50
51
52
53
54
# File 'lib/xednese/client.rb', line 46

def self.post(credentials, path, object)
  request = Net::HTTP::Post.new(url(path))
  request.body = object.serialise
  request.content_type = CONTENT_TYPE

  code, body = execute(credentials, request)

  block_given? ? yield(code, body) : [code, body]
end

.put(credentials, path, object) ⇒ Integer, String

Returns the status code and the response body.

Parameters:

  • credentials (Credentials)
  • path (String)

    Path to PUT to

  • object (#serialise)

    The object that represents the content to be posted. This must be an object with a method #serialise that returns a string of xml.

Returns:

  • (Integer, String)

    Returns the status code and the response body.



63
64
65
66
67
68
69
70
71
# File 'lib/xednese/client.rb', line 63

def self.put(credentials, path, object)
  request = Net::HTTP::Put.new(url(path))
  request.body = object.serialise
  request.content_type = CONTENT_TYPE

  code, body = execute(credentials, request)

  block_given? ? yield(code, body) : [code, body]
end