class ZanoxPublisher::Category

General category class

Legacy name category is for program and admedium

NOTE: Later create enumerable class Categories to allow .include? on result from API

@param [Integer] id The identifer of the category @param [String] name The name of the category

Attributes

id[R]
name[R]

Public Class Methods

fetch(data = nil) click to toggle source

Fetch all categories from Zanox API Response

@param data [Array] the value of the 'categories' element

@return [Array<Category>, nil]

# File lib/zanox_publisher/category.rb, line 17
def fetch(data = nil)
  # To support API of picking categories of hash with [] notation
  return nil if data.nil?
  categories = data.first
  # In case the array is empty first will return a nil
  return nil if categories.nil?
  categories = categories['category']
  # In case the second level is missing the hash [] notation will nil
  return nil if categories.nil?
  # In case just one category is in the list it must be converted back to an array
  categories = [categories] if categories.is_a? Hash

  # We got the array of category hash's
  retval = []

  categories.each do |category|
    retval << self.new(category)
  end

  retval
end
new(data = {}) click to toggle source
# File lib/zanox_publisher/category.rb, line 41
def initialize(data = {})
  @id   = data.fetch('@id').to_i
  @name = data.fetch('$', nil)
end

Public Instance Methods

to_i() click to toggle source

Returns the category ID as integer representation

@return [Integer]

# File lib/zanox_publisher/category.rb, line 53
def to_i
  @id
end
to_s() click to toggle source
# File lib/zanox_publisher/category.rb, line 46
def to_s
  @name
end