moatlight 1.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f017b1b6fd54a94aa56c7fa61c9b14761d9717f524a29032f083aeb8cc040ad7
4
+ data.tar.gz: 1d10e2ae0f4e1e7ca024b7d7a58baee2d54afeeebd7f799d360337fc877ac4b4
5
+ SHA512:
6
+ metadata.gz: 8472aed4ca35a36f7b6b175eb201da45ad9b5fb3149e6175daf003de963008bb018b17f4ffb06cd757bd27322d4fa27ccf179a29d0b3ccf1c6e2e4092b802807
7
+ data.tar.gz: bc19244dcc0f8188cd91de38108ca5feb265f85f313239aef4a4cb9490d75ef8fb067dba4e9da83c9e47c54d7a8bd0014dcc5737d5803d6da49a31cba3805e27
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Moatlight
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # moatlight (Ruby)
2
+
3
+ Official Ruby client for the [Moatlight API](https://www.moatlight.com/docs). Standard library only.
4
+
5
+ ```ruby
6
+ require "moatlight"
7
+
8
+ client = Moatlight::Client.new # public endpoints need no token
9
+ puts client.get_product["name"]
10
+
11
+ client = Moatlight::Client.new(token: ENV["MOATLIGHT_TOKEN"]) # OAuth access token
12
+ report = client.create_report(website: "yourcompany.com")
13
+ client.start_report(report["id"])
14
+ ```
15
+
16
+ Try it with no login against the sandbox: `Moatlight::Client.new(base_url: "https://www.moatlight.com/api/v1/sandbox")` is not needed, the sandbox shares the paths under `/api/v1/sandbox`; see https://www.moatlight.com/docs#sandbox.
17
+
18
+ Writes send an `Idempotency-Key`, so a retry never starts a second report. `429` and `5xx` are retried and `Retry-After` is honoured. Errors raise `Moatlight::Error` with `status`, `code`, `message`, `hint`.
19
+
20
+ Homepage: https://www.moatlight.com. Source: https://github.com/Moatlight/moatlight-agent-kit. License: MIT.
@@ -0,0 +1,3 @@
1
+ module Moatlight
2
+ VERSION = "1.1.0"
3
+ end
data/lib/moatlight.rb ADDED
@@ -0,0 +1,178 @@
1
+ # Moatlight API client. Standard library only.
2
+ #
3
+ # Writes carry an Idempotency-Key that stays the same across retries, and network errors, 429 and 5xx are
4
+ # retried (Retry-After honoured), so a retry can never start a second report.
5
+ require "json"
6
+ require "net/http"
7
+ require "securerandom"
8
+ require "uri"
9
+ require_relative "moatlight/version"
10
+
11
+ module Moatlight
12
+ DEFAULT_BASE_URL = "https://www.moatlight.com".freeze
13
+
14
+ # An API error: HTTP status plus the code, message and hint from the JSON error body.
15
+ class Error < StandardError
16
+ attr_reader :status, :code, :hint, :retry_after, :body
17
+
18
+ def initialize(status, code, message, hint = nil, retry_after = nil, body = nil)
19
+ super("#{code}: #{message}")
20
+ @status, @code, @hint, @retry_after, @body = status, code, hint, retry_after, body
21
+ end
22
+ end
23
+
24
+ class Client
25
+ # RateLimit headers of the last answer: { limit:, remaining:, reset_seconds: }
26
+ attr_reader :last_rate_limit
27
+
28
+ def initialize(token: nil, base_url: DEFAULT_BASE_URL, max_retries: 3, timeout: 60)
29
+ @token = token
30
+ @base_url = base_url.sub(%r{/+\z}, "")
31
+ @max_retries = max_retries
32
+ @timeout = timeout
33
+ @last_rate_limit = {}
34
+ end
35
+
36
+ # ---- public ----
37
+ def get_product(fields: nil)
38
+ request("/api/v1/product", query: { fields: fields })
39
+ end
40
+ def get_pricing(fields: nil)
41
+ request("/api/v1/pricing", query: { fields: fields })
42
+ end
43
+ def get_sample_report
44
+ request("/api/v1/sample-report")
45
+ end
46
+ def list_faq(limit: nil, cursor: nil)
47
+ request("/api/v1/faq", query: { limit: limit, cursor: cursor })
48
+ end
49
+
50
+ # ---- account (OAuth token) ----
51
+ def get_account(fields: nil)
52
+ request("/api/v1/me", query: { fields: fields })
53
+ end
54
+ def list_reports(limit: nil, cursor: nil)
55
+ request("/api/v1/reports", query: { limit: limit, cursor: cursor })
56
+ end
57
+
58
+ # Every report, newest first, following next_cursor for you.
59
+ def each_report(limit: 50)
60
+ return enum_for(:each_report, limit: limit) unless block_given?
61
+
62
+ cursor = nil
63
+ loop do
64
+ page = list_reports(limit: limit, cursor: cursor)
65
+ page["reports"].each { |r| yield r }
66
+ cursor = page["next_cursor"]
67
+ break unless cursor
68
+ end
69
+ end
70
+
71
+ def create_report(website:, country: nil, language: nil, idempotency_key: nil)
72
+ body = { website: website, country: country, language: language }.compact
73
+ request("/api/v1/reports", method: :post, body: body, idempotency_key: idempotency_key)
74
+ end
75
+
76
+ def get_report(report_id, fields: nil)
77
+
78
+ request("/api/v1/reports/#{esc(report_id)}", query: { fields: fields })
79
+
80
+ end
81
+ def batch_get_reports(report_ids)
82
+ request("/api/v1/reports/batch", method: :post, body: { report_ids: report_ids })
83
+ end
84
+
85
+ def start_report(report_id, question_ids: nil, idempotency_key: nil)
86
+ body = question_ids ? { question_ids: question_ids } : {}
87
+ request("/api/v1/reports/#{esc(report_id)}/start", method: :post, body: body, idempotency_key: idempotency_key)
88
+ end
89
+
90
+ def get_report_actions(report_id)
91
+
92
+ request("/api/v1/reports/#{esc(report_id)}/actions")
93
+
94
+ end
95
+ def get_agent_readiness(report_id)
96
+ request("/api/v1/reports/#{esc(report_id)}/agent-readiness")
97
+ end
98
+
99
+ def scan_agent_readiness(report_id, force: false, idempotency_key: nil)
100
+ request("/api/v1/reports/#{esc(report_id)}/agent-readiness/scan", method: :post, body: { force: force },
101
+ idempotency_key: idempotency_key)
102
+ end
103
+
104
+ # ---- transport ----
105
+ def request(path, method: :get, query: nil, body: nil, idempotency_key: nil)
106
+ uri = URI(@base_url + path)
107
+ params = (query || {}).compact
108
+ uri.query = URI.encode_www_form(params) unless params.empty?
109
+ key = method == :post ? (idempotency_key || SecureRandom.uuid) : nil
110
+ attempt = 0
111
+ begin
112
+ res = perform(uri, method, body, key)
113
+ record_rate_limit(res)
114
+ code = res.code.to_i
115
+ if code.between?(200, 299)
116
+ return res.body.to_s.empty? ? nil : JSON.parse(res.body)
117
+ end
118
+
119
+ retry_after = res["Retry-After"].to_i
120
+ retry_after = nil if retry_after.zero?
121
+ if (code == 429 || code >= 500) && attempt < @max_retries
122
+ attempt += 1
123
+ sleep(retry_after ? [retry_after, 60].min : backoff(attempt - 1))
124
+ raise Retry
125
+ end
126
+ payload = begin
127
+ JSON.parse(res.body.to_s)
128
+ rescue JSON::ParserError
129
+ nil
130
+ end
131
+ err = payload.is_a?(Hash) ? (payload["error"] || {}) : {}
132
+ raise Error.new(code, err["code"] || "http_#{code}", err["message"] || res.message, err["hint"], retry_after, payload)
133
+ rescue Retry
134
+ retry
135
+ rescue SocketError, Timeout::Error, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout, Net::ReadTimeout
136
+ raise if attempt >= @max_retries
137
+
138
+ attempt += 1
139
+ sleep(backoff(attempt - 1))
140
+ retry
141
+ end
142
+ end
143
+
144
+ private
145
+
146
+ class Retry < StandardError; end
147
+
148
+ def perform(uri, method, body, key)
149
+ klass = method == :post ? Net::HTTP::Post : Net::HTTP::Get
150
+ req = klass.new(uri)
151
+ req["Accept"] = "application/json"
152
+ req["User-Agent"] = "moatlight-ruby/#{VERSION}"
153
+ req["Authorization"] = "Bearer #{@token}" if @token
154
+ req["Idempotency-Key"] = key if key
155
+ if body
156
+ req["Content-Type"] = "application/json"
157
+ req.body = JSON.generate(body)
158
+ end
159
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: @timeout, read_timeout: @timeout) do |http|
160
+ http.request(req)
161
+ end
162
+ end
163
+
164
+ def record_rate_limit(res)
165
+ num = ->(k) { (v = res[k]) && v =~ /\A\d+\z/ ? v.to_i : nil }
166
+ @last_rate_limit = { limit: num.("RateLimit-Limit"), remaining: num.("RateLimit-Remaining"), reset_seconds: num.("RateLimit-Reset") }
167
+ end
168
+
169
+ def backoff(attempt)
170
+
171
+ [0.5 * (2**attempt), 8].min + rand * 0.25
172
+
173
+ end
174
+ def esc(id)
175
+ URI.encode_www_form_component(id.to_s).gsub("+", "%20")
176
+ end
177
+ end
178
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moatlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Moatlight
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Measure whether ChatGPT and Google AI Overview name a business. Standard
14
+ library only; writes carry an Idempotency-Key and 429/5xx are retried.
15
+ email: ceo@moatlight.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/moatlight.rb
23
+ - lib/moatlight/version.rb
24
+ homepage: https://www.moatlight.com
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://www.moatlight.com
29
+ source_code_uri: https://github.com/Moatlight/moatlight-agent-kit
30
+ documentation_uri: https://www.moatlight.com/docs
31
+ bug_tracker_uri: https://github.com/Moatlight/moatlight-agent-kit/issues
32
+ rubygems_mfa_required: 'true'
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '2.7'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.0.3.1
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Official Ruby client for the Moatlight API
52
+ test_files: []