class Newznab::Api::Item

Class representing a single Newznab item

Attributes

description[R]
guid[R]
metadata[R]
pub_date[R]
title[R]

Public Class Methods

new(args) click to toggle source

@param args [Hash<String, Object>] Item hash from response @return [Newznab::Item] @since 0.1.0

# File lib/newznab/api/item.rb, line 13
def initialize(args)

  @raw_resp = args
  @metadata = {}

  args.each_pair do |k, v|
    case k
      when 'title'
        @title = v
      when 'guid'
        @guid = v
      when 'link'
        @link = v
      when 'pubDate'
        @pub_date = Date.parse(v)
      when 'description'
        @description = v
      when 'enclosure'
        @_attributes = v['@attributes']
      when 'attr'
        @metadata = _parse_attr(v)
      else
        # Do nothing
    end
  end
end

Private Instance Methods

_parse_attr(attrs) click to toggle source

@param attrs [Array<Hash<Hash<String, String>>>] Newznab attr array response @return [Hash<String, Array<String>>] @since 0.1.0

# File lib/newznab/api/item.rb, line 46
def _parse_attr(attrs)

  metadata = {}
  attrs.each do |attr|
    name = attr['@attributes']['name']
    value = attr['@attributes']['value']

    if metadata.has_key? name
      metadata[name].push value
    else
      metadata[name] = [value]
    end
  end
  new_meta = {}
  metadata.each { |k, v| new_meta[k] = v.count.eql?(1) ? v.first : v }
  new_meta
end
method_missing(id, *args) click to toggle source

@since 0.1.0

Calls superclass method
# File lib/newznab/api/item.rb, line 65
def method_missing(id, *args)
  begin
    if @_attributes.has_key? id.to_s
      @_attributes[id.to_s]
    elsif @metadata.has_key? id.to_s
      @metadata[id.to_s]
    else
      super
    end
  end
end
respond_to_missing?(id, *args) click to toggle source

@since 0.1.0

Calls superclass method
# File lib/newznab/api/item.rb, line 78
def respond_to_missing?(id, *args)
  begin
    if @_attributes.has_key? id.to_s
      true
    elsif @metadata.has_key? id.to_s
      true
    else
      super
    end
  end
end