Module: Claire::Client::Item

Included in:
Category, Stream, Video
Defined in:
lib/claire_client/item.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#partial=(value) ⇒ Object (writeonly)

Sets the attribute partial

Parameters:

  • value

    the value to set the attribute partial to.



5
6
7
# File 'lib/claire_client/item.rb', line 5

def partial=(value)
  @partial = value
end

Class Method Details

.childrenObject

Lists all avaliable Claire::Items



86
87
88
# File 'lib/claire_client/item.rb', line 86

def self.children
	@@children ||= []
end

.included(base) ⇒ Object

Extends the class methods above.



80
81
82
83
# File 'lib/claire_client/item.rb', line 80

def Item.included base
	base.extend ClassMethods
	(@@children ||= []) << base
end

Instance Method Details

#<=>(arg) ⇒ Object

comparison operator. Sorts by title.



50
51
52
53
54
55
# File 'lib/claire_client/item.rb', line 50

def <=> arg
	criteria = :title 
	return 1 if self.send(criteria) > arg.send(criteria)
	return 0 if self.send(criteria) == arg.send(criteria)
	return -1 if self.send(criteria) < arg.send(criteria) 				
end

#initialize(arg) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/claire_client/item.rb', line 7

def initialize arg
	if arg.is_a? String
		file = Claire::Client.get("#{self.class.base_url}/#{arg}")
if Claire::Client.format == 'xml'
	hash = Hashie::Mash.new(Hashie::Mash.from_xml(file))
else
	hash = Hashie::Mash.new(JSON.parse(file))
end
@partial = false
else 
hash = Hashie::Mash.new(arg) and @partial = true
				end
				
				# fetches items from rss, channel, or directly?
				if hash.rss
hash = hash.rss
hash = 	if hash.channel
					then	hash.channel
					else  hash.item
				end
				end
					
				hash.each do |k,v|

# skips paging and sub-items.
next if %w(item next previous).include? k

# sets an element's label										         										
v = v.label if v.respond_to?('merge') and not v.label.nil?

# properly fetches the items's link, in case there are more than one link
v = v.first if k == 'link' and v.respond_to? 'push'

class_eval "def #{k}; @#{k}; end" 
instance_variable_set "@#{k}", v
				end
				
				# a link is always required. 
				raise Claire::Error, "a link is always required" if @link.nil? or @link.empty?
			
end

#partial?Boolean

checks an item’s partial status

Returns:

  • (Boolean)


58
59
60
# File 'lib/claire_client/item.rb', line 58

def partial?
	@partial
end