urbanairship 5.6.1 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +30 -0
  3. data/README.rst +91 -34
  4. data/docs/ab_tests.rst +162 -0
  5. data/docs/attributes.rst +52 -0
  6. data/docs/automations.rst +212 -0
  7. data/docs/index.rst +3 -0
  8. data/docs/push.rst +24 -0
  9. data/docs/sms.rst +19 -0
  10. data/docs/static_lists.rst +2 -2
  11. data/lib/urbanairship.rb +12 -0
  12. data/lib/urbanairship/ab_tests/ab_test.rb +88 -0
  13. data/lib/urbanairship/ab_tests/experiment.rb +45 -0
  14. data/lib/urbanairship/ab_tests/variant.rb +34 -0
  15. data/lib/urbanairship/automations/automation.rb +105 -0
  16. data/lib/urbanairship/automations/pipeline.rb +52 -0
  17. data/lib/urbanairship/client.rb +32 -10
  18. data/lib/urbanairship/common.rb +108 -42
  19. data/lib/urbanairship/configuration.rb +2 -1
  20. data/lib/urbanairship/custom_events/custom_event.rb +60 -0
  21. data/lib/urbanairship/custom_events/payload.rb +89 -0
  22. data/lib/urbanairship/devices/attribute.rb +54 -0
  23. data/lib/urbanairship/devices/channel_tags.rb +2 -2
  24. data/lib/urbanairship/devices/channel_uninstall.rb +10 -10
  25. data/lib/urbanairship/devices/create_and_send.rb +4 -4
  26. data/lib/urbanairship/devices/devicelist.rb +28 -7
  27. data/lib/urbanairship/devices/email.rb +5 -5
  28. data/lib/urbanairship/devices/named_user.rb +6 -6
  29. data/lib/urbanairship/devices/open_channel.rb +22 -23
  30. data/lib/urbanairship/devices/segment.rb +6 -8
  31. data/lib/urbanairship/devices/sms.rb +40 -9
  32. data/lib/urbanairship/devices/static_lists.rb +12 -12
  33. data/lib/urbanairship/push/location.rb +18 -18
  34. data/lib/urbanairship/push/push.rb +23 -13
  35. data/lib/urbanairship/push/schedule.rb +9 -0
  36. data/lib/urbanairship/reports/response_statistics.rb +42 -31
  37. data/lib/urbanairship/version.rb +1 -1
  38. metadata +16 -5
@@ -13,22 +13,30 @@ module Urbanairship
13
13
  #
14
14
  # @param [Object] key Application Key
15
15
  # @param [Object] secret Application Secret
16
+ # @param [String] server Airship server to use ("go.airship.eu" or "go.urbanairship.com").
17
+ # Used only when the request is sent with a "path", not an "url".
18
+ # @param [String] token Application Auth Token (for custom events endpoint)
16
19
  # @return [Object] Client
17
- def initialize(key: required('key'), secret: required('secret'))
20
+ def initialize(key: required('key'), secret: required('secret'),
21
+ server: Urbanairship.configuration.server, token: nil)
18
22
  @key = key
19
23
  @secret = secret
24
+ @server = server
25
+ @token = token
20
26
  end
21
27
 
22
28
  # Send a request to Airship's API
23
29
  #
24
30
  # @param [Object] method HTTP Method
25
31
  # @param [Object] body Request Body
32
+ # @param [Object] path Request path
26
33
  # @param [Object] url Request URL
27
34
  # @param [Object] content_type Content-Type
28
- # @param [Object] version API Version
35
+ # @param [Object] encoding Encoding
36
+ # @param [Symbol] auth_type (:basic|:bearer)
29
37
  # @return [Object] Push Response
30
- def send_request(method: required('method'), url: required('url'), body: nil,
31
- content_type: nil, encoding: nil)
38
+ def send_request(method: required('method'), path: nil, url: nil, body: nil,
39
+ content_type: nil, encoding: nil, auth_type: :basic)
32
40
  req_type = case method
33
41
  when 'GET'
34
42
  :get
@@ -42,13 +50,22 @@ module Urbanairship
42
50
  fail 'Method was not "GET" "POST" "PUT" or "DELETE"'
43
51
  end
44
52
 
53
+ raise ArgumentError.new("path and url can't be both nil") if path.nil? && url.nil?
54
+
45
55
  headers = {'User-agent' => 'UARubyLib/' + Urbanairship::VERSION}
46
56
  headers['Accept'] = 'application/vnd.urbanairship+json; version=3'
47
57
  headers['Content-type'] = content_type unless content_type.nil?
48
58
  headers['Content-Encoding'] = encoding unless encoding.nil?
49
59
 
50
- debug = "Making #{method} request to #{url}.\n"+
51
- "\tHeaders:\n"
60
+ if auth_type == :bearer
61
+ raise ArgumentError.new('token must be provided as argument if auth_type=bearer') if @token.nil?
62
+ headers['X-UA-Appkey'] = @key
63
+ headers['Authorization'] = "Bearer #{@token}"
64
+ end
65
+
66
+ url = "https://#{@server}/api#{path}" unless path.nil?
67
+
68
+ debug = "Making #{method} request to #{url}.\n"+ "\tHeaders:\n"
52
69
  debug += "\t\tcontent-type: #{content_type}\n" unless content_type.nil?
53
70
  debug += "\t\tcontent-encoding: gzip\n" unless encoding.nil?
54
71
  debug += "\t\taccept: application/vnd.urbanairship+json; version=3\n"
@@ -56,15 +73,20 @@ module Urbanairship
56
73
 
57
74
  logger.debug(debug)
58
75
 
59
- response = RestClient::Request.execute(
76
+ params = {
60
77
  method: method,
61
78
  url: url,
62
79
  headers: headers,
63
- user: @key,
64
- password: @secret,
65
80
  payload: body,
66
81
  timeout: Urbanairship.configuration.timeout
67
- )
82
+ }
83
+
84
+ if auth_type == :basic
85
+ params[:user] = @key
86
+ params[:password] = @secret
87
+ end
88
+
89
+ response = RestClient::Request.execute(params)
68
90
 
69
91
  logger.debug("Received #{response.code} response. Headers:\n\t#{response.headers}\nBody:\n\t#{response.body}")
70
92
  Response.check_code(response.code, response)
@@ -4,22 +4,65 @@ require 'urbanairship/loggable'
4
4
  module Urbanairship
5
5
  # Features mixed in to all classes
6
6
  module Common
7
- SERVER = 'go.urbanairship.com'
8
- BASE_URL = 'https://go.urbanairship.com/api'
9
- CHANNEL_URL = BASE_URL + '/channels/'
10
- OPEN_CHANNEL_URL = BASE_URL + '/channels/open/'
11
- DEVICE_TOKEN_URL = BASE_URL + '/device_tokens/'
12
- APID_URL = BASE_URL + '/apids/'
13
- PUSH_URL = BASE_URL + '/push/'
14
- SCHEDULES_URL = BASE_URL + '/schedules/'
15
- SEGMENTS_URL = BASE_URL + '/segments/'
16
- NAMED_USER_URL = BASE_URL + '/named_users/'
17
- REPORTS_URL = BASE_URL + '/reports/'
18
- LISTS_URL = BASE_URL + '/lists/'
19
- PIPELINES_URL = BASE_URL + '/pipelines/'
20
- FEEDS_URL = BASE_URL + '/feeds/'
21
- LOCATION_URL = BASE_URL + '/location/'
22
- CREATE_AND_SEND_URL = BASE_URL + '/create-and-send/'
7
+ def apid_path(path='')
8
+ "/apids/#{path}"
9
+ end
10
+
11
+ def channel_path(path='')
12
+ "/channels/#{path}"
13
+ end
14
+
15
+ def create_and_send_path(path='')
16
+ "/create-and-send/#{path}"
17
+ end
18
+
19
+ def custom_events_path(path='')
20
+ "/custom-events/#{path}"
21
+ end
22
+
23
+ def device_token_path(path='')
24
+ "/device_tokens/#{path}"
25
+ end
26
+
27
+ def experiments_path(path='')
28
+ "/experiments/#{path}"
29
+ end
30
+
31
+ def lists_path(path='')
32
+ "/lists/#{path}"
33
+ end
34
+
35
+ def location_path(path='')
36
+ "/location/#{path}"
37
+ end
38
+
39
+ def named_users_path(path='')
40
+ "/named_users/#{path}"
41
+ end
42
+
43
+ def open_channel_path(path='')
44
+ "/open/#{path}"
45
+ end
46
+
47
+ def pipelines_path(path='')
48
+ "/pipelines/#{path}"
49
+ end
50
+
51
+ def push_path(path='')
52
+ "/push/#{path}"
53
+ end
54
+
55
+ def reports_path(path='')
56
+ "/reports/#{path}"
57
+ end
58
+
59
+ def schedules_path(path='')
60
+ "/schedules/#{path}"
61
+ end
62
+
63
+ def segments_path(path='')
64
+ "/segments/#{path}"
65
+ end
23
66
 
24
67
  # Helper method for required keyword args in Ruby 2.0 that is compatible with 2.1+
25
68
  # @example
@@ -114,36 +157,18 @@ module Urbanairship
114
157
 
115
158
  def initialize(client: required('client'))
116
159
  @client = client
117
- @next_page = nil
118
- @data_list = nil
119
- @data_attribute = nil
120
160
  @count = 0
121
- end
122
-
123
- def load_page
124
- return false unless @next_page
125
- response = @client.send_request(
126
- method: 'GET',
127
- url: @next_page
128
- )
129
- logger.info("Retrieving data from: #{@next_page}")
130
- check_next_page = response['body']['next_page']
131
- if check_next_page != @next_page
132
- @next_page = check_next_page
133
- elsif check_next_page
134
- # if check_page = next_page, we have repeats in the response.
135
- # and we don't want to load them
136
- return false
137
- else
138
- @next_page = nil
139
- end
140
- @data_list = response['body'][@data_attribute]
141
- true
161
+ @data_attribute = nil
162
+ @data_list = nil
163
+ @next_page_path = nil
164
+ @next_page_url = nil
142
165
  end
143
166
 
144
167
  def each
145
- while load_page
146
- @data_list.each do | value |
168
+ while @next_page_path || @next_page_url
169
+ load_page
170
+
171
+ @data_list.each do |value|
147
172
  @count += 1
148
173
  yield value
149
174
  end
@@ -153,6 +178,47 @@ module Urbanairship
153
178
  def count
154
179
  @count
155
180
  end
181
+
182
+ private
183
+
184
+ def load_page
185
+ logger.info("Retrieving data from: #{@next_page_url || @next_page_path}")
186
+ params = {
187
+ method: 'GET',
188
+ path: @next_page_path,
189
+ url: @next_page_url
190
+ }.select { |k, v| !v.nil? }
191
+ response = @client.send_request(params)
192
+
193
+ @data_list = get_new_data(response)
194
+ @next_page_url = get_next_page_url(response)
195
+ @next_page_path = nil
196
+ end
197
+
198
+ def extract_next_page_url(response)
199
+ response['body']['next_page']
200
+ end
201
+
202
+ def get_new_data(response)
203
+ potential_next_page_url = extract_next_page_url(response)
204
+
205
+ # if potential_next_page_url is the same as the current page, we have
206
+ # repeats in the response and we don't want to load them
207
+ return [] if potential_next_page_url && get_next_page_url(response).nil?
208
+
209
+ response['body'][@data_attribute]
210
+ end
211
+
212
+ def get_next_page_url(response)
213
+ potential_next_page_url = extract_next_page_url(response)
214
+ return nil if potential_next_page_url.nil?
215
+
216
+ # if potential_next_page_url is the same as the current page, we have
217
+ # repeats in the response and we don't want to check the next pages
218
+ return potential_next_page_url if @next_page_url && potential_next_page_url != @next_page_url
219
+ return potential_next_page_url if @next_page_path && !potential_next_page_url.end_with?(@next_page_path)
220
+ nil
221
+ end
156
222
  end
157
223
  end
158
224
  end
@@ -1,8 +1,9 @@
1
1
  module Urbanairship
2
2
  class Configuration
3
- attr_accessor :custom_logger, :log_path, :log_level, :timeout
3
+ attr_accessor :custom_logger, :log_path, :log_level, :server, :timeout
4
4
 
5
5
  def initialize
6
+ @server = 'go.urbanairship.com'
6
7
  @custom_logger = nil
7
8
  @log_path = nil
8
9
  @log_level = Logger::INFO
@@ -0,0 +1,60 @@
1
+ require 'uri'
2
+ require 'urbanairship'
3
+ require 'urbanairship/common'
4
+ require 'urbanairship/loggable'
5
+
6
+ module Urbanairship
7
+ module CustomEvents
8
+ class CustomEvent
9
+ include Urbanairship::Common
10
+ include Urbanairship::Loggable
11
+
12
+ attr_accessor :events
13
+
14
+ def initialize(client: required('client'))
15
+ @client = client
16
+ end
17
+
18
+ def create
19
+ fail ArgumentError, 'events must be an array of custom events' unless events.is_a?(Array)
20
+
21
+ response = @client.send_request(
22
+ auth_type: :bearer,
23
+ body: JSON.dump(events),
24
+ content_type: 'application/json',
25
+ method: 'POST',
26
+ path: custom_events_path
27
+ )
28
+ cer = CustomEventResponse.new(body: response['body'], code: response['code'])
29
+ logger.info { cer.format }
30
+
31
+ cer
32
+ end
33
+ end
34
+
35
+ # Response to a successful custom event creation.
36
+ class CustomEventResponse
37
+ attr_reader :ok, :operation_id, :payload, :status_code
38
+
39
+ def initialize(body: nil, code: nil)
40
+ @payload = (body.nil? || body.empty?) ? {} : body
41
+ @ok = payload['ok']
42
+ @operation_id = payload['operationId']
43
+ @status_code = code
44
+ end
45
+
46
+ # String Formatting of the CustomEventResponse
47
+ #
48
+ # @return [Object] String Formatted CustomEventResponse
49
+ def format
50
+ "Received [#{status_code}] response code.\nBody:\n#{formatted_body}"
51
+ end
52
+
53
+ def formatted_body
54
+ payload
55
+ .map { |key, value| "#{key}:\t#{value.to_s || 'None'}" }
56
+ .join("\n")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,89 @@
1
+ require 'urbanairship'
2
+ require 'urbanairship/common'
3
+
4
+ module Urbanairship
5
+ module CustomEvents
6
+ module Payload
7
+ include Urbanairship::Common
8
+
9
+ def custom_events(
10
+ body: required('body'),
11
+ occurred: required('occurred'),
12
+ user: required('user')
13
+ )
14
+ compact_helper({
15
+ body: body,
16
+ occurred: format_timestamp(occurred),
17
+ user: user
18
+ })
19
+ end
20
+
21
+ # Body specific portion of CustomEvent Object
22
+ def custom_events_body(
23
+ interaction_id: nil, interaction_type: nil, name: required('name'),
24
+ properties: nil, session_id: nil, transaction: nil, value: nil
25
+ )
26
+
27
+ validates_name_format(name)
28
+ validates_value_format(value)
29
+
30
+ compact_helper({
31
+ interaction_id: interaction_id,
32
+ interaction_type: interaction_type,
33
+ name: name,
34
+ properties: properties,
35
+ session_id: session_id,
36
+ transaction: transaction,
37
+ value: value
38
+ })
39
+ end
40
+
41
+ # User specific portion of CustomEvent Object
42
+ def custom_events_user(
43
+ amazon_channel: nil, android_channel: nil, channel: nil,
44
+ ios_channel: nil, named_user_id: nil, web_channel: nil
45
+ )
46
+ res = compact_helper({
47
+ amazon_channel: amazon_channel,
48
+ android_channel: android_channel,
49
+ channel: channel,
50
+ ios_channel: ios_channel,
51
+ named_user_id: named_user_id,
52
+ web_channel: web_channel,
53
+ })
54
+
55
+ fail ArgumentError, 'at least one user identifier must be defined' if res.empty?
56
+
57
+ res
58
+ end
59
+
60
+
61
+ # Formatters
62
+ # ------------------------------------------------------------------------
63
+
64
+ def format_timestamp(timestamp)
65
+ return timestamp if timestamp.is_a?(String)
66
+
67
+ timestamp.strftime('%Y-%m-%dT%H:%M:%S')
68
+ end
69
+
70
+
71
+ # Validators
72
+ # ------------------------------------------------------------------------
73
+
74
+ NAME_REGEX = /^[a-z0-9_\-]+$/
75
+ def validates_name_format(name)
76
+ return if name =~ NAME_REGEX
77
+
78
+ fail ArgumentError, 'invalid "name": it must follows this pattern /^[a-z0-9_\-]+$/'
79
+ end
80
+
81
+ def validates_value_format(value)
82
+ return if value.nil?
83
+ return if value.is_a?(Numeric)
84
+
85
+ fail ArgumentError, 'invalid "value": must be a number'
86
+ end
87
+ end
88
+ end
89
+ end
@@ -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