Class: Hatenablog::Entry

Inherits:
Object
  • Object
show all
Extended by:
AfterHook
Defined in:
lib/hatenablog/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterHook

after_hook

Instance Attribute Details

#author_nameObject

Returns the value of attribute author_name.



36
37
38
# File 'lib/hatenablog/entry.rb', line 36

def author_name
  @author_name
end

#categoriesArray

Returns:

  • (Array)


86
87
88
# File 'lib/hatenablog/entry.rb', line 86

def categories
  @categories.dup
end

#contentObject

Returns the value of attribute content.



36
37
38
# File 'lib/hatenablog/entry.rb', line 36

def content
  @content
end

#draftObject

Returns the value of attribute draft.



36
37
38
# File 'lib/hatenablog/entry.rb', line 36

def draft
  @draft
end

#edit_uriObject

Returns the value of attribute edit_uri.



39
40
41
# File 'lib/hatenablog/entry.rb', line 39

def edit_uri
  @edit_uri
end

#idObject (readonly)

Returns the value of attribute id.



39
40
41
# File 'lib/hatenablog/entry.rb', line 39

def id
  @id
end

#titleObject

Returns the value of attribute title.



36
37
38
# File 'lib/hatenablog/entry.rb', line 36

def title
  @title
end

#updatedObject

Returns the value of attribute updated.



39
40
41
# File 'lib/hatenablog/entry.rb', line 39

def updated
  @updated
end

#uriObject

Returns the value of attribute uri.



36
37
38
# File 'lib/hatenablog/entry.rb', line 36

def uri
  @uri
end

Class Method Details

.build_xml(uri, edit_uri, author_name, title, content, draft, categories, updated) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hatenablog/entry.rb', line 106

def self.build_xml(uri, edit_uri, author_name, title, content, draft, categories, updated)
  builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
    xml.entry('xmlns'     => 'http://www.w3.org/2005/Atom',
              'xmlns:app' => 'http://www.w3.org/2007/app') do
      xml.link(href: edit_uri, rel: 'edit')
      xml.link(href: uri,      rel: 'alternate', type: 'text/html')
      xml.author do
        xml.name author_name
      end
      xml.title title
      xml.content(content, type: 'text/x-markdown')
      xml.updated updated unless updated.nil? || updated.empty?
      unless categories.nil?
        categories.each do |category|
          xml.category(term: category)
        end
      end
      xml['app'].control do
        xml['app'].draft draft
      end
    end
  end

  builder.to_xml
end

.create(uri: '', edit_uri: '', author_name: '', title: '', content: '', draft: 'no', categories: [], updated: '') {|entry| ... } ⇒ Hatenablog::Entry

Create a new blog entry from arguments.

Parameters:

  • uri (String) (defaults to: '')

    entry URI

  • edit_uri (String) (defaults to: '')

    entry URI for editing

  • author_name (String) (defaults to: '')

    entry author name

  • title (String) (defaults to: '')

    entry title

  • content (String) (defaults to: '')

    entry content

  • draft (String) (defaults to: 'no')

    this entry is draft if ‘yes’, otherwise it is not draft

  • categories (Array) (defaults to: [])

    categories array

  • updated (String) (defaults to: '')

    entry updated datetime (ISO 8601)

Yields:

  • (entry)

Returns:



72
73
74
75
76
77
78
# File 'lib/hatenablog/entry.rb', line 72

def self.create(uri: '', edit_uri: '', author_name: '', title: '',
                content: '', draft: 'no', categories: [], updated: '')
  entry = Hatenablog::Entry.new(self.build_xml(uri, edit_uri, author_name, title,
                                               content, draft, categories, updated))
  yield entry if block_given?
  entry
end

.load_xml(xml) ⇒ Hatenablog::Entry

Create a new blog entry from a XML string.

Parameters:

  • xml (String)

    XML string representation

Returns:



58
59
60
# File 'lib/hatenablog/entry.rb', line 58

def self.load_xml(xml)
  Hatenablog::Entry.new(xml)
end

Instance Method Details

#draft?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/hatenablog/entry.rb', line 81

def draft?
  @draft == 'yes'
end

#each_categoryObject



90
91
92
93
94
# File 'lib/hatenablog/entry.rb', line 90

def each_category
  @categories.each do |category|
    yield category
  end
end

#formatted_contentString

Returns:

  • (String)


102
103
104
# File 'lib/hatenablog/entry.rb', line 102

def formatted_content
  @formatted_content
end

#to_xmlString

Returns:

  • (String)


97
98
99
# File 'lib/hatenablog/entry.rb', line 97

def to_xml
  @document.to_s.gsub(/\"/, "'")
end