Class: BlogMarks::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/blogmarks/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Tag

Create a tag from params

params hash can contains any of the object vars, but :title is mandatory


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/blogmarks/tag.rb', line 53

def initialize( params={} )
  @id             = params[:id]             || nil
  @title          = params[:title]          || raise( ArgumentError, ":title can't be empty")
  @link_alternate = params[:link_alternate] || nil
  @link_image     = params[:link_image]     || nil
  @summary        = params[:summary]        || nil
  @updated        = params[:updated]        || nil
  @published      = params[:published]      || nil
  @edit           = params[:edit]           || nil
  @bm_created     = params[:bm_created]     || nil
end

Instance Attribute Details

#bm_createdObject (readonly)

Tag creation date


46
47
48
# File 'lib/blogmarks/tag.rb', line 46

def bm_created
  @bm_created
end

#editObject (readonly)

Tag edit uri


38
39
40
# File 'lib/blogmarks/tag.rb', line 38

def edit
  @edit
end

#idObject (readonly)

Tag id


32
33
34
# File 'lib/blogmarks/tag.rb', line 32

def id
  @id
end

Tag html representation uri


34
35
36
# File 'lib/blogmarks/tag.rb', line 34

def link_alternate
  @link_alternate
end

Tag image uri


36
37
38
# File 'lib/blogmarks/tag.rb', line 36

def link_image
  @link_image
end

#publishedObject

Tag publish date


44
45
46
# File 'lib/blogmarks/tag.rb', line 44

def published
  @published
end

#summaryObject

Tag summary


42
43
44
# File 'lib/blogmarks/tag.rb', line 42

def summary
  @summary
end

#titleObject

Tag title


40
41
42
# File 'lib/blogmarks/tag.rb', line 40

def title
  @title
end

#updatedObject (readonly)

Tag update date


48
49
50
# File 'lib/blogmarks/tag.rb', line 48

def updated
  @updated
end

Class Method Details

.build_from_xml_element(tag) ⇒ Object

Parse an REXML::Element corresponding to the tag


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blogmarks/tag.rb', line 69

def build_from_xml_element( tag )
  # We need a valide REXML::Element parameters :D
  return nil unless tag.kind_of?(REXML::Element)
  
  # Let's build the params to build a BlogMarks::Tag
  params = {}
  
  # Iterate over child nodes
  tag.each_element do |element|
    case element.name            
      when 'id'
        params[:id]           = element.text
      when 'title'
        params[:title]        = element.text
      when 'updated'
        params[:updated]      = Time.parse(element.text)
      when 'published'
        params[:published]    = Time.parse(element.text)
      when 'edit'
        params[:edit]         = URI.parse(element.text)
      when 'bm:created'
        params[:bm_created]   = Time.parse(element.text)
      when 'summary'
        params[:summary]      = element.text
      when 'link'
        params[:link_alternate] = URI.parse( element.attributes['href'] ) if element.attributes['rel'] == 'alternate'
        params[:link_image]     = URI.parse( element.attributes['href'] ) if element.attributes['rel'] == 'image'
    end
  end
  
  # Create and return a new Tag constructs using params hash
  return Tag.new(params)
end