Class: Listmonk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/listmonk/client.rb,
sig/listmonk.rbs

Constant Summary collapse

RESOURCE_NAMES =
%i[
  miscellaneous settings admin logs subscribers bounces lists imports campaigns
  media templates transactional maintenance public
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, username: nil, token: nil, auth: :basic, timeout: 30, open_timeout: 10, connection: nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
# File 'lib/listmonk/client.rb', line 18

def initialize(base_url:, username: nil, token: nil, auth: :basic, timeout: 30, open_timeout: 10,
               connection: nil)
  @base_url = normalize_base_url(base_url)
  @connection = connection || build_connection(username, token, auth, timeout, open_timeout)
end

Instance Attribute Details

#base_urlString (readonly)

Returns the value of attribute base_url.

Returns:

  • (String)


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

def base_url
  @base_url
end

Instance Method Details

#adminObject

Returns:

  • (Object)


51
# File 'sig/listmonk.rbs', line 51

def admin: () -> untyped

#bouncesObject

Returns:

  • (Object)


54
# File 'sig/listmonk.rbs', line 54

def bounces: () -> untyped

#campaignsObject

Returns:

  • (Object)


57
# File 'sig/listmonk.rbs', line 57

def campaigns: () -> untyped

#file_part(file, content_type: "application/octet-stream", filename: nil) ⇒ Object

Parameters:

  • file (Object)
  • content_type: (String) (defaults to: "application/octet-stream")
  • filename: (String, nil) (defaults to: nil)

Returns:

  • (Object)


58
59
60
61
62
63
# File 'lib/listmonk/client.rb', line 58

def file_part(file, content_type: "application/octet-stream", filename: nil)
  return file if file.is_a?(Faraday::Multipart::FilePart)

  filename ||= File.basename(file.respond_to?(:path) ? file.path : file.to_s)
  Faraday::Multipart::FilePart.new(file, content_type, filename)
end

#importsObject

Returns:

  • (Object)


56
# File 'sig/listmonk.rbs', line 56

def imports: () -> untyped

#listsObject

Returns:

  • (Object)


55
# File 'sig/listmonk.rbs', line 55

def lists: () -> untyped

#logsObject

Returns:

  • (Object)


52
# File 'sig/listmonk.rbs', line 52

def logs: () -> untyped

#maintenanceObject

Returns:

  • (Object)


61
# File 'sig/listmonk.rbs', line 61

def maintenance: () -> untyped

#mediaObject

Returns:

  • (Object)


58
# File 'sig/listmonk.rbs', line 58

def media: () -> untyped

#miscellaneousObject

Returns:

  • (Object)


49
# File 'sig/listmonk.rbs', line 49

def miscellaneous: () -> untyped

#publicObject

Returns:

  • (Object)


62
# File 'sig/listmonk.rbs', line 62

def public: () -> untyped

#request(method, path, params: nil, json: nil, body: nil, headers: {}) ⇒ Response

Parameters:

  • method (Symbol)
  • path (String)
  • params: (Hash[Symbol, untyped], nil) (defaults to: nil)
  • json: (Object) (defaults to: nil)
  • body: (Object) (defaults to: nil)
  • headers: (Hash[String, String]) (defaults to: {})

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/listmonk/client.rb', line 33

def request(method, path, params: nil, json: nil, body: nil, headers: {})
  raw = @connection.run_request(method, clean_path(path), nil, headers) do |request|
    request.params.update(compact(params)) if params
    if json.nil?
      request.body = body
    else
      request.headers["Content-Type"] = "application/json"
      request.body = JSON.generate(json)
    end
  end

  response = Response.new(
    status: raw.status,
    headers: raw.headers.to_h,
    body: parse_body(raw.body, raw.headers["content-type"])
  )
  raise_http_error(response) unless response.success?

  response
rescue Faraday::TimeoutError => e
  raise TimeoutError, e.message
rescue Faraday::ConnectionFailed => e
  raise ConnectionError, e.message
end

#settingsObject

Returns:

  • (Object)


50
# File 'sig/listmonk.rbs', line 50

def settings: () -> untyped

#subscribersObject

Returns:

  • (Object)


53
# File 'sig/listmonk.rbs', line 53

def subscribers: () -> untyped

#templatesObject

Returns:

  • (Object)


59
# File 'sig/listmonk.rbs', line 59

def templates: () -> untyped

#transactionalObject

Returns:

  • (Object)


60
# File 'sig/listmonk.rbs', line 60

def transactional: () -> untyped