Class: Courier::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_token = nil, username: nil, password: nil, base_url: nil) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/trycourier.rb', line 42

def initialize(auth_token = nil, username: nil, password: nil, base_url: nil)
  base = if base_url
    base_url
  elsif ENV["COURIER_BASE_URL"]
    ENV["COURIER_BASE_URL"]
  else
    "https://api.courier.com"
  end

  @session = Courier::CourierAPISession.new(base)

  if auth_token
    @session.init_token_auth(auth_token)
  elsif ENV["COURIER_AUTH_TOKEN"]
    @session.init_token_auth(ENV["COURIER_AUTH_TOKEN"])
  elsif username && password
    @session.init_basic_auth(username, password)
  elsif ENV["COURIER_AUTH_USERNAME"] && ENV["COURIER_AUTH_PASSWORD"]
    @session.init_basic_auth(ENV["COURIER_AUTH_USERNAME"], ENV["COURIER_AUTH_PASSWORD"])
  end

  @messages = Courier::Messages.new(@session)
  @profiles = Courier::Profiles.new(@session)
  @lists = Courier::Lists.new(@session)
  @events = Courier::Events.new(@session)
  @brands = Courier::Brands.new(@session)
  @automations = Courier::Automations.new(@session)
  @bulk = Courier::Bulk.new(@session)
  @audiences = Courier::Audiences.new(@session)
  @audit_events = Courier::AuditEvents.new(@session)
  @tenants = Courier::Tenants.new(@session)
  @auth_tokens = Courier::AuthTokens.new(@session)
end

Instance Attribute Details

#audiencesObject (readonly)

Returns the value of attribute audiences.



142
143
144
# File 'lib/trycourier.rb', line 142

def audiences
  @audiences
end

#audit_eventsObject (readonly)

Returns the value of attribute audit_events.



144
145
146
# File 'lib/trycourier.rb', line 144

def audit_events
  @audit_events
end

#auth_tokensObject (readonly)

Returns the value of attribute auth_tokens.



148
149
150
# File 'lib/trycourier.rb', line 148

def auth_tokens
  @auth_tokens
end

#automationsObject (readonly)

Returns the value of attribute automations.



138
139
140
# File 'lib/trycourier.rb', line 138

def automations
  @automations
end

#brandsObject (readonly)

Returns the value of attribute brands.



136
137
138
# File 'lib/trycourier.rb', line 136

def brands
  @brands
end

#bulkObject (readonly)

Returns the value of attribute bulk.



140
141
142
# File 'lib/trycourier.rb', line 140

def bulk
  @bulk
end

#eventsObject (readonly)

Returns the value of attribute events.



132
133
134
# File 'lib/trycourier.rb', line 132

def events
  @events
end

#listsObject (readonly)

Returns the value of attribute lists.



134
135
136
# File 'lib/trycourier.rb', line 134

def lists
  @lists
end

#messagesObject (readonly)

Returns the value of attribute messages.



128
129
130
# File 'lib/trycourier.rb', line 128

def messages
  @messages
end

#profilesObject (readonly)

Returns the value of attribute profiles.



130
131
132
# File 'lib/trycourier.rb', line 130

def profiles
  @profiles
end

#sessionObject (readonly)

getters for all class variables



126
127
128
# File 'lib/trycourier.rb', line 126

def session
  @session
end

#tenantsObject (readonly)

Returns the value of attribute tenants.



146
147
148
# File 'lib/trycourier.rb', line 146

def tenants
  @tenants
end

Instance Method Details

#send(body) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/trycourier.rb', line 76

def send(body)
  if !body.is_a?(Hash)
    raise InputError, "Client#send must be passed a Hash as first argument."
  elsif body["event"].nil? && body[:event].nil?
    raise InputError, "Must specify the 'event' key in Hash supplied to Client#send."
  elsif body["recipient"].nil? && body[:recipient].nil?
    raise InputError, "Must specify the 'recipient' key in Hash supplied to Client#send."
  elsif (!body["data"].nil? && !body["data"].is_a?(Hash)) || (!body[:data].nil? && !body[:data].is_a?(Hash))
    raise InputError, "The 'data' key in the Hash supplied to Client#send must also be a Hash."
  elsif (!body["profile"].nil? && !body["profile"].is_a?(Hash)) || (!body[:profile].nil? && !body[:profile].is_a?(Hash))
    raise InputError, "The 'profile' key in the Hash supplied to Client#send must also be a Hash."
  end

  res = @session.send("/send", "POST", body: body)

  code = res.code.to_i
  obj = JSON.parse res.read_body

  if code == 200
    message_id = obj["messageId"]
    SendResponse.new(code, message_id)
  elsif (message = obj["Message"].nil? ? obj["message"] : obj["Message"])
    err = "#{code}: #{message}"
    raise CourierAPIError, err
  end
end

#send_message(body) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/trycourier.rb', line 103

def send_message(body)
  if !body.is_a?(Hash)
    raise InputError, "Client#send_message must be passed a Hash as first argument."
  elsif (!body["message"].nil? && !body["message"].is_a?(Hash)) || (!body[:message].nil? && !body[:message].is_a?(Hash))
    raise InputError, "The 'message' key in the Hash supplied to Client#send_message must also be a Hash."
  end

  res = @session.send("/send", "POST", body: body)

  code = res.code.to_i
  obj = JSON.parse res.read_body

  if code == 202
    request_id = obj["requestId"]
    SendMessageResponse.new(code, request_id)
  elsif (message = obj["Message"].nil? ? obj["message"] : obj["Message"])
    err = "#{code}: #{message}"
    raise CourierAPIError, err
  end
end