urbanairship 5.5.1 → 5.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require 'urbanairship'
2
+
3
+ module Urbanairship
4
+ module Automations
5
+ class Pipeline
6
+ include Urbanairship::Common
7
+ include Urbanairship::Loggable
8
+ attr_accessor :activation_time,
9
+ :cancellation_trigger,
10
+ :condition,
11
+ :constraint,
12
+ :creation_time,
13
+ :deactivation_time,
14
+ :historical_trigger,
15
+ :immediate_trigger,
16
+ :last_modified_time,
17
+ :name,
18
+ :status,
19
+ :timing,
20
+ :url,
21
+ :enabled,
22
+ :outcome
23
+
24
+ def initialize(client: required('client'))
25
+ @client = client
26
+ end
27
+
28
+ def payload
29
+ fail ArgumentError, 'enabled must be set to create pipeline payload' if @enabled.nil?
30
+ fail ArgumentError, 'outcome must be set to create pipeline payload' if @outcome.nil?
31
+ {
32
+ activation_time: activation_time,
33
+ cancellation_trigger: cancellation_trigger,
34
+ condition: condition,
35
+ constraint: constraint,
36
+ creation_time: creation_time,
37
+ deactivation_time: deactivation_time,
38
+ enabled: enabled,
39
+ historical_trigger: historical_trigger,
40
+ immediate_trigger: immediate_trigger,
41
+ last_modified_time: last_modified_time,
42
+ name: name,
43
+ outcome: outcome,
44
+ status: status,
45
+ timing: timing,
46
+ url: url
47
+ }.delete_if {|key, value| value.nil?} #this removes the nil key value pairs
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -13,10 +13,12 @@ module Urbanairship
13
13
  #
14
14
  # @param [Object] key Application Key
15
15
  # @param [Object] secret Application Secret
16
+ # @param [String] token Application Auth Token (for custom events endpoint)
16
17
  # @return [Object] Client
17
- def initialize(key: required('key'), secret: required('secret'))
18
+ def initialize(key: required('key'), secret: required('secret'), token: nil)
18
19
  @key = key
19
20
  @secret = secret
21
+ @token = token
20
22
  end
21
23
 
22
24
  # Send a request to Airship's API
@@ -25,10 +27,11 @@ module Urbanairship
25
27
  # @param [Object] body Request Body
26
28
  # @param [Object] url Request URL
27
29
  # @param [Object] content_type Content-Type
28
- # @param [Object] version API Version
30
+ # @param [Object] encoding Encoding
31
+ # @param [Symbol] auth_type (:basic|:bearer)
29
32
  # @return [Object] Push Response
30
33
  def send_request(method: required('method'), url: required('url'), body: nil,
31
- content_type: nil, encoding: nil)
34
+ content_type: nil, encoding: nil, auth_type: :basic)
32
35
  req_type = case method
33
36
  when 'GET'
34
37
  :get
@@ -46,9 +49,14 @@ module Urbanairship
46
49
  headers['Accept'] = 'application/vnd.urbanairship+json; version=3'
47
50
  headers['Content-type'] = content_type unless content_type.nil?
48
51
  headers['Content-Encoding'] = encoding unless encoding.nil?
52
+
53
+ if auth_type == :bearer
54
+ raise ArgumentError.new('token must be provided as argument if auth_type=bearer') if @token.nil?
55
+ headers['X-UA-Appkey'] = @key
56
+ headers['Authorization'] = "Bearer #{@token}"
57
+ end
49
58
 
50
- debug = "Making #{method} request to #{url}.\n"+
51
- "\tHeaders:\n"
59
+ debug = "Making #{method} request to #{url}.\n"+ "\tHeaders:\n"
52
60
  debug += "\t\tcontent-type: #{content_type}\n" unless content_type.nil?
53
61
  debug += "\t\tcontent-encoding: gzip\n" unless encoding.nil?
54
62
  debug += "\t\taccept: application/vnd.urbanairship+json; version=3\n"
@@ -56,15 +64,20 @@ module Urbanairship
56
64
 
57
65
  logger.debug(debug)
58
66
 
59
- response = RestClient::Request.execute(
67
+ params = {
60
68
  method: method,
61
69
  url: url,
62
70
  headers: headers,
63
- user: @key,
64
- password: @secret,
65
71
  payload: body,
66
72
  timeout: Urbanairship.configuration.timeout
67
- )
73
+ }
74
+
75
+ if auth_type == :basic
76
+ params[:user] = @key
77
+ params[:password] = @secret
78
+ end
79
+
80
+ response = RestClient::Request.execute(params)
68
81
 
69
82
  logger.debug("Received #{response.code} response. Headers:\n\t#{response.headers}\nBody:\n\t#{response.body}")
70
83
  Response.check_code(response.code, response)
@@ -20,6 +20,7 @@ module Urbanairship
20
20
  FEEDS_URL = BASE_URL + '/feeds/'
21
21
  LOCATION_URL = BASE_URL + '/location/'
22
22
  CREATE_AND_SEND_URL = BASE_URL + '/create-and-send/'
23
+ EXPERIMENTS_URL = BASE_URL + '/experiments/'
23
24
 
24
25
  # Helper method for required keyword args in Ruby 2.0 that is compatible with 2.1+
25
26
  # @example
@@ -0,0 +1,54 @@
1
+ require 'urbanairship'
2
+
3
+ module Urbanairship
4
+ module Devices
5
+ class Attribute
6
+ include Urbanairship::Common
7
+ include Urbanairship::Loggable
8
+ attr_accessor :attribute,
9
+ :operator,
10
+ :precision,
11
+ :value
12
+
13
+ def initialize(client: required('client'))
14
+ @client = client
15
+ end
16
+
17
+ def payload
18
+ if precision
19
+ date_attribute
20
+ elsif value.is_a? String
21
+ text_attribute
22
+ elsif value.is_a? Integer
23
+ number_attribute
24
+ end
25
+ end
26
+
27
+ def number_attribute
28
+ {
29
+ 'attribute': attribute,
30
+ 'operator': operator,
31
+ 'value': value
32
+ }
33
+ end
34
+
35
+ def text_attribute
36
+ {
37
+ 'attribute': attribute,
38
+ 'operator': operator,
39
+ 'value': value
40
+ }
41
+ end
42
+
43
+ def date_attribute
44
+ {
45
+ 'attribute': attribute,
46
+ 'operator': operator,
47
+ 'precision': precision,
48
+ 'value': value
49
+ }
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -57,7 +57,7 @@ module Urbanairship
57
57
  content_type: 'application/json'
58
58
  )
59
59
 
60
- logger.info { "Successfully unintalled open channel with address: #{address}"}
60
+ logger.info { "Successfully uninstalled open channel with address: #{address}"}
61
61
  response
62
62
  end
63
63
  end
@@ -15,12 +15,6 @@ module Urbanairship
15
15
 
16
16
  def initialize(client: required('client'))
17
17
  @client = client
18
- @addresses = nil
19
- @device_types = nil
20
- @notification = nil
21
- @campaigns = nil
22
- @name = nil
23
- @scheduled_time = nil
24
18
  end
25
19
 
26
20
  def validate_address
@@ -40,17 +34,17 @@ module Urbanairship
40
34
 
41
35
  full_payload = {
42
36
  'audience': {
43
- 'create_and_send': @addresses
37
+ 'create_and_send': addresses
44
38
  },
45
- 'device_types': @device_types,
46
- 'notification': @notification,
39
+ 'device_types': device_types,
40
+ 'notification': notification,
47
41
  }
48
42
 
49
- if @campaigns
50
- campaign_object = {'categories': @campaigns}
43
+ if campaigns
44
+ campaign_object = {'categories': campaigns}
51
45
  full_payload[:campaigns] = campaign_object
52
46
  end
53
-
47
+
54
48
  full_payload
55
49
  end
56
50
 
@@ -81,9 +75,9 @@ module Urbanairship
81
75
 
82
76
  scheduled_payload = {
83
77
  "schedule": {
84
- "scheduled_time": @scheduled_time
78
+ "scheduled_time": scheduled_time
85
79
  },
86
- "name": @name,
80
+ "name": name,
87
81
  "push": payload
88
82
  }
89
83
 
@@ -7,6 +7,8 @@ module Urbanairship
7
7
  include Urbanairship::Common
8
8
  include Urbanairship::Loggable
9
9
  attr_writer :client
10
+ attr_accessor :audience,
11
+ :attributes
10
12
 
11
13
  def initialize(client: required('client'))
12
14
  @client = client
@@ -20,6 +22,25 @@ module Urbanairship
20
22
  logger.info("Retrieved channel information for #{uuid}")
21
23
  response['body']['channel']
22
24
  end
25
+
26
+ def payload
27
+ {
28
+ 'audience': audience,
29
+ 'attributes': [
30
+ attributes
31
+ ]
32
+ }
33
+ end
34
+
35
+ def set_attributes
36
+ response = @client.send_request(
37
+ method: 'POST',
38
+ body: JSON.dump(payload),
39
+ url: CHANNEL_URL + 'attributes',
40
+ content_type: 'application/json'
41
+ )
42
+ response
43
+ end
23
44
  end
24
45
 
25
46
  class ChannelList < Urbanairship::Common::PageIterator
@@ -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
  )
@@ -16,21 +16,12 @@ module Urbanairship
16
16
  :sender_name,
17
17
  :subject,
18
18
  :template_id,
19
- :variable_details
19
+ :variable_details,
20
+ :click_tracking,
21
+ :open_tracking
20
22
 
21
23
  def initialize(client: required('client'))
22
24
  @client = client
23
- @bcc = nil
24
- @bypass_opt_in_level = nil
25
- @html_body = nil
26
- @message_type = nil
27
- @plaintext_body = nil
28
- @reply_to = nil
29
- @sender_address = nil
30
- @sender_name = nil
31
- @subject = nil
32
- @template_id = nil
33
- @variable_details = nil
34
25
  end
35
26
 
36
27
  def email_override
@@ -41,22 +32,21 @@ module Urbanairship
41
32
  fail ArgumentError, 'sender_name is needed for email override' if @sender_name.nil?
42
33
  fail ArgumentError, 'subject is needed for email override' if @subject.nil?
43
34
 
44
- override = {'email': {
45
- 'bypass_opt_in_level': @bypass_opt_in_level,
46
- 'html_body': @html_body,
47
- 'message_type': @message_type,
48
- 'plaintext_body': @plaintext_body,
49
- 'reply_to': @reply_to,
50
- 'sender_address': @sender_address,
51
- 'sender_name': @sender_name,
52
- 'subject': @subject
53
- }}
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
54
48
 
55
- if @bcc
56
- override[:email][:bcc] = @bcc
57
- end
58
-
59
- override
49
+ {'email': override}
60
50
  end
61
51
 
62
52
  def email_with_inline_template
@@ -65,33 +55,36 @@ module Urbanairship
65
55
  fail ArgumentError, 'sender_address is needed for email with inline template' if @sender_address.nil?
66
56
  fail ArgumentError, 'sender_name is needed for email with inline template' if @sender_name.nil?
67
57
 
68
- inline_template = {'email': {
69
- 'message_type': @message_type,
70
- 'reply_to': @reply_to,
71
- 'sender_address': @sender_address,
72
- 'sender_name': @sender_name,
73
- 'template': {}
74
- }
75
- }
76
-
77
- if @subject and @plaintext_body
78
- fields_object = {'plaintext_body': @plaintext_body, 'subject': @subject}
79
- inline_template[:email][:template][:fields] = fields_object
80
- end
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
81
68
 
82
- if @bcc
83
- inline_template[:email][:bcc] = @bcc
84
- end
69
+ {'email': inline_template}
70
+ end
85
71
 
86
- if @variable_details
87
- inline_template[:email][:template][:variable_details] = @variable_details
88
- end
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
89
80
 
90
- if @template_id
91
- inline_template[:email][:template][:template_id] = @template_id
81
+ def define_fields
82
+ if subject && plaintext_body
83
+ {
84
+ subject: subject,
85
+ plaintext_body: plaintext_body
86
+ }
92
87
  end
93
-
94
- inline_template
95
88
  end
96
89
 
97
90
  end