Class: Urbanairship::Devices::Segment

Inherits:
Object
  • Object
show all
Includes:
Common, Loggable
Defined in:
lib/urbanairship/devices/segment.rb

Constant Summary

Constants included from Common

Common::CONTENT_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

create_logger, logger, #logger

Methods included from Common

#apid_path, #channel_path, #compact_helper, #create_and_send_path, #custom_events_path, #device_token_path, #experiments_path, #lists_path, #named_users_path, #open_channel_path, #pipelines_path, #push_path, #reports_path, #required, #schedules_path, #segments_path, #tag_lists_path, #try_helper

Constructor Details

#initialize(client: required('client')) ⇒ Segment

Returns a new instance of Segment.



13
14
15
# File 'lib/urbanairship/devices/segment.rb', line 13

def initialize(client: required('client'))
  @client = client
end

Instance Attribute Details

#criteriaObject

Returns the value of attribute criteria.



10
11
12
# File 'lib/urbanairship/devices/segment.rb', line 10

def criteria
  @criteria
end

#display_nameObject

Returns the value of attribute display_name.



10
11
12
# File 'lib/urbanairship/devices/segment.rb', line 10

def display_name
  @display_name
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/urbanairship/devices/segment.rb', line 11

def id
  @id
end

Instance Method Details

#createObject

Build a Segment from the display_name and criteria attributes

Parameters:

  • client (Object)

    The Client

Returns:

  • (Object)

    response HTTP response



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/urbanairship/devices/segment.rb', line 21

def create
  fail ArgumentError,
    'Both display_name and criteria must be set to a value' if display_name.nil? or criteria.nil?
  payload = {
    'display_name': display_name,
    'criteria': criteria
  }
  response = @client.send_request(
    method: 'POST',
    body: JSON.dump(payload),
    path: segments_path,
    content_type: 'application/json'
  )
  logger.info { "Successful segment creation: #{@display_name}" }
  seg_url = response['headers'][:location]
  @id = seg_url.split('/')[-1]
  response
end

#deleteObject

Delete a segment

@ returns [Object] response HTTP response



82
83
84
85
86
87
88
89
90
91
# File 'lib/urbanairship/devices/segment.rb', line 82

def delete
  fail ArgumentError, 'id cannot be nil' if id.nil?

  response = @client.send_request(
    method: 'DELETE',
    path: segments_path(id)
  )
  logger.info { "Successful segment deletion: #{@display_name}" }
  response
end

#from_id(id: required('id')) ⇒ Object

Build a Segment from the display_name and criteria attributes

Parameters:

  • id (Object) (defaults to: required('id'))

    The id of the segment being looked up



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/urbanairship/devices/segment.rb', line 43

def from_id(id: required('id'))
  fail ArgumentError,
    'id must be set to a valid string' if id.nil?
  response = @client.send_request(
    method: 'GET',
    path: segments_path(id)
  )
  logger.info("Retrieved segment information for #{id}")
  @id = id
  @criteria = response['body']['criteria']
  @display_name = response['body']['display_name']
  response
end

#updateObject

Update a segment with new criteria/display_name

@ returns [Object] response HTTP response



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/urbanairship/devices/segment.rb', line 60

def update
  fail ArgumentError,
    'id cannot be nil' if @id.nil?
  fail ArgumentError,
    'Either display_name or criteria must be set to a value' if display_name.nil? and criteria.nil?

  data = {}
  data['display_name'] = display_name
  data['criteria'] = criteria
  response = @client.send_request(
    method: 'PUT',
    body: JSON.dump(data),
    path: segments_path(@id),
    content_type: 'application/json'
  )
  logger.info { "Successful segment update: #{@display_name}" }
  response
end