urbanairship 5.4.0 → 5.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,16 +19,6 @@ module Urbanairship
19
19
 
20
20
  def initialize(client: required('client'))
21
21
  @client = client
22
- @address = nil
23
- @commercial_opted_in = nil
24
- @commercial_opted_out = nil
25
- @locale_country = nil
26
- @locale_language = nil
27
- @timezone = nil
28
- @transactional_opted_in = nil
29
- @transactional_opted_out = nil
30
- @type = nil
31
- @channel_id = nil
32
22
  end
33
23
 
34
24
  def register
@@ -36,15 +26,15 @@ module Urbanairship
36
26
 
37
27
  payload = {
38
28
  'channel': {
39
- 'address': @address,
40
- 'commercial_opted_in': @commercial_opted_in,
41
- 'commercial_opted_out': @commercial_opted_out,
42
- 'locale_country': @locale_country,
43
- 'locale_language': @locale_language,
44
- 'timezone': @timezone,
45
- 'transactional_opted_in': @transactional_opted_in,
46
- 'transactional_opted_out': @transactional_opted_out,
47
- 'type': @type
29
+ 'address': address,
30
+ 'commercial_opted_in': commercial_opted_in,
31
+ 'commercial_opted_out': commercial_opted_out,
32
+ 'locale_country': locale_country,
33
+ 'locale_language': locale_language,
34
+ 'timezone': timezone,
35
+ 'transactional_opted_in': transactional_opted_in,
36
+ 'transactional_opted_out': transactional_opted_out,
37
+ 'type': type
48
38
  }
49
39
  }
50
40
 
@@ -54,7 +44,7 @@ module Urbanairship
54
44
  url: CHANNEL_URL + 'email',
55
45
  content_type: 'application/json'
56
46
  )
57
- logger.info("Registering email channel with address #{@address}")
47
+ logger.info("Registering email channel with address #{address}")
58
48
  response
59
49
  end
60
50
 
@@ -62,7 +52,7 @@ module Urbanairship
62
52
  fail ArgumentError, 'address must be set to register email channel' if @address.nil?
63
53
 
64
54
  payload = {
65
- 'email_address': @address
55
+ 'email_address': address
66
56
  }
67
57
 
68
58
  response = @client.send_request(
@@ -71,7 +61,7 @@ module Urbanairship
71
61
  url: CHANNEL_URL + 'email/uninstall',
72
62
  content_type: 'application/json'
73
63
  )
74
- logger.info("Uninstalling email channel with address #{@address}")
64
+ logger.info("Uninstalling email channel with address #{address}")
75
65
  response
76
66
  end
77
67
 
@@ -80,32 +70,32 @@ module Urbanairship
80
70
 
81
71
  response = @client.send_request(
82
72
  method: 'GET',
83
- url: CHANNEL_URL + 'email/' + @address
73
+ url: CHANNEL_URL + 'email/' + address
84
74
  )
85
- logger.info("Looking up email channel with address #{@address}")
75
+ logger.info("Looking up email channel with address #{address}")
86
76
  response
87
77
  end
88
78
 
89
79
  def update
90
- fail ArgumentError, 'address must be set to update email channel' if @channel_id.nil?
91
-
92
- channel_data = {}
93
-
94
- channel_data['address'] = @address if @address
95
- channel_data['commercial_opted_in'] = @commercial_opted_in if @commercial_opted_in
96
- channel_data['commercial_opted_out'] = @commercial_opted_out if @commercial_opted_out
97
- channel_data['locale_country'] = @locale_country if @locale_country
98
- channel_data['locale_language'] = @locale_language if @locale_language
99
- channel_data['timezone'] = @timezone if @timezone
100
- channel_data['transactional_opted_in'] = @transactional_opted_in if @transactional_opted_in
101
- channel_data['transactional_opted_out'] = @transactional_opted_out if @transactional_opted_out
102
- channel_data['type'] = @type if @type
103
-
104
- payload = {channel: channel_data}
80
+ fail ArgumentError, 'address must be set to update email channel' if channel_id.nil?
81
+
82
+ channel_data = {
83
+ 'address': address,
84
+ 'commercial_opted_in': commercial_opted_in,
85
+ 'commercial_opted_out': commercial_opted_out,
86
+ 'localte_country': locale_country,
87
+ 'locale_language': locale_language,
88
+ 'timezone': timezone,
89
+ 'transactional_opted_in': transactional_opted_in,
90
+ 'transactional_opted_out': transactional_opted_out,
91
+ 'type': type
92
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
93
+
94
+ payload = {'channel': channel_data}
105
95
 
106
96
  response = @client.send_request(
107
97
  method: 'PUT',
108
- url: CHANNEL_URL + 'email/' + @channel_id,
98
+ url: CHANNEL_URL + 'email/' + channel_id,
109
99
  body: JSON.dump(payload),
110
100
  content_type: 'application/json'
111
101
  )
@@ -0,0 +1,92 @@
1
+ require 'urbanairship'
2
+ require 'json'
3
+
4
+ module Urbanairship
5
+ module Devices
6
+ class EmailNotification
7
+ include Urbanairship::Common
8
+ include Urbanairship::Loggable
9
+ attr_accessor :bcc,
10
+ :bypass_opt_in_level,
11
+ :html_body,
12
+ :message_type,
13
+ :plaintext_body,
14
+ :reply_to,
15
+ :sender_address,
16
+ :sender_name,
17
+ :subject,
18
+ :template_id,
19
+ :variable_details,
20
+ :click_tracking,
21
+ :open_tracking
22
+
23
+ def initialize(client: required('client'))
24
+ @client = client
25
+ end
26
+
27
+ def email_override
28
+ fail ArgumentError, 'message_type is needed for email override' if @message_type.nil?
29
+ fail ArgumentError, 'plaintext_body is needed for email override' if @plaintext_body.nil?
30
+ fail ArgumentError, 'reply_to is needed for email override' if @reply_to.nil?
31
+ fail ArgumentError, 'sender_address is needed for email override' if @sender_address.nil?
32
+ fail ArgumentError, 'sender_name is needed for email override' if @sender_name.nil?
33
+ fail ArgumentError, 'subject is needed for email override' if @subject.nil?
34
+
35
+ override = {
36
+ bcc: bcc,
37
+ bypass_opt_in_level: bypass_opt_in_level,
38
+ click_tracking: click_tracking,
39
+ html_body: html_body,
40
+ message_type: message_type,
41
+ open_tracking: open_tracking,
42
+ plaintext_body: plaintext_body,
43
+ reply_to: reply_to,
44
+ sender_address: sender_address,
45
+ sender_name: sender_name,
46
+ subject: subject
47
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
48
+
49
+ {'email': override}
50
+ end
51
+
52
+ def email_with_inline_template
53
+ fail ArgumentError, 'message_type is needed for email with inline template' if @message_type.nil?
54
+ fail ArgumentError, 'reply_to is needed for email with inline template' if @reply_to.nil?
55
+ fail ArgumentError, 'sender_address is needed for email with inline template' if @sender_address.nil?
56
+ fail ArgumentError, 'sender_name is needed for email with inline template' if @sender_name.nil?
57
+
58
+ inline_template = {
59
+ bcc: bcc,
60
+ click_tracking: click_tracking,
61
+ message_type: message_type,
62
+ open_tracking: open_tracking,
63
+ reply_to: reply_to,
64
+ sender_address: sender_address,
65
+ sender_name: sender_name,
66
+ template: define_template_object
67
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
68
+
69
+ {'email': inline_template}
70
+ end
71
+
72
+ def define_template_object
73
+ fail ArgumentError, 'Must choose between template_id or fields object' if template_id && plaintext_body && subject
74
+ template_portion = {
75
+ template_id: template_id,
76
+ fields: define_fields,
77
+ variable_details: variable_details
78
+ }.delete_if {|key, value| value.nil?}
79
+ end
80
+
81
+ def define_fields
82
+ if subject && plaintext_body
83
+ {
84
+ subject: subject,
85
+ plaintext_body: plaintext_body
86
+ }
87
+ end
88
+ end
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,107 @@
1
+ require 'urbanairship'
2
+ require 'json'
3
+
4
+ module Urbanairship
5
+ module Devices
6
+ class MmsNotification
7
+ include Urbanairship::Common
8
+ include Urbanairship::Loggable
9
+
10
+ attr_accessor :fallback_text,
11
+ :shorten_links,
12
+ :content_length,
13
+ :content_type,
14
+ :url,
15
+ :text,
16
+ :subject,
17
+ :template_id,
18
+ :slide_1_text
19
+
20
+ def initialize(client: required('client'))
21
+ @client = client
22
+ end
23
+
24
+ def validate_url
25
+ unless ['.jpg', '.gif', '.png', 'jpeg'].include?(@url[-4..-1])
26
+ fail ArgumentError, 'url must end in .gif, .jpg, .png, or .jpeg'
27
+ end
28
+ end
29
+
30
+ def mms_override
31
+ fail ArgumentError, 'fallback_text is needed for MMS override' if fallback_text.nil?
32
+ fail ArgumentError, 'content_length is needed for MMS override' if content_length.nil?
33
+ fail ArgumentError, 'content_type is needed for MMS override' if content_type.nil?
34
+ fail ArgumentError, 'url is needed for MMS override' if url.nil?
35
+
36
+ validate_url
37
+
38
+ override = {"mms": {
39
+ "subject": subject,
40
+ "fallback_text": fallback_text,
41
+ "shorten_links": shorten_links,
42
+ "slides": [
43
+ {
44
+ "text": text,
45
+ "media": {
46
+ "url": url,
47
+ "content_type": content_type,
48
+ "content_length": content_length
49
+ }
50
+ }
51
+ ]
52
+ }
53
+ }
54
+ override
55
+ end
56
+
57
+ def mms_template_with_id
58
+ fail ArgumentError, 'content_length is needed for MMS Inline Template with ID' if content_length.nil?
59
+ fail ArgumentError, 'content_type is needed for MMS Inline Template with ID' if content_type.nil?
60
+ fail ArgumentError, 'url is needed for MMS Inline Template with ID' if url.nil?
61
+ fail ArgumentError, 'template_id is needed for MMS Inline Template with ID' if template_id.nil?
62
+
63
+ {"mms": {
64
+ "template": {
65
+ "template_id": template_id
66
+ },
67
+ "shorten_links": true,
68
+ "slides": [
69
+ {
70
+ "media": {
71
+ "url": url,
72
+ "content_type": content_type,
73
+ "content_length": content_length
74
+ }
75
+ }
76
+ ]
77
+ }
78
+ }
79
+ end
80
+
81
+ def mms_inline_template
82
+ fail ArgumentError, 'slide_1_text text is needed for MMS with inline template' if text.nil?
83
+
84
+ {"mms": {
85
+ "template": {
86
+ "fields": {
87
+ "subject": subject,
88
+ "fallback_text": fallback_text,
89
+ "slide_1_text": text
90
+ }
91
+ },
92
+ "slides": [
93
+ {
94
+ "media": {
95
+ "url": url,
96
+ "content_type": content_type,
97
+ "content_length": content_length
98
+ }
99
+ }
100
+ ]
101
+ }
102
+ }
103
+ end
104
+
105
+ end
106
+ end
107
+ end
@@ -5,37 +5,41 @@ module Urbanairship
5
5
  class OpenChannel
6
6
  include Urbanairship::Common
7
7
  include Urbanairship::Loggable
8
- attr_accessor :channel_id, :open_platform, :opt_in, :address,
9
- :tags, :identifiers
8
+ attr_accessor :channel_id,
9
+ :open_platform,
10
+ :opt_in,
11
+ :address,
12
+ :tags,
13
+ :identifiers,
14
+ :template_id,
15
+ :alert,
16
+ :extra,
17
+ :media_attachment,
18
+ :summary,
19
+ :title,
20
+ :template_id,
21
+ :fields,
22
+ :interactive,
23
+ :platform_alert
10
24
 
11
25
  def initialize(client: required('client'))
12
26
  @client = client
13
- @channel_id = nil
14
- @open_platform = nil
15
- @opt_in = nil
16
- @address = nil
17
- @tags = nil
18
- @identifiers = nil
19
27
  end
20
28
 
21
29
  def create()
22
- fail TypeError, 'address must be set to create open channel' unless @address.is_a? String
23
- fail TypeError, 'open_platform must be set to create open channel' unless @open_platform.is_a? String
24
- fail TypeError, 'opt_in must be boolean' unless [true, false].include? @opt_in
30
+ fail TypeError, 'address must be set to create open channel' unless address.is_a? String
31
+ fail TypeError, 'open_platform must be set to create open channel' unless open_platform.is_a? String
32
+ fail TypeError, 'opt_in must be boolean' unless [true, false].include? opt_in
25
33
 
26
34
  channel_data = {
27
35
  'type': 'open',
28
- 'open': {:open_platform_name => @open_platform},
29
- 'opt_in': @opt_in,
30
- 'address': @address
31
- }
36
+ 'open': {:open_platform_name => open_platform},
37
+ 'opt_in': opt_in,
38
+ 'address': address,
39
+ 'tags': tags
40
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
32
41
 
33
- if @tags
34
- channel_data['tags'] = @tags
35
- end
36
- if @identifiers
37
- channel_data[:open][:identifiers] = @identifiers
38
- end
42
+ set_identifiers
39
43
 
40
44
  body = {'channel': channel_data}
41
45
 
@@ -45,37 +49,29 @@ module Urbanairship
45
49
  body: JSON.dump(body),
46
50
  content_type: 'application/json'
47
51
  )
48
- logger.info("Registering open channel with address: #{@address}")
52
+ logger.info("Registering open channel with address: #{address}")
49
53
  response
50
54
  end
51
55
 
52
56
  def update(set_tags: required('set_tags'))
53
57
  fail ArgumentError, 'set_tags must be boolean' unless [true, false].include? set_tags
54
- fail ArgumentError, 'set_tags cannot be true when tags are not set' unless set_tags == true && @tags != nil
55
- fail TypeError, 'opt_in must be boolean' unless [true, false].include? @opt_in
56
- fail TypeError, 'address or channel_id must not be nil' unless @address.is_a? String || @channel_id.is_a?(String)
57
- fail TypeError, 'open_platform cannot be nil' unless @open_platform.is_a? String
58
- fail TypeErorr, 'address must not be nil if opt_in is true' unless @opt_in.is_a? TrueClass
58
+ fail ArgumentError, 'set_tags cannot be true when tags are not set' unless set_tags == true && tags != nil
59
+ fail TypeError, 'opt_in must be boolean' unless [true, false].include? opt_in
60
+ fail TypeError, 'address or channel_id must not be nil' unless address.is_a? String || channel_id.is_a?(String)
61
+ fail TypeError, 'open_platform cannot be nil' unless open_platform.is_a? String
62
+ fail TypeErorr, 'address must not be nil if opt_in is true' unless opt_in.is_a? TrueClass
59
63
 
60
64
  channel_data = {
61
65
  'type': 'open',
62
- 'open': {'open_platform_name': @open_platform},
63
- 'opt_in': @opt_in,
64
- 'set_tags': set_tags
65
- }
66
+ 'open': {'open_platform_name': open_platform},
67
+ 'opt_in': opt_in,
68
+ 'set_tags': set_tags,
69
+ 'channel_id': channel_id,
70
+ 'address': address,
71
+ 'tags': tags
72
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
66
73
 
67
- if @channel_id
68
- channel_data['channel_id'] = @channel_id
69
- end
70
- if @address
71
- channel_data['address'] = @address
72
- end
73
- if @tags
74
- channel_data['tags'] = @tags
75
- end
76
- if @identifiers
77
- channel_data['open']['identifiers'] = @identifiers
78
- end
74
+ set_identifiers
79
75
 
80
76
  body = {'channel': channel_data}
81
77
 
@@ -85,7 +81,7 @@ module Urbanairship
85
81
  body: JSON.dump(body),
86
82
  content_type: 'application/json'
87
83
  )
88
- logger.info("Updating open channel with address #{@address}")
84
+ logger.info("Updating open channel with address #{address}")
89
85
  response
90
86
  end
91
87
 
@@ -99,6 +95,55 @@ module Urbanairship
99
95
  logger.info("Looking up info on device token #{channel_id}")
100
96
  response
101
97
  end
98
+
99
+ def notification_with_template_id
100
+ fail TypeError, 'open_platform cannot be nil' if open_platform.nil?
101
+
102
+ if alert
103
+ payload = {
104
+ "open::#{open_platform}":{
105
+ 'template': {
106
+ 'template_id': template_id,
107
+ 'fields': {
108
+ 'alert': alert
109
+ }
110
+ }
111
+ }
112
+ }
113
+ else
114
+ payload = {
115
+ "open::#{open_platform}":{
116
+ 'template': {
117
+ 'template_id': template_id,
118
+ }
119
+ }
120
+ }
121
+ end
122
+
123
+ payload
124
+ end
125
+
126
+ def open_channel_override
127
+ fail TypeError, 'open_platform cannot be nil' if open_platform.nil?
128
+ payload = {
129
+ 'alert': platform_alert,
130
+ 'extra': extra,
131
+ 'media_attachment': media_attachment,
132
+ 'summary': summary,
133
+ 'title': title,
134
+ 'interactive': interactive
135
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
136
+
137
+ {'alert': alert,
138
+ "open::#{open_platform}": payload}
139
+ end
140
+
141
+ def set_identifiers
142
+ if identifiers
143
+ channel_data[:open][:identifiers] = identifiers
144
+ end
145
+ end
146
+
102
147
  end
103
148
  end
104
149
  end