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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6bbf4a34a9d69b838e5a2a0e5562ad0f598ceae6521c5a0738d8966afdebba2
4
- data.tar.gz: 5a789324350150dd3a0dfd1e9ad04655fbcd87701f365d4d51998ee40a5cb370
3
+ metadata.gz: 8a7f27b5b01c8b9ea55f876a6250f0c88eb475f5b420357a286a89cf8fa9482e
4
+ data.tar.gz: 59347383246e9ba8820d31fc97eed2f1a41c58fda460c540e00ea91d9a2fd5b6
5
5
  SHA512:
6
- metadata.gz: 49263d5ee3c1089f55ee635afcb843a7779db1c3d1ad024f902018474143b3ea1d68cf3caf8ae72f9db6455463432cae8729e330eb699e4e9f7da783c78c1eaa
7
- data.tar.gz: 9732144b1e654ae41a4f886ae08841d5bb97a37ccd2dba69664258722340b6c45651278f57d79888630a844a5827dff6ac87b7a59b8b0829082677b5fffbfbd0
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 UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
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 UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
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',
@@ -3,11 +3,12 @@
3
3
  module Peach
4
4
  module Stream
5
5
  class Request # :nodoc:
6
- attr_reader :params, :context, :contact, :prompt
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, header_media: {})
29
+ def send_message(text, media_url: nil)
28
30
  message = { text: text }
29
- message[:header_media] = header_media unless header_media.empty?
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?(:header_media)
78
+ return unless message.key?(:media_url)
55
79
 
56
- if !message[:header_media].is_a?(Hash)
80
+ unless message[:media_url].is_a?(String)
57
81
  raise Peach::BadResponseError,
58
- 'Field `message.header_media` is not a Hash'
59
- else
60
- raise Peach::BadResponseError, 'Field `message.header_media` does not have a `type`' if message.dig(
61
- :header_media, :type
62
- ).nil?
63
-
64
- type = message.dig(:header_media, :type)
65
- if message.dig(:header_media, type,
66
- :url).nil?
67
- raise Peach::BadResponseError,
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
- if prompt.key?(:data) && !prompt[:data].is_a?(Hash)
77
- raise Peach::BadResponseError,
78
- 'Field `prompt.data` is not a Hash'
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
- return unless prompt.key?(:options)
131
+ def validate_option_is_hash(option, idx)
132
+ return if option.is_a?(Hash)
82
133
 
83
- if !prompt[:options].is_a?(Array)
84
- raise Peach::BadResponseError,
85
- 'Field `prompt.options` is not an Array'
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
- prompt[:options].each do |option|
88
- unless option.is_a?(Hash)
89
- raise Peach::BadResponseError,
90
- "Field `prompt.options`'s item #{option} is not a Hash"
91
- end
92
- if (option[:title] || '').empty?
93
- raise Peach::BadResponseError,
94
- "Field `prompt.options`'s item #{option} does not have a `title`"
95
- end
96
- if (option[:value] || '').empty?
97
- raise Peach::BadResponseError,
98
- "Field `prompt.options`'s item #{option} does not have a `value`"
99
- end
100
- end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Peach
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.0'
5
5
  end
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.1.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-02 00:00:00.000000000 Z
11
+ date: 2024-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debug