blueticks 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 +7 -0
- data/CHANGELOG.md +21 -0
- data/LICENSE +21 -0
- data/README.md +85 -0
- data/lib/blueticks/base_resource.rb +12 -0
- data/lib/blueticks/client.rb +107 -0
- data/lib/blueticks/errors.rb +108 -0
- data/lib/blueticks/resources/account.rb +18 -0
- data/lib/blueticks/resources/audiences.rb +69 -0
- data/lib/blueticks/resources/campaigns.rb +52 -0
- data/lib/blueticks/resources/chats.rb +95 -0
- data/lib/blueticks/resources/contacts.rb +27 -0
- data/lib/blueticks/resources/engines.rb +32 -0
- data/lib/blueticks/resources/groups.rb +58 -0
- data/lib/blueticks/resources/messages.rb +48 -0
- data/lib/blueticks/resources/ping.rb +16 -0
- data/lib/blueticks/resources/scheduled_messages.rb +51 -0
- data/lib/blueticks/resources/utils.rb +23 -0
- data/lib/blueticks/resources/webhooks.rb +52 -0
- data/lib/blueticks/transport.rb +161 -0
- data/lib/blueticks/types/account.rb +15 -0
- data/lib/blueticks/types/audiences.rb +29 -0
- data/lib/blueticks/types/base.rb +158 -0
- data/lib/blueticks/types/campaigns.rb +28 -0
- data/lib/blueticks/types/chats.rb +51 -0
- data/lib/blueticks/types/contacts.rb +17 -0
- data/lib/blueticks/types/engines.rb +20 -0
- data/lib/blueticks/types/groups.rb +27 -0
- data/lib/blueticks/types/messages.rb +25 -0
- data/lib/blueticks/types/page.rb +45 -0
- data/lib/blueticks/types/ping.rb +14 -0
- data/lib/blueticks/types/scheduled_messages.rb +23 -0
- data/lib/blueticks/types/utils.rb +19 -0
- data/lib/blueticks/types/webhooks.rb +41 -0
- data/lib/blueticks/version.rb +5 -0
- data/lib/blueticks/webhooks.rb +112 -0
- data/lib/blueticks.rb +35 -0
- metadata +124 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/groups"
|
|
5
|
+
|
|
6
|
+
module Blueticks
|
|
7
|
+
module Resources
|
|
8
|
+
class GroupsResource < BaseResource
|
|
9
|
+
# Create a new group with an initial participant list.
|
|
10
|
+
def create(name:, participants:)
|
|
11
|
+
data = client.request("POST", "/v1/groups",
|
|
12
|
+
body: { "name" => name, "participants" => participants })
|
|
13
|
+
Types::Group.from_hash(data)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Retrieve group metadata by JID.
|
|
17
|
+
def get(group_id)
|
|
18
|
+
data = client.request("GET", "/v1/groups/#{group_id}")
|
|
19
|
+
Types::Group.from_hash(data)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Rename the group and/or update admin-only settings.
|
|
23
|
+
def update(group_id, name: nil, settings: nil)
|
|
24
|
+
body = {}
|
|
25
|
+
body["name"] = name unless name.nil?
|
|
26
|
+
body["settings"] = settings unless settings.nil?
|
|
27
|
+
client.request("PATCH", "/v1/groups/#{group_id}", body: body)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_member(group_id, chat_id:)
|
|
31
|
+
client.request("POST", "/v1/groups/#{group_id}/members", body: { "chat_id" => chat_id })
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def remove_member(group_id, chat_id)
|
|
35
|
+
client.request("DELETE", "/v1/groups/#{group_id}/members/#{chat_id}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def promote_admin(group_id, chat_id)
|
|
39
|
+
client.request("POST", "/v1/groups/#{group_id}/members/#{chat_id}/admin")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def demote_admin(group_id, chat_id)
|
|
43
|
+
client.request("DELETE", "/v1/groups/#{group_id}/members/#{chat_id}/admin")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def set_picture(group_id, file_data_url:, file_name: nil, file_mime_type: nil)
|
|
47
|
+
body = { "file_data_url" => file_data_url }
|
|
48
|
+
body["file_name"] = file_name unless file_name.nil?
|
|
49
|
+
body["file_mime_type"] = file_mime_type unless file_mime_type.nil?
|
|
50
|
+
client.request("PUT", "/v1/groups/#{group_id}/picture", body: body)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def leave(group_id)
|
|
54
|
+
client.request("DELETE", "/v1/groups/#{group_id}/members/me")
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/messages"
|
|
5
|
+
require_relative "../types/page"
|
|
6
|
+
|
|
7
|
+
module Blueticks
|
|
8
|
+
module Resources
|
|
9
|
+
class MessagesResource < BaseResource
|
|
10
|
+
# Send a message immediately, or schedule one for later with `send_at:`.
|
|
11
|
+
#
|
|
12
|
+
# @param to [String] WhatsApp chat-id or phone number
|
|
13
|
+
# @param text [String, nil] Plain-text message body
|
|
14
|
+
# @param media_url [String, nil] Public URL to media to attach
|
|
15
|
+
# @param media_caption [String, nil] Caption for media
|
|
16
|
+
# @param send_at [String, nil] ISO 8601 timestamp to schedule
|
|
17
|
+
# @param from [String, nil] Multi-number engine identifier
|
|
18
|
+
# @param idempotency_key [String, nil] Optional idempotency key
|
|
19
|
+
# @return [Blueticks::Types::Message]
|
|
20
|
+
def send(to:, text: nil, media_url: nil, media_caption: nil, send_at: nil,
|
|
21
|
+
from: nil, idempotency_key: nil)
|
|
22
|
+
body = { "to" => to }
|
|
23
|
+
body["text"] = text unless text.nil?
|
|
24
|
+
body["media_url"] = media_url unless media_url.nil?
|
|
25
|
+
body["media_caption"] = media_caption unless media_caption.nil?
|
|
26
|
+
body["send_at"] = send_at unless send_at.nil?
|
|
27
|
+
body["from"] = from unless from.nil?
|
|
28
|
+
data = client.request("POST", "/v1/messages", body: body, idempotency_key: idempotency_key)
|
|
29
|
+
Types::Message.from_hash(data)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Retrieve a single message by id.
|
|
33
|
+
def get(message_id)
|
|
34
|
+
data = client.request("GET", "/v1/messages/#{message_id}")
|
|
35
|
+
Types::Message.from_hash(data)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# List messages sent through the API, newest first. Cursor-paginated.
|
|
39
|
+
def list(limit: nil, cursor: nil)
|
|
40
|
+
params = {}
|
|
41
|
+
params["limit"] = limit unless limit.nil?
|
|
42
|
+
params["cursor"] = cursor unless cursor.nil?
|
|
43
|
+
data = client.request("GET", "/v1/messages", params: params.empty? ? nil : params)
|
|
44
|
+
Types::Page.from_hash(data, item_type: Types::Message)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/ping"
|
|
5
|
+
|
|
6
|
+
module Blueticks
|
|
7
|
+
module Resources
|
|
8
|
+
class PingResource < BaseResource
|
|
9
|
+
# Health check. Returns basic info about the authenticated API key.
|
|
10
|
+
def retrieve
|
|
11
|
+
data = client.request("GET", "/v1/ping")
|
|
12
|
+
Types::Ping.from_hash(data)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/scheduled_messages"
|
|
5
|
+
require_relative "../types/page"
|
|
6
|
+
|
|
7
|
+
module Blueticks
|
|
8
|
+
module Resources
|
|
9
|
+
class ScheduledMessagesResource < BaseResource
|
|
10
|
+
# List scheduled messages.
|
|
11
|
+
#
|
|
12
|
+
# Retrieves a list of all resources from the service. Cursor-paginated.
|
|
13
|
+
def list(limit: nil, cursor: nil)
|
|
14
|
+
params = {}
|
|
15
|
+
params["limit"] = limit unless limit.nil?
|
|
16
|
+
params["cursor"] = cursor unless cursor.nil?
|
|
17
|
+
data = client.request("GET", "/v1/scheduled-messages", params: params.empty? ? nil : params)
|
|
18
|
+
Types::Page.from_hash(data, item_type: Types::ScheduledMessage)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Get scheduled message.
|
|
22
|
+
#
|
|
23
|
+
# Retrieves a single resource with the given id from the service.
|
|
24
|
+
def retrieve(id)
|
|
25
|
+
data = client.request("GET", "/v1/scheduled-messages/#{id}")
|
|
26
|
+
Types::ScheduledMessage.from_hash(data)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Update scheduled message.
|
|
30
|
+
#
|
|
31
|
+
# Updates the resource identified by id using data.
|
|
32
|
+
def update(id, text: nil, media_url: nil, media_caption: nil, send_at: nil)
|
|
33
|
+
body = {}
|
|
34
|
+
body["text"] = text unless text.nil?
|
|
35
|
+
body["media_url"] = media_url unless media_url.nil?
|
|
36
|
+
body["media_caption"] = media_caption unless media_caption.nil?
|
|
37
|
+
body["send_at"] = send_at unless send_at.nil?
|
|
38
|
+
data = client.request("PATCH", "/v1/scheduled-messages/#{id}", body: body)
|
|
39
|
+
Types::ScheduledMessage.from_hash(data)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Cancel scheduled message.
|
|
43
|
+
#
|
|
44
|
+
# Removes the resource with id.
|
|
45
|
+
def delete(id)
|
|
46
|
+
data = client.request("DELETE", "/v1/scheduled-messages/#{id}")
|
|
47
|
+
Types::ScheduledMessage.from_hash(data)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/utils"
|
|
5
|
+
|
|
6
|
+
module Blueticks
|
|
7
|
+
module Resources
|
|
8
|
+
class UtilsResource < BaseResource
|
|
9
|
+
# Validate a phone number or chat-id; returns the engine's canonical form.
|
|
10
|
+
def validate_phone(phone_or_chat_id:)
|
|
11
|
+
data = client.request("POST", "/v1/utils/validate_phone",
|
|
12
|
+
body: { "phone_or_chat_id" => phone_or_chat_id })
|
|
13
|
+
Types::PhoneValidation.from_hash(data)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Fetch OpenGraph-style metadata for a URL (uses the engine's renderer).
|
|
17
|
+
def link_preview(url:)
|
|
18
|
+
data = client.request("GET", "/v1/utils/link_preview", params: { "url" => url })
|
|
19
|
+
Types::LinkPreview.from_hash(data)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../base_resource"
|
|
4
|
+
require_relative "../types/webhooks"
|
|
5
|
+
require_relative "../types/page"
|
|
6
|
+
|
|
7
|
+
module Blueticks
|
|
8
|
+
module Resources
|
|
9
|
+
class WebhooksResource < BaseResource
|
|
10
|
+
def create(url:, events:, description: nil)
|
|
11
|
+
body = { "url" => url, "events" => events }
|
|
12
|
+
body["description"] = description unless description.nil?
|
|
13
|
+
data = client.request("POST", "/v1/webhooks", body: body)
|
|
14
|
+
Types::WebhookCreateResult.from_hash(data)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# List webhooks, newest first. Cursor-paginated.
|
|
18
|
+
def list(limit: nil, cursor: nil)
|
|
19
|
+
params = {}
|
|
20
|
+
params["limit"] = limit unless limit.nil?
|
|
21
|
+
params["cursor"] = cursor unless cursor.nil?
|
|
22
|
+
data = client.request("GET", "/v1/webhooks", params: params.empty? ? nil : params)
|
|
23
|
+
Types::Page.from_hash(data, item_type: Types::Webhook)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def get(webhook_id)
|
|
27
|
+
data = client.request("GET", "/v1/webhooks/#{webhook_id}")
|
|
28
|
+
Types::Webhook.from_hash(data)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def update(webhook_id, url: nil, events: nil, description: nil, status: nil)
|
|
32
|
+
body = {}
|
|
33
|
+
body["url"] = url unless url.nil?
|
|
34
|
+
body["events"] = events unless events.nil?
|
|
35
|
+
body["description"] = description unless description.nil?
|
|
36
|
+
body["status"] = status unless status.nil?
|
|
37
|
+
data = client.request("PATCH", "/v1/webhooks/#{webhook_id}", body: body)
|
|
38
|
+
Types::Webhook.from_hash(data)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def delete(webhook_id)
|
|
42
|
+
client.request("DELETE", "/v1/webhooks/#{webhook_id}")
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def rotate_secret(webhook_id)
|
|
47
|
+
data = client.request("POST", "/v1/webhooks/#{webhook_id}/rotate-secret")
|
|
48
|
+
Types::WebhookCreateResult.from_hash(data)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
require_relative "errors"
|
|
8
|
+
require_relative "version"
|
|
9
|
+
|
|
10
|
+
module Blueticks
|
|
11
|
+
# HTTP transport for the Blueticks API. Wraps Net::HTTP with retry/backoff.
|
|
12
|
+
class Transport
|
|
13
|
+
RETRIABLE_STATUS = [429, 502, 503, 504].freeze
|
|
14
|
+
IDEMPOTENT_METHODS = %w[GET HEAD OPTIONS DELETE PATCH PUT].freeze
|
|
15
|
+
BACKOFF_BASE = 0.5
|
|
16
|
+
BACKOFF_CAP = 8.0
|
|
17
|
+
RETRIABLE_NETWORK_ERRORS = [
|
|
18
|
+
Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
|
|
19
|
+
Net::OpenTimeout, Net::ReadTimeout, SocketError, EOFError, IOError
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
METHOD_CLASSES = {
|
|
23
|
+
"GET" => Net::HTTP::Get,
|
|
24
|
+
"POST" => Net::HTTP::Post,
|
|
25
|
+
"PUT" => Net::HTTP::Put,
|
|
26
|
+
"PATCH" => Net::HTTP::Patch,
|
|
27
|
+
"DELETE" => Net::HTTP::Delete,
|
|
28
|
+
"HEAD" => Net::HTTP::Head
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
def initialize(api_key:, base_url:, timeout: 30.0, max_retries: 3, user_agent: nil, http: nil)
|
|
32
|
+
@api_key = api_key
|
|
33
|
+
@base_url = base_url.sub(%r{/+\z}, "")
|
|
34
|
+
@timeout = timeout
|
|
35
|
+
@max_retries = max_retries
|
|
36
|
+
@user_agent = "blueticks-ruby/#{Blueticks::VERSION}"
|
|
37
|
+
@user_agent = "#{@user_agent} #{user_agent}" if user_agent && !user_agent.empty?
|
|
38
|
+
@http = http # Optional injected client for testing.
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Issue a request. Returns the parsed JSON body, or nil on 204/empty.
|
|
42
|
+
def request(method, path, params: nil, body: nil, idempotency_key: nil)
|
|
43
|
+
method = method.to_s.upcase
|
|
44
|
+
uri = build_uri(path, params)
|
|
45
|
+
is_idempotent = IDEMPOTENT_METHODS.include?(method) || !idempotency_key.nil?
|
|
46
|
+
|
|
47
|
+
attempt = 0
|
|
48
|
+
loop do
|
|
49
|
+
req = build_request(method, uri, body: body, idempotency_key: idempotency_key)
|
|
50
|
+
begin
|
|
51
|
+
response = perform(uri, req)
|
|
52
|
+
rescue *RETRIABLE_NETWORK_ERRORS => e
|
|
53
|
+
if is_idempotent && attempt < @max_retries
|
|
54
|
+
sleep_backoff(attempt, retry_after: nil)
|
|
55
|
+
attempt += 1
|
|
56
|
+
next
|
|
57
|
+
end
|
|
58
|
+
raise Errors::APIConnectionError.new(
|
|
59
|
+
message: "connection error: #{e.class}: #{e.message}",
|
|
60
|
+
status_code: nil,
|
|
61
|
+
code: nil,
|
|
62
|
+
request_id: nil
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
return parse_success(response) if (200..299).cover?(response.code.to_i)
|
|
67
|
+
|
|
68
|
+
retry_after = response.code.to_i == 429 ? parse_retry_after(response) : nil
|
|
69
|
+
retriable = RETRIABLE_STATUS.include?(response.code.to_i)
|
|
70
|
+
if retriable && is_idempotent && attempt < @max_retries
|
|
71
|
+
sleep_backoff(attempt, retry_after: retry_after)
|
|
72
|
+
attempt += 1
|
|
73
|
+
next
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
raise build_api_error(response, retry_after: retry_after)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def build_uri(path, params)
|
|
83
|
+
uri = URI.parse("#{@base_url}#{path}")
|
|
84
|
+
if params && !params.empty?
|
|
85
|
+
clean = params.compact
|
|
86
|
+
uri.query = URI.encode_www_form(clean) unless clean.empty?
|
|
87
|
+
end
|
|
88
|
+
uri
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def build_request(method, uri, body:, idempotency_key:)
|
|
92
|
+
klass = METHOD_CLASSES.fetch(method) do
|
|
93
|
+
raise ArgumentError, "Unsupported HTTP method: #{method}"
|
|
94
|
+
end
|
|
95
|
+
req = klass.new(uri.request_uri)
|
|
96
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
|
97
|
+
req["User-Agent"] = @user_agent
|
|
98
|
+
req["Accept"] = "application/json"
|
|
99
|
+
req["Idempotency-Key"] = idempotency_key if idempotency_key
|
|
100
|
+
if body
|
|
101
|
+
req["Content-Type"] = "application/json"
|
|
102
|
+
req.body = JSON.generate(body)
|
|
103
|
+
end
|
|
104
|
+
req
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def perform(uri, req)
|
|
108
|
+
if @http
|
|
109
|
+
@http.request(req)
|
|
110
|
+
else
|
|
111
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
112
|
+
http.use_ssl = (uri.scheme == "https")
|
|
113
|
+
http.read_timeout = @timeout
|
|
114
|
+
http.open_timeout = @timeout
|
|
115
|
+
http.request(req)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def parse_success(response)
|
|
120
|
+
raw = response.body
|
|
121
|
+
return nil if raw.nil? || raw.empty?
|
|
122
|
+
|
|
123
|
+
JSON.parse(raw)
|
|
124
|
+
rescue JSON::ParserError
|
|
125
|
+
nil
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def build_api_error(response, retry_after: nil)
|
|
129
|
+
status = response.code.to_i
|
|
130
|
+
raw = response.body
|
|
131
|
+
parsed = begin
|
|
132
|
+
raw && !raw.empty? ? JSON.parse(raw) : raw
|
|
133
|
+
rescue JSON::ParserError
|
|
134
|
+
raw
|
|
135
|
+
end
|
|
136
|
+
Errors.from_envelope(
|
|
137
|
+
status_code: status,
|
|
138
|
+
body: parsed,
|
|
139
|
+
response: response,
|
|
140
|
+
retry_after: retry_after
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def parse_retry_after(response)
|
|
145
|
+
raw = response["retry-after"]
|
|
146
|
+
return nil if raw.nil?
|
|
147
|
+
|
|
148
|
+
Float(raw)
|
|
149
|
+
rescue ArgumentError, TypeError
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def sleep_backoff(attempt, retry_after:)
|
|
154
|
+
return sleep(retry_after) if retry_after
|
|
155
|
+
|
|
156
|
+
backoff = [BACKOFF_BASE * (2**attempt), BACKOFF_CAP].min
|
|
157
|
+
jitter = rand * BACKOFF_BASE
|
|
158
|
+
sleep(backoff + jitter)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base"
|
|
4
|
+
|
|
5
|
+
module Blueticks
|
|
6
|
+
module Types
|
|
7
|
+
# Response from GET /v1/account.
|
|
8
|
+
class Account < Base
|
|
9
|
+
field :id, :string
|
|
10
|
+
field :name, :string
|
|
11
|
+
field :timezone, :string, nullable: true
|
|
12
|
+
field :created_at, :string
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base"
|
|
4
|
+
|
|
5
|
+
module Blueticks
|
|
6
|
+
module Types
|
|
7
|
+
class Contact < Base
|
|
8
|
+
field :id, :string
|
|
9
|
+
field :to, :string
|
|
10
|
+
field :variables, %i[hash_of string]
|
|
11
|
+
field :added_at, :string
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Audience < Base
|
|
15
|
+
field :id, :string
|
|
16
|
+
field :name, :string
|
|
17
|
+
field :contact_count, :integer
|
|
18
|
+
field :created_at, :string
|
|
19
|
+
field :contacts, [:array, Contact], optional: true
|
|
20
|
+
field :page, :integer, optional: true
|
|
21
|
+
field :has_more, :boolean, optional: true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class AppendContactsResult < Base
|
|
25
|
+
field :added, :integer
|
|
26
|
+
field :contact_count, :integer
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module Blueticks
|
|
6
|
+
module Types
|
|
7
|
+
# Lightweight DTO base class. Subclasses declare fields with `field` + a type spec.
|
|
8
|
+
# Provides `.from_hash(raw)` that asserts shape and raises ValidationError on mismatch.
|
|
9
|
+
#
|
|
10
|
+
# Type specs supported:
|
|
11
|
+
# :string, :integer, :number, :boolean, :hash, :any
|
|
12
|
+
# [:array, inner_spec] — array of inner_spec values
|
|
13
|
+
# [:enum, [..values..]] — string in allowed set
|
|
14
|
+
# [:hash_of, value_spec] — Hash<String, value_spec>
|
|
15
|
+
# SomeTypeClass — nested DTO; calls .from_hash on it
|
|
16
|
+
#
|
|
17
|
+
# Mark fields nullable: true to permit nil. Mark optional: true to permit missing.
|
|
18
|
+
class Base
|
|
19
|
+
class << self
|
|
20
|
+
def fields
|
|
21
|
+
@fields ||= {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def field(name, type, nullable: false, optional: false, wire_name: nil)
|
|
25
|
+
wire = (wire_name || name).to_s
|
|
26
|
+
fields[name] = { type: type, nullable: nullable, optional: optional, wire: wire }
|
|
27
|
+
attr_reader name
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def from_hash(raw, context: name)
|
|
31
|
+
unless raw.is_a?(Hash)
|
|
32
|
+
raise Errors::ValidationError.new(
|
|
33
|
+
message: "Expected Hash for #{context}, got #{raw.class}"
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
values = {}
|
|
38
|
+
fields.each do |attr, spec|
|
|
39
|
+
wire = spec[:wire]
|
|
40
|
+
present = raw.key?(wire)
|
|
41
|
+
unless present
|
|
42
|
+
if spec[:optional] || spec[:nullable]
|
|
43
|
+
values[attr] = nil
|
|
44
|
+
next
|
|
45
|
+
end
|
|
46
|
+
raise Errors::ValidationError.new(
|
|
47
|
+
message: "Missing required field '#{wire}' in #{context}"
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
v = raw[wire]
|
|
52
|
+
if v.nil?
|
|
53
|
+
if spec[:nullable] || spec[:optional]
|
|
54
|
+
values[attr] = nil
|
|
55
|
+
next
|
|
56
|
+
end
|
|
57
|
+
raise Errors::ValidationError.new(
|
|
58
|
+
message: "Field '#{wire}' may not be null in #{context}"
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
values[attr] = coerce(v, spec[:type], "#{context}.#{wire}")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
new(values)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def coerce(value, type, path)
|
|
69
|
+
case type
|
|
70
|
+
when :string
|
|
71
|
+
assert_kind!(value, String, path, "string")
|
|
72
|
+
value
|
|
73
|
+
when :integer
|
|
74
|
+
unless value.is_a?(Integer) && !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
|
|
75
|
+
raise Errors::ValidationError.new(
|
|
76
|
+
message: "Field '#{path}' must be integer, got #{value.class}"
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
value
|
|
80
|
+
when :number
|
|
81
|
+
unless value.is_a?(Numeric) && !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
|
|
82
|
+
raise Errors::ValidationError.new(
|
|
83
|
+
message: "Field '#{path}' must be number, got #{value.class}"
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
value
|
|
87
|
+
when :boolean
|
|
88
|
+
unless [true, false].include?(value)
|
|
89
|
+
raise Errors::ValidationError.new(
|
|
90
|
+
message: "Field '#{path}' must be boolean, got #{value.class}"
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
value
|
|
94
|
+
when :hash
|
|
95
|
+
assert_kind!(value, Hash, path, "hash")
|
|
96
|
+
value
|
|
97
|
+
when :any
|
|
98
|
+
value
|
|
99
|
+
when Array
|
|
100
|
+
head = type[0]
|
|
101
|
+
case head
|
|
102
|
+
when :array
|
|
103
|
+
assert_kind!(value, Array, path, "array")
|
|
104
|
+
value.each_with_index.map { |item, i| coerce(item, type[1], "#{path}[#{i}]") }
|
|
105
|
+
when :enum
|
|
106
|
+
allowed = type[1]
|
|
107
|
+
unless allowed.include?(value)
|
|
108
|
+
raise Errors::ValidationError.new(
|
|
109
|
+
message: "Field '#{path}' must be one of #{allowed.inspect}, got #{value.inspect}"
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
value
|
|
113
|
+
when :hash_of
|
|
114
|
+
assert_kind!(value, Hash, path, "hash")
|
|
115
|
+
out = {}
|
|
116
|
+
value.each { |k, v| out[k.to_s] = coerce(v, type[1], "#{path}.#{k}") }
|
|
117
|
+
out
|
|
118
|
+
else
|
|
119
|
+
raise ArgumentError, "Unknown compound type spec: #{type.inspect}"
|
|
120
|
+
end
|
|
121
|
+
else
|
|
122
|
+
raise ArgumentError, "Unknown type spec: #{type.inspect}" unless type.is_a?(Class) && type < Base
|
|
123
|
+
|
|
124
|
+
type.from_hash(value, context: "#{path} (#{type.name})")
|
|
125
|
+
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def assert_kind!(value, expected, path, label)
|
|
130
|
+
return if value.is_a?(expected)
|
|
131
|
+
|
|
132
|
+
raise Errors::ValidationError.new(
|
|
133
|
+
message: "Field '#{path}' must be #{label}, got #{value.class}"
|
|
134
|
+
)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def initialize(values)
|
|
139
|
+
values.each { |k, v| instance_variable_set("@#{k}", v) }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def to_h
|
|
143
|
+
out = {}
|
|
144
|
+
self.class.fields.each_key { |k| out[k] = instance_variable_get("@#{k}") }
|
|
145
|
+
out
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def ==(other)
|
|
149
|
+
other.is_a?(self.class) && to_h == other.to_h
|
|
150
|
+
end
|
|
151
|
+
alias eql? ==
|
|
152
|
+
|
|
153
|
+
def hash
|
|
154
|
+
to_h.hash
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base"
|
|
4
|
+
|
|
5
|
+
module Blueticks
|
|
6
|
+
module Types
|
|
7
|
+
CAMPAIGN_STATUSES = %w[
|
|
8
|
+
pending running paused complete_sent complete_delivered aborted
|
|
9
|
+
].freeze
|
|
10
|
+
|
|
11
|
+
class Campaign < Base
|
|
12
|
+
field :id, :string
|
|
13
|
+
field :name, :string
|
|
14
|
+
field :audience_id, :string
|
|
15
|
+
field :status, [:enum, CAMPAIGN_STATUSES]
|
|
16
|
+
field :total_count, :integer
|
|
17
|
+
field :sent_count, :integer
|
|
18
|
+
field :delivered_count, :integer
|
|
19
|
+
field :read_count, :integer
|
|
20
|
+
field :failed_count, :integer
|
|
21
|
+
field :from, :string, nullable: true
|
|
22
|
+
field :created_at, :string
|
|
23
|
+
field :started_at, :string, nullable: true
|
|
24
|
+
field :completed_at, :string, nullable: true
|
|
25
|
+
field :aborted_at, :string, nullable: true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|