trycourier 1.0.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/trycourier.rb CHANGED
@@ -1,12 +1,18 @@
1
+ require "trycourier/events"
2
+ require "trycourier/brands"
3
+ require "trycourier/lists"
4
+ require "trycourier/profiles"
5
+ require "trycourier/session"
6
+ require "trycourier/messages"
7
+ require "trycourier/automations"
8
+ require "trycourier/bulk"
1
9
  require "trycourier/version"
10
+ require "trycourier/exceptions"
2
11
  require "net/http"
3
12
  require "json"
4
13
  require "openssl"
5
14
 
6
15
  module Courier
7
- class ResponseError < StandardError; end
8
- class InputError < StandardError; end
9
-
10
16
  class SendResponse
11
17
  attr_reader :code
12
18
  attr_reader :message_id
@@ -17,50 +23,111 @@ module Courier
17
23
  end
18
24
  end
19
25
 
26
+ class SendMessageResponse
27
+ attr_reader :code
28
+ attr_reader :request_id
29
+
30
+ def initialize(code, request_id)
31
+ @code = code
32
+ @request_id = request_id
33
+ end
34
+ end
35
+
20
36
  class Client
21
- def initialize(auth_token = nil)
22
- @auth_token = auth_token || ENV['COURIER_AUTH_TOKEN']
23
- @uri = URI.parse('https://api.trycourier.app/send')
24
- if @auth_token == nil or @auth_token == ""
25
- raise InputError, "Client requires an auth_token be supplied."
37
+ def initialize(auth_token = nil, username: nil, password: nil, base_url: nil)
38
+ base = if base_url
39
+ base_url
40
+ elsif ENV["COURIER_BASE_URL"]
41
+ ENV["COURIER_BASE_URL"]
42
+ else
43
+ "https://api.courier.com"
44
+ end
45
+
46
+ @session = Courier::CourierAPISession.new(base)
47
+
48
+ if auth_token
49
+ @session.init_token_auth(auth_token)
50
+ elsif ENV["COURIER_AUTH_TOKEN"]
51
+ @session.init_token_auth(ENV["COURIER_AUTH_TOKEN"])
52
+ elsif username && password
53
+ @session.init_basic_auth(username, password)
54
+ elsif ENV["COURIER_AUTH_USERNAME"] && ENV["COURIER_AUTH_PASSWORD"]
55
+ @session.init_basic_auth(ENV["COURIER_AUTH_USERNAME"], ENV["COURIER_AUTH_PASSWORD"])
26
56
  end
57
+
58
+ @messages = Courier::Messages.new(@session)
59
+ @profiles = Courier::Profiles.new(@session)
60
+ @lists = Courier::Lists.new(@session)
61
+ @events = Courier::Events.new(@session)
62
+ @brands = Courier::Brands.new(@session)
63
+ @automations = Courier::Automations.new(@session)
64
+ @bulk = Courier::Bulk.new(@session)
27
65
  end
28
66
 
29
67
  def send(body)
30
- if not body.is_a?(Hash)
68
+ if !body.is_a?(Hash)
31
69
  raise InputError, "Client#send must be passed a Hash as first argument."
32
- elsif body["event"] == nil
70
+ elsif body["event"].nil? && body[:event].nil?
33
71
  raise InputError, "Must specify the 'event' key in Hash supplied to Client#send."
34
- elsif body["recipient"] == nil
72
+ elsif body["recipient"].nil? && body[:recipient].nil?
35
73
  raise InputError, "Must specify the 'recipient' key in Hash supplied to Client#send."
36
- elsif body["data"] != nil and not body["data"].is_a?(Hash)
74
+ elsif (!body["data"].nil? && !body["data"].is_a?(Hash)) || (!body[:data].nil? && !body[:data].is_a?(Hash))
37
75
  raise InputError, "The 'data' key in the Hash supplied to Client#send must also be a Hash."
38
- elsif body["profile"] != nil and not body["profile"].is_a?(Hash)
76
+ elsif (!body["profile"].nil? && !body["profile"].is_a?(Hash)) || (!body[:profile].nil? && !body[:profile].is_a?(Hash))
39
77
  raise InputError, "The 'profile' key in the Hash supplied to Client#send must also be a Hash."
40
78
  end
41
79
 
42
- http = Net::HTTP.new(@uri.host, @uri.port)
43
- http.use_ssl = true
44
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
45
-
46
- req = Net::HTTP::Post.new(@uri)
47
- req["authorization"] = "Bearer #{@auth_token}"
48
- req["content-type"] = "application/json"
49
- req["User-Agent"] = "courier-ruby/#{Courier::VERSION}"
50
- req.body = body.to_json
80
+ res = @session.send("/send", "POST", body: body)
51
81
 
52
- res = http.request(req)
53
82
  code = res.code.to_i
54
83
  obj = JSON.parse res.read_body
55
84
 
56
85
  if code == 200
57
86
  message_id = obj["messageId"]
58
- return SendResponse.new(code, message_id)
59
- elsif
60
- message = obj["Message"] == nil ? obj["message"] : obj["Message"]
87
+ SendResponse.new(code, message_id)
88
+ elsif (message = obj["Message"].nil? ? obj["message"] : obj["Message"])
61
89
  err = "#{code}: #{message}"
62
- raise ResponseError, err
90
+ raise CourierAPIError, err
63
91
  end
64
92
  end
93
+
94
+ def send_message(body)
95
+ if !body.is_a?(Hash)
96
+ raise InputError, "Client#send_message must be passed a Hash as first argument."
97
+ elsif (!body["message"].nil? && !body["message"].is_a?(Hash)) || (!body[:message].nil? && !body[:message].is_a?(Hash))
98
+ raise InputError, "The 'message' key in the Hash supplied to Client#send_message must also be a Hash."
99
+ end
100
+
101
+ res = @session.send("/send", "POST", body: body)
102
+
103
+ code = res.code.to_i
104
+ obj = JSON.parse res.read_body
105
+
106
+ if code == 202
107
+ request_id = obj["requestId"]
108
+ SendMessageResponse.new(code, request_id)
109
+ elsif (message = obj["Message"].nil? ? obj["message"] : obj["Message"])
110
+ err = "#{code}: #{message}"
111
+ raise CourierAPIError, err
112
+ end
113
+ end
114
+
115
+ # getters for all class variables
116
+
117
+ attr_reader :session
118
+
119
+ attr_reader :messages
120
+
121
+ attr_reader :profiles
122
+
123
+ attr_reader :events
124
+
125
+ attr_reader :lists
126
+
127
+ attr_reader :brands
128
+
129
+ attr_reader :automations
130
+
131
+ attr_reader :bulk
65
132
  end
66
133
  end
data/trycourier.gemspec CHANGED
@@ -1,15 +1,15 @@
1
- require_relative 'lib/trycourier/version'
1
+ require_relative "lib/trycourier/version"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = "trycourier"
5
- spec.version = Courier::VERSION
6
- spec.authors = ["Courier"]
7
- spec.email = ["support@trycourier.com"]
4
+ spec.name = "trycourier"
5
+ spec.version = Courier::VERSION
6
+ spec.authors = ["Courier"]
7
+ spec.email = ["support@courier.com"]
8
8
 
9
- spec.summary = %q{Wraps calls to the Courier REST API}
10
- spec.description = %q{Courier is the smartest way to design & deliver notifications. Design your notifications once, deliver them to any channel through one API.}
11
- spec.homepage = "https://github.com/trycourier/courier-ruby"
12
- spec.license = "MIT"
9
+ spec.summary = "Wraps calls to the Courier REST API"
10
+ spec.description = "Courier is the smartest way to design & deliver notifications. Design your notifications once, deliver them to any channel through one API."
11
+ spec.homepage = "https://github.com/trycourier/courier-ruby"
12
+ spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
15
15
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
@@ -20,10 +20,13 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
24
24
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
25
  end
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rspec", "~> 3.2"
31
+ spec.add_development_dependency "webmock", ">=1.24.2"
29
32
  end
metadata CHANGED
@@ -1,28 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trycourier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Courier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-06 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.24.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.24.2
13
41
  description: Courier is the smartest way to design & deliver notifications. Design
14
42
  your notifications once, deliver them to any channel through one API.
15
43
  email:
16
- - support@trycourier.com
44
+ - support@courier.com
17
45
  executables: []
18
46
  extensions: []
19
47
  extra_rdoc_files: []
20
48
  files:
21
49
  - ".github/ISSUE_TEMPLATE.md"
22
50
  - ".github/PULL_REQUEST_TEMPLATE.md"
51
+ - ".github/workflows/gem-push.yml"
23
52
  - ".gitignore"
24
53
  - ".rspec"
25
- - ".travis.yml"
54
+ - CHANGELOG.md
55
+ - CONTRIBUTING.md
26
56
  - Gemfile
27
57
  - Gemfile.lock
28
58
  - LICENSE
@@ -31,6 +61,15 @@ files:
31
61
  - bin/console
32
62
  - bin/setup
33
63
  - lib/trycourier.rb
64
+ - lib/trycourier/automations.rb
65
+ - lib/trycourier/brands.rb
66
+ - lib/trycourier/bulk.rb
67
+ - lib/trycourier/events.rb
68
+ - lib/trycourier/exceptions.rb
69
+ - lib/trycourier/lists.rb
70
+ - lib/trycourier/messages.rb
71
+ - lib/trycourier/profiles.rb
72
+ - lib/trycourier/session.rb
34
73
  - lib/trycourier/version.rb
35
74
  - trycourier.gemspec
36
75
  homepage: https://github.com/trycourier/courier-ruby
@@ -56,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
95
  - !ruby/object:Gem::Version
57
96
  version: '0'
58
97
  requirements: []
59
- rubygems_version: 3.0.3
98
+ rubygems_version: 3.1.6
60
99
  signing_key:
61
100
  specification_version: 4
62
101
  summary: Wraps calls to the Courier REST API
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- before_install: gem install bundler -v 2.1.4