class Urbanairship::Devices::Segment

Attributes

criteria[RW]
display_name[RW]
id[R]

Public Class Methods

new(client: required('client')) click to toggle source
# File lib/urbanairship/devices/segment.rb, line 13
def initialize(client: required('client'))
  @client = client
end

Public Instance Methods

create() click to toggle source

Build a Segment from the display_name and criteria attributes

@param [Object] client The Client @return [Object] response HTTP response

# 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
delete() click to toggle source

Delete a segment

@ returns [Object] response HTTP response

# 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')) click to toggle source

Build a Segment from the display_name and criteria attributes

@param [Object] id The id of the segment being looked up

# 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
update() click to toggle source

Update a segment with new criteria/display_name

@ returns [Object] response HTTP response

# 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