peach-ruby 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -4
- data/lib/peach/stream/request.rb +11 -4
- data/lib/peach/stream/response.rb +136 -38
- data/lib/peach/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a7f27b5b01c8b9ea55f876a6250f0c88eb475f5b420357a286a89cf8fa9482e
|
4
|
+
data.tar.gz: 59347383246e9ba8820d31fc97eed2f1a41c58fda460c540e00ea91d9a2fd5b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f82051293a16fdd61f4bc0b8d034e92eaed01f44fb0aa7dcaabc36db8f17b8e8af0c7c55092efe08fe24db6e1c3d9a3ed1ee9d85da7bdce31afdafcc5c0cbb5
|
7
|
+
data.tar.gz: b416cdc3ae802d1466a612e41f806407fe89597c3c113ca46b558e151abed10fce3eaaee7edf0f225cea8d59eef91b85a02722997f8fd3335b33f0b749f41b9a
|
data/README.md
CHANGED
@@ -4,21 +4,21 @@ Ruby library for Peach API
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
8
|
-
|
9
7
|
Install the gem and add to the application's Gemfile by executing:
|
10
8
|
|
11
|
-
$ bundle add
|
9
|
+
$ bundle add peach-ruby
|
12
10
|
|
13
11
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
12
|
|
15
|
-
$ gem install
|
13
|
+
$ gem install peach-ruby
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
19
17
|
Configure the SDK with your API Token:
|
20
18
|
|
21
19
|
```ruby
|
20
|
+
require 'peach-ruby'
|
21
|
+
|
22
22
|
Peach.configure do |config|
|
23
23
|
config.api_token = '<YOUR API TOKEN>'
|
24
24
|
end
|
@@ -27,6 +27,8 @@ end
|
|
27
27
|
And then use it:
|
28
28
|
|
29
29
|
```ruby
|
30
|
+
require 'peach-ruby'
|
31
|
+
|
30
32
|
contact = {
|
31
33
|
id: 1,
|
32
34
|
name: 'John Doe',
|
data/lib/peach/stream/request.rb
CHANGED
@@ -3,11 +3,12 @@
|
|
3
3
|
module Peach
|
4
4
|
module Stream
|
5
5
|
class Request # :nodoc:
|
6
|
-
attr_reader :params, :
|
6
|
+
attr_reader :contact, :params, :prompt, :message, :payload, :context
|
7
7
|
|
8
8
|
STREAM_TYPES = {
|
9
9
|
init: 'stream.init',
|
10
|
-
in_progress: 'stream.in_progress'
|
10
|
+
in_progress: 'stream.in_progress',
|
11
|
+
event_received: 'stream.event_received'
|
11
12
|
}.freeze
|
12
13
|
|
13
14
|
def initialize(raw_params)
|
@@ -24,6 +25,10 @@ module Peach
|
|
24
25
|
@type == STREAM_TYPES[:in_progress]
|
25
26
|
end
|
26
27
|
|
28
|
+
def event_received?
|
29
|
+
@type == STREAM_TYPES[:event_received]
|
30
|
+
end
|
31
|
+
|
27
32
|
private
|
28
33
|
|
29
34
|
def parse_params(raw_params)
|
@@ -45,10 +50,12 @@ module Peach
|
|
45
50
|
|
46
51
|
def instantiate_instance_variables_from(params)
|
47
52
|
@type = params[:type]
|
48
|
-
@params = params.dig(:request, :params) || {}
|
49
|
-
@context = params.dig(:request, :context) || {}
|
50
53
|
@contact = OpenStruct.new(params.dig(:request, :contact) || {})
|
54
|
+
@params = params.dig(:request, :params) || {}
|
51
55
|
@prompt = params.dig(:request, :prompt)
|
56
|
+
@message = OpenStruct.new(params.dig(:request, :message) || {})
|
57
|
+
@payload = params.dig(:request, :payload) || {}
|
58
|
+
@context = params.dig(:request, :context) || {}
|
52
59
|
end
|
53
60
|
|
54
61
|
def deep_symbolize_keys(hash)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'uri'
|
4
|
+
|
3
5
|
module Peach
|
4
6
|
module Stream
|
5
7
|
class Response # :nodoc:
|
@@ -24,21 +26,43 @@ module Peach
|
|
24
26
|
|
25
27
|
private
|
26
28
|
|
27
|
-
def send_message(text,
|
29
|
+
def send_message(text, media_url: nil)
|
28
30
|
message = { text: text }
|
29
|
-
message[:
|
31
|
+
message[:media_url] = media_url if media_url
|
30
32
|
validate_message(message)
|
31
33
|
{ message: message }
|
32
34
|
end
|
33
35
|
|
34
|
-
def send_prompt(key, data: {}, options: [])
|
36
|
+
def send_prompt(key, data: {}, button: nil, options: [])
|
35
37
|
prompt = { key: key }
|
36
38
|
prompt[:data] = data unless data.empty?
|
39
|
+
prompt[:button] = button unless button.nil?
|
37
40
|
prompt[:options] = options unless options.empty?
|
38
41
|
validate_prompt(prompt)
|
39
42
|
{ prompt: prompt }
|
40
43
|
end
|
41
44
|
|
45
|
+
def handoff(user_id: nil, group_id: nil, text: nil)
|
46
|
+
handoff = {}
|
47
|
+
handoff[:message] = { text: text } if text
|
48
|
+
handoff[:user_id] = user_id if user_id
|
49
|
+
handoff[:group_id] = group_id if group_id
|
50
|
+
validate_handoff(handoff)
|
51
|
+
if handoff.empty?
|
52
|
+
{}
|
53
|
+
else
|
54
|
+
{ handoff: handoff }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def opt_out
|
59
|
+
{}
|
60
|
+
end
|
61
|
+
|
62
|
+
def opt_in
|
63
|
+
{}
|
64
|
+
end
|
65
|
+
|
42
66
|
def add_context(data)
|
43
67
|
validate_context data
|
44
68
|
{ context: data }
|
@@ -51,54 +75,128 @@ module Peach
|
|
51
75
|
def validate_message(message)
|
52
76
|
raise Peach::BadResponseError, 'Field `message` does not have a `text`' if (message[:text] || '').empty?
|
53
77
|
|
54
|
-
return unless message.key?(:
|
78
|
+
return unless message.key?(:media_url)
|
55
79
|
|
56
|
-
|
80
|
+
unless message[:media_url].is_a?(String)
|
57
81
|
raise Peach::BadResponseError,
|
58
|
-
'Field `message.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
"Field `message.header_media` does not have a `#{type}.url`"
|
69
|
-
end
|
82
|
+
'Field `message.media_url` is not a String'
|
83
|
+
end
|
84
|
+
if message[:media_url].empty?
|
85
|
+
raise Peach::BadResponseError,
|
86
|
+
'Field `message.media_url` is empty'
|
87
|
+
end
|
88
|
+
begin
|
89
|
+
URI.parse(message[:media_url])
|
90
|
+
rescue URI::InvalidURIError
|
91
|
+
raise Peach::BadResponseError, 'Field `message.media_url` is not a valid URL'
|
70
92
|
end
|
71
93
|
end
|
72
94
|
|
73
95
|
def validate_prompt(prompt)
|
96
|
+
validate_key(prompt)
|
97
|
+
validate_data(prompt)
|
98
|
+
validate_button(prompt) if prompt.key?(:button)
|
99
|
+
validate_options(prompt) if prompt.key?(:options)
|
100
|
+
end
|
101
|
+
|
102
|
+
def validate_key(prompt)
|
74
103
|
raise Peach::BadResponseError, 'Field `prompt` does not have a `key`' if (prompt[:key] || '').empty?
|
104
|
+
end
|
75
105
|
|
76
|
-
|
77
|
-
|
78
|
-
|
106
|
+
def validate_data(prompt)
|
107
|
+
return if !prompt.key?(:data) || prompt[:data].is_a?(Hash)
|
108
|
+
|
109
|
+
raise Peach::BadResponseError, 'Field `prompt.data` is not a Hash'
|
110
|
+
end
|
111
|
+
|
112
|
+
def validate_button(prompt)
|
113
|
+
raise Peach::BadResponseError, 'Field `prompt.button` is not a String' unless prompt[:button].is_a?(String)
|
114
|
+
raise Peach::BadResponseError, 'Field `prompt.button` is empty' if prompt[:button].empty?
|
115
|
+
end
|
116
|
+
|
117
|
+
def validate_options(prompt)
|
118
|
+
raise Peach::BadResponseError, 'Field `prompt.options` is not an Array' unless prompt[:options].is_a?(Array)
|
119
|
+
|
120
|
+
validate_each_option(prompt)
|
121
|
+
end
|
122
|
+
|
123
|
+
def validate_each_option(prompt)
|
124
|
+
prompt[:options].each_with_index do |option, idx|
|
125
|
+
validate_option_is_hash(option, idx)
|
126
|
+
validate_option_title(option, idx)
|
127
|
+
validate_option_value_or_values(prompt, option, idx)
|
79
128
|
end
|
129
|
+
end
|
80
130
|
|
81
|
-
|
131
|
+
def validate_option_is_hash(option, idx)
|
132
|
+
return if option.is_a?(Hash)
|
82
133
|
|
83
|
-
|
84
|
-
|
85
|
-
|
134
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}]` '#{option}' is not a Hash"
|
135
|
+
end
|
136
|
+
|
137
|
+
def validate_option_title(option, idx)
|
138
|
+
return if option.key?(:title) && !option[:title].empty?
|
139
|
+
|
140
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}]` '#{option}' does not have a `title`"
|
141
|
+
end
|
142
|
+
|
143
|
+
def validate_option_value_or_values(prompt, option, idx)
|
144
|
+
if prompt.key?(:button)
|
145
|
+
validate_option_values(option, idx)
|
86
146
|
else
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
147
|
+
validate_option_value(option, idx)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def validate_option_value(option, idx)
|
152
|
+
return if option.key?(:value) && !option[:value].empty?
|
153
|
+
|
154
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}]` '#{option}' does not have a `value`"
|
155
|
+
end
|
156
|
+
|
157
|
+
def validate_option_values(option, idx)
|
158
|
+
unless option.key?(:values)
|
159
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}] does not have `values`"
|
160
|
+
end
|
161
|
+
|
162
|
+
validate_values_are_array(option, idx)
|
163
|
+
validate_each_value(option, idx)
|
164
|
+
end
|
165
|
+
|
166
|
+
def validate_values_are_array(option, idx)
|
167
|
+
unless option[:values].is_a?(Array)
|
168
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}].values` '#{option}' is not an Array"
|
101
169
|
end
|
170
|
+
return unless option[:values].empty?
|
171
|
+
|
172
|
+
raise Peach::BadResponseError, "Field `prompt.options[#{idx}].values` '#{option}' is empty"
|
173
|
+
end
|
174
|
+
|
175
|
+
def validate_each_value(option, idx)
|
176
|
+
option[:values].each_with_index do |item, item_idx|
|
177
|
+
validate_value_title(item, idx, item_idx)
|
178
|
+
validate_value_value(item, idx, item_idx)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def validate_value_title(item, idx, item_idx)
|
183
|
+
return if item.key?(:title) && !item[:title].empty?
|
184
|
+
|
185
|
+
raise Peach::BadResponseError,
|
186
|
+
"Field `prompt.options[#{idx}].values[#{item_idx}]`' '#{item}' does not have a `title`"
|
187
|
+
end
|
188
|
+
|
189
|
+
def validate_value_value(item, idx, item_idx)
|
190
|
+
return if item.key?(:value) && !item[:value].empty?
|
191
|
+
|
192
|
+
raise Peach::BadResponseError,
|
193
|
+
"Field `prompt.options[#{idx}].values[#{item_idx}]` '#{item}' does not have a `value`"
|
194
|
+
end
|
195
|
+
|
196
|
+
def validate_handoff(handoff)
|
197
|
+
return unless handoff.key?(:user_id) && handoff.key?(:group_id)
|
198
|
+
|
199
|
+
raise Peach::BadResponseError, 'Field `handoff` has both `user_id` and `group_id`'
|
102
200
|
end
|
103
201
|
|
104
202
|
def validate_context(context)
|
data/lib/peach/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: peach-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peach Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: debug
|