agilis-ads 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AgilisAds
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AgilisAds
4
+ # Verifies webhook messages sent by Agilis.
5
+ module Webhook
6
+ DEFAULT_TOLERANCE = 300
7
+ SIGNATURE_HEADER = "Agilis-Signature"
8
+
9
+ module_function
10
+
11
+ # Checks the `Agilis-Signature` header of a received webhook and returns the
12
+ # parsed event as an AgilisObject.
13
+ #
14
+ # raw_body - the request body exactly as received (not re-encoded JSON)
15
+ # signature_header - the value of the Agilis-Signature header
16
+ # secret - your endpoint's signing secret (whsec_...)
17
+ # tolerance - how many seconds the timestamp may differ from now
18
+ #
19
+ # Raises AgilisAds::SignatureVerificationError when the header is missing or
20
+ # malformed, the timestamp is outside the tolerance, or no v1 signature matches.
21
+ def construct_event(raw_body, signature_header, secret, tolerance = DEFAULT_TOLERANCE, now: nil)
22
+ raise ArgumentError, "A webhook secret is required" if secret.nil? || secret.to_s.empty?
23
+
24
+ payload = raw_body.to_s.b
25
+ timestamp, signatures = parse_header(signature_header)
26
+ current = (now || Time.now).to_i
27
+ if (current - timestamp.to_i).abs > tolerance.to_i
28
+ raise SignatureVerificationError.new("The timestamp is outside the tolerance", reason: :timestamp)
29
+ end
30
+
31
+ expected = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, "#{timestamp}.".b + payload)
32
+ unless signatures.any? { |signature| OpenSSL.secure_compare(expected, signature) }
33
+ raise SignatureVerificationError.new("No signature matches the expected signature", reason: :signature)
34
+ end
35
+
36
+ parse_event(payload)
37
+ end
38
+
39
+ # Returns [timestamp string, [v1 signatures]] or raises SignatureVerificationError.
40
+ def parse_header(header)
41
+ timestamp = nil
42
+ signatures = []
43
+ header.to_s.split(",").each do |item|
44
+ key, value = item.strip.split("=", 2)
45
+ next if value.nil?
46
+
47
+ case key
48
+ when "t" then timestamp = value
49
+ when "v1" then signatures << value
50
+ end
51
+ end
52
+
53
+ raise header_error("The signature header has no timestamp") if timestamp.nil?
54
+ raise header_error("The signature header's timestamp is not a number") unless timestamp.match?(/\A\d+\z/)
55
+ raise header_error("The signature header has no v1 signature") if signatures.empty?
56
+
57
+ [timestamp, signatures]
58
+ end
59
+
60
+ def parse_event(payload)
61
+ values = JSON.parse(payload.dup.force_encoding(Encoding::UTF_8))
62
+ raise JSON::ParserError, "not an object" unless values.is_a?(Hash)
63
+
64
+ AgilisObject.new(values)
65
+ rescue JSON::ParserError
66
+ raise SignatureVerificationError.new("The webhook body is not a JSON object", reason: :payload)
67
+ end
68
+
69
+ def header_error(message)
70
+ SignatureVerificationError.new(message, reason: :header)
71
+ end
72
+ end
73
+ end
data/lib/agilis_ads.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "pathname"
7
+ require "securerandom"
8
+ require "uri"
9
+
10
+ require_relative "agilis_ads/version"
11
+ require_relative "agilis_ads/errors"
12
+ require_relative "agilis_ads/agilis_object"
13
+ require_relative "agilis_ads/response"
14
+ require_relative "agilis_ads/page"
15
+ require_relative "agilis_ads/upload"
16
+ require_relative "agilis_ads/multipart"
17
+ require_relative "agilis_ads/net_http_client"
18
+ require_relative "agilis_ads/requestor"
19
+ require_relative "agilis_ads/webhook"
20
+ require_relative "agilis_ads/resources/base"
21
+ require_relative "agilis_ads/resources/categories"
22
+ require_relative "agilis_ads/resources/interests"
23
+ require_relative "agilis_ads/resources/offers"
24
+ require_relative "agilis_ads/resources/event_categories"
25
+ require_relative "agilis_ads/resources/event_currencies"
26
+ require_relative "agilis_ads/resources/event_config"
27
+ require_relative "agilis_ads/resources/events"
28
+ require_relative "agilis_ads/resources/webhook_endpoints"
29
+ require_relative "agilis_ads/resources/webhook_events"
30
+ require_relative "agilis_ads/resources/webhooks"
31
+ require_relative "agilis_ads/client"
32
+
33
+ # Ruby client for the Agilis Advertising REST API.
34
+ module AgilisAds
35
+ end
@@ -0,0 +1,205 @@
1
+ module AgilisAds
2
+ VERSION: String
3
+
4
+ type params = Hash[String | Symbol, untyped]
5
+
6
+ class AgilisError < StandardError
7
+ attr_reader http_status: Integer?
8
+ attr_reader return_code: String?
9
+ attr_reader body: untyped
10
+ attr_reader headers: Hash[String, String]
11
+ def initialize: (?String? message, ?http_status: Integer?, ?return_code: String?, ?body: untyped, ?headers: Hash[String, String]?) -> void
12
+ end
13
+
14
+ class AuthenticationError < AgilisError
15
+ end
16
+
17
+ class PermissionError < AgilisError
18
+ end
19
+
20
+ class NotFoundError < AgilisError
21
+ end
22
+
23
+ class ConflictError < AgilisError
24
+ end
25
+
26
+ class InvalidRequestError < AgilisError
27
+ end
28
+
29
+ class RateLimitError < AgilisError
30
+ attr_reader retry_after: Float?
31
+ def initialize: (?String? message, ?retry_after: Float?, **untyped) -> void
32
+ end
33
+
34
+ class ApiError < AgilisError
35
+ end
36
+
37
+ class ConnectionError < AgilisError
38
+ end
39
+
40
+ class SignatureVerificationError < AgilisError
41
+ attr_reader reason: (:header | :timestamp | :signature | :payload)?
42
+ def initialize: (?String? message, ?reason: Symbol?) -> void
43
+ end
44
+
45
+ class Response
46
+ attr_reader status: Integer
47
+ attr_reader headers: Hash[String, String]
48
+ attr_reader body: String
49
+ def initialize: (Integer status, Hash[untyped, untyped]? headers, String? body) -> void
50
+ def header: (String | Symbol name) -> String?
51
+ def rate_limit_limit: () -> Integer?
52
+ def rate_limit_remaining: () -> Integer?
53
+ end
54
+
55
+ class AgilisObject
56
+ attr_reader last_response: Response?
57
+ def initialize: (?Hash[untyped, untyped]? values, ?Response? last_response) -> void
58
+ def []: (String | Symbol key) -> untyped
59
+ def key?: (String | Symbol key) -> bool
60
+ def keys: () -> Array[Symbol]
61
+ def dig: (String | Symbol key, *untyped rest) -> untyped
62
+ def each: () { (Symbol, untyped) -> void } -> self
63
+ | () -> Enumerator[[Symbol, untyped], self]
64
+ def to_h: () -> Hash[Symbol, untyped]
65
+ def to_json: (*untyped) -> String
66
+ def method_missing: (Symbol name, *untyped args) -> untyped
67
+ def self.convert: (untyped value) -> untyped
68
+ def self.deep_symbolize: (untyped value) -> untyped
69
+ end
70
+
71
+ class Page < AgilisObject
72
+ def initialize: (Hash[untyped, untyped]? values, ?Response? last_response, ?fetch_next: (^(String) -> Page)?) -> void
73
+ def data: () -> Array[AgilisObject]
74
+ def pagination: () -> AgilisObject?
75
+ def more?: () -> bool
76
+ def next_cursor: () -> String?
77
+ def next_page: () -> Page?
78
+ def to_a: () -> Array[AgilisObject]
79
+ def auto_paging_each: () { (AgilisObject) -> void } -> self
80
+ | () -> Enumerator[AgilisObject, self]
81
+ end
82
+
83
+ class Upload
84
+ CONTENT_TYPES: Hash[String, String]
85
+ DEFAULT_CONTENT_TYPE: String
86
+ attr_reader filename: String
87
+ attr_reader content_type: String
88
+ def self.from_path: (String | Pathname path, ?filename: String?, ?content_type: String?) -> Upload
89
+ def self.from_bytes: (String bytes, filename: String, ?content_type: String?) -> Upload
90
+ def self.from_io: (untyped io, ?filename: String?, ?content_type: String?) -> Upload
91
+ def self.coerce: (untyped value) -> Upload
92
+ def self.upload?: (untyped value) -> bool
93
+ def self.content_type_for: (String filename) -> String
94
+ def initialize: (^() -> String reader, filename: String, ?content_type: String?) -> void
95
+ def read: () -> String
96
+ end
97
+
98
+ module Multipart
99
+ def self.encode: (Hash[String, untyped] params, ?boundary: String) -> [String, String]
100
+ end
101
+
102
+ class NetHttpClient
103
+ def call: (method: String, url: String, headers: Hash[String, String], body: String?, timeout: Numeric) -> [Integer, Hash[String, String], String]
104
+ end
105
+
106
+ module Webhook
107
+ DEFAULT_TOLERANCE: Integer
108
+ SIGNATURE_HEADER: String
109
+ def self.construct_event: (String raw_body, String? signature_header, String secret, ?Integer tolerance, ?now: (Integer | Time)?) -> AgilisObject
110
+ end
111
+
112
+ class Client
113
+ DEFAULT_BASE_URL: String
114
+ DEFAULT_TIMEOUT: Integer
115
+ DEFAULT_MAX_RETRIES: Integer
116
+
117
+ attr_reader base_url: String
118
+ attr_reader timeout: Numeric
119
+ attr_reader max_retries: Integer
120
+ attr_reader mode: ("test" | "live")?
121
+ attr_reader categories: Resources::Categories
122
+ attr_reader interests: Resources::Interests
123
+ attr_reader offers: Resources::Offers
124
+ attr_reader event_categories: Resources::EventCategories
125
+ attr_reader event_currencies: Resources::EventCurrencies
126
+ attr_reader event_config: Resources::EventConfig
127
+ attr_reader events: Resources::Events
128
+ attr_reader webhook_endpoints: Resources::WebhookEndpoints
129
+ attr_reader webhook_events: Resources::WebhookEvents
130
+ attr_reader webhooks: Resources::Webhooks
131
+
132
+ def initialize: (?api_key: String?, ?base_url: String, ?timeout: Numeric, ?max_retries: Integer, ?http_client: untyped) -> void
133
+ def self.mode_for: (String api_key) -> ("test" | "live")?
134
+ def test_mode?: () -> bool
135
+ def live_mode?: () -> bool
136
+ end
137
+
138
+ module Resources
139
+ class Base
140
+ def initialize: (Client client) -> void
141
+ end
142
+
143
+ class Categories < Base
144
+ def list: () -> AgilisObject
145
+ end
146
+
147
+ class Interests < Base
148
+ def list: () -> AgilisObject
149
+ end
150
+
151
+ class EventCategories < Base
152
+ def list: () -> AgilisObject
153
+ end
154
+
155
+ class EventCurrencies < Base
156
+ def list: () -> AgilisObject
157
+ end
158
+
159
+ class EventConfig < Base
160
+ def get: () -> AgilisObject
161
+ end
162
+
163
+ class Offers < Base
164
+ def estimate_price: (?params? params, **untyped) -> AgilisObject
165
+ def create: (?params? params, ?dry_run: bool, ?idempotency_key: String?, **untyped) -> AgilisObject
166
+ def list: (?params? params, **untyped) -> Page
167
+ def pause: (Integer | String offer_id) -> AgilisObject
168
+ def resume: (Integer | String offer_id) -> AgilisObject
169
+ def analytics: (Integer | String offer_id, ?params? params, **untyped) -> AgilisObject
170
+ def delete: (Integer | String offer_id, ?dry_run: bool) -> AgilisObject
171
+ end
172
+
173
+ class Events < Base
174
+ def preview: (?params? params, **untyped) -> AgilisObject
175
+ def publish: (?params? params, ?dry_run: bool, ?idempotency_key: String?, **untyped) -> AgilisObject
176
+ def list: (?params? params, **untyped) -> Page
177
+ def get: (Integer | String event_id) -> AgilisObject
178
+ def edit: (Integer | String event_id, ?params? params, **untyped) -> AgilisObject
179
+ def delete: (Integer | String event_id, ?dry_run: bool) -> AgilisObject
180
+ def reschedule: (Integer | String event_id, ?params? params, **untyped) -> AgilisObject
181
+ def cancel: (Integer | String event_id, ?dry_run: bool) -> AgilisObject
182
+ def analytics: (Integer | String event_id, ?params? params, **untyped) -> AgilisObject
183
+ end
184
+
185
+ class WebhookEndpoints < Base
186
+ def list: () -> AgilisObject
187
+ def create: (?params? params, **untyped) -> AgilisObject
188
+ def get: (String endpoint_id) -> AgilisObject
189
+ def update: (String endpoint_id, ?params? params, **untyped) -> AgilisObject
190
+ def delete: (String endpoint_id) -> AgilisObject
191
+ def roll_secret: (String endpoint_id, ?params? params, **untyped) -> AgilisObject
192
+ def send_test: (String endpoint_id, ?params? params, **untyped) -> AgilisObject
193
+ end
194
+
195
+ class WebhookEvents < Base
196
+ def list: (?params? params, **untyped) -> Page
197
+ def get: (String webhook_event_id) -> AgilisObject
198
+ def resend: (String webhook_event_id, ?params? params, **untyped) -> AgilisObject
199
+ end
200
+
201
+ class Webhooks < Base
202
+ def construct_event: (String raw_body, String? signature_header, String secret, ?Integer tolerance, ?now: (Integer | Time)?) -> AgilisObject
203
+ end
204
+ end
205
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agilis-ads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Agilis Dating Ltd
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Create and manage Agilis Offers, Events and webhook endpoints, and verify
13
+ webhook signatures.
14
+ email:
15
+ - help@agilis.dating
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - SECURITY.md
24
+ - lib/agilis_ads.rb
25
+ - lib/agilis_ads/agilis_object.rb
26
+ - lib/agilis_ads/client.rb
27
+ - lib/agilis_ads/errors.rb
28
+ - lib/agilis_ads/multipart.rb
29
+ - lib/agilis_ads/net_http_client.rb
30
+ - lib/agilis_ads/page.rb
31
+ - lib/agilis_ads/requestor.rb
32
+ - lib/agilis_ads/resources/base.rb
33
+ - lib/agilis_ads/resources/categories.rb
34
+ - lib/agilis_ads/resources/event_categories.rb
35
+ - lib/agilis_ads/resources/event_config.rb
36
+ - lib/agilis_ads/resources/event_currencies.rb
37
+ - lib/agilis_ads/resources/events.rb
38
+ - lib/agilis_ads/resources/interests.rb
39
+ - lib/agilis_ads/resources/offers.rb
40
+ - lib/agilis_ads/resources/webhook_endpoints.rb
41
+ - lib/agilis_ads/resources/webhook_events.rb
42
+ - lib/agilis_ads/resources/webhooks.rb
43
+ - lib/agilis_ads/response.rb
44
+ - lib/agilis_ads/upload.rb
45
+ - lib/agilis_ads/version.rb
46
+ - lib/agilis_ads/webhook.rb
47
+ - sig/agilis_ads.rbs
48
+ homepage: https://github.com/agilisdating/agilis-ads-ruby
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/agilisdating/agilis-ads-ruby
53
+ source_code_uri: https://github.com/agilisdating/agilis-ads-ruby
54
+ changelog_uri: https://github.com/agilisdating/agilis-ads-ruby/blob/main/CHANGELOG.md
55
+ documentation_uri: https://www.agilis.dating/api-doc
56
+ bug_tracker_uri: https://github.com/agilisdating/agilis-ads-ruby/issues
57
+ rubygems_mfa_required: 'true'
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '3.2'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.6.9
73
+ specification_version: 4
74
+ summary: Ruby client for the Agilis Advertising REST API
75
+ test_files: []