Class: Rapidmail::Client

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

Constant Summary collapse

API_BASE =
"https://apiv3.emailsys.net".freeze
TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(username: Rapidmail.configuration.username, password: Rapidmail.configuration.password) ⇒ Client

Initializes a new client instance.

Parameters:

  • username (String) (defaults to: Rapidmail.configuration.username)

    The API username.

  • password (String) (defaults to: Rapidmail.configuration.password)

    The API password.



36
37
38
# File 'lib/rapidmail/client.rb', line 36

def initialize(username: Rapidmail.configuration.username, password: Rapidmail.configuration.password)
  @connection = build_connection(username, password)
end

Instance Method Details

#delete(path) ⇒ Faraday::Response

Sends a DELETE request.

Parameters:

  • path (String)

    The API endpoint path.

Returns:

  • (Faraday::Response)

    The response from the API.



104
105
106
107
108
109
# File 'lib/rapidmail/client.rb', line 104

def delete(path)
  @connection.delete(path)
rescue Faraday::ConnectionFailed
  Rapidmail.online = false
  raise
end

#get(path, params = {}) ⇒ Faraday::Response

Sends a GET request.

Parameters:

  • path (String)

    The API endpoint path.

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

    The query parameters.

Returns:

  • (Faraday::Response)

    The response from the API.



45
46
47
48
49
50
# File 'lib/rapidmail/client.rb', line 45

def get(path, params = {})
  @connection.get(path, params)
rescue Faraday::ConnectionFailed
  Rapidmail.online = false
  raise
end

#jobsObject



16
17
18
# File 'lib/rapidmail/client.rb', line 16

def jobs
  @jobs ||= Resources::Job.new(self)
end

#mailingsObject



20
21
22
# File 'lib/rapidmail/client.rb', line 20

def mailings
  @mailings ||= Resources::Mailing.new(self)
end

#patch(path, params = {}) ⇒ Faraday::Response

Sends a PATCH request.

Parameters:

  • path (String)

    The API endpoint path.

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

    The request body parameters.

Returns:

  • (Faraday::Response)

    The response from the API.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rapidmail/client.rb', line 73

def patch(path, params = {})
  @connection.patch do |req|
    req.url path
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  end
rescue Faraday::ConnectionFailed
  Rapidmail.online = false
  raise
end

#pingObject



9
10
11
12
13
14
# File 'lib/rapidmail/client.rb', line 9

def ping
  get("mailings")
  true
rescue Faraday::ConnectionFailed
  false
end

#post(path, params = {}) ⇒ Faraday::Response

Sends a POST request.

Parameters:

  • path (String)

    The API endpoint path.

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

    The request body parameters.

Returns:

  • (Faraday::Response)

    The response from the API.



57
58
59
60
61
62
63
64
65
66
# File 'lib/rapidmail/client.rb', line 57

def post(path, params = {})
  @connection.post do |req|
    req.url path
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  end
rescue Faraday::ConnectionFailed
  Rapidmail.online = false
  raise
end

#put(path, params = {}) ⇒ Faraday::Response

Sends a PUT request.

Parameters:

  • path (String)

    The API endpoint path.

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

    The request body parameters.

Returns:

  • (Faraday::Response)

    The response from the API.



89
90
91
92
93
94
95
96
97
98
# File 'lib/rapidmail/client.rb', line 89

def put(path, params = {})
  @connection.put do |req|
    req.url path
    req.headers["Content-Type"] = "application/json"
    req.body = params.to_json
  end
rescue Faraday::ConnectionFailed
  Rapidmail.online = false
  raise
end

#recipient_listsObject



24
25
26
# File 'lib/rapidmail/client.rb', line 24

def recipient_lists
  @recipient_lists ||= Resources::RecipientList.new(self)
end

#recipientsObject



28
29
30
# File 'lib/rapidmail/client.rb', line 28

def recipients
  @recipients ||= Resources::Recipient.new(self)
end