hatenablog 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b3523997853851a6cfaf4a4d00230dd4f6b8b06
4
- data.tar.gz: bcfa0433a24776cabbb92b16add175064bf7983d
3
+ metadata.gz: f87d69430e84141add234b284afe132a93b62ec2
4
+ data.tar.gz: 917bc1f2cfdb6a26c892db351b2826f2ee0a03cb
5
5
  SHA512:
6
- metadata.gz: 83c65f0473fd3f11b137b329a7af1d14916262383ef090b4e11da81d9519820068971623b33459199e99b1be1ad0847150bec7230987fa8bd519f2ed2e7b9b61
7
- data.tar.gz: 479bfb1f32b41e701f23ef8c3f5da750177d4894a4154a34f350fb681db8f5557d8bb80445e2c59c26577c86cdf61e856f112b14e6022cd87d72a209ba5f5d4b
6
+ metadata.gz: 274d6839f11d6cf7d74329986e99d09f319137d9fbe751138f645e82e7fe8ccdaa75bb0a8bac3d43a3156cc10866dc5c7c81b4989a46869b4d1e98b3afba152d
7
+ data.tar.gz: fdc4ce8d677bcd77edde50acc4f8b743b68fedd841157d076de8d2cfbe9b89bd1d5191f5eb1c55a4e00f88a5b78a884e48bad67dd51e01730b81fe4ef3ccb6ea
@@ -148,27 +148,25 @@ module Hatenablog
148
148
  # @param [String] author_name entry author name
149
149
  # @return [String] XML string
150
150
  def entry_xml(title = '', content = '', categories = [], draft = 'no', updated = '', author_name = @user_id)
151
- xml = <<XML
152
- <?xml version="1.0" encoding="utf-8"?>
153
- <entry xmlns="http://www.w3.org/2005/Atom"
154
- xmlns:app="http://www.w3.org/2007/app">
155
- <title>%s</title>
156
- <author><name>%s</name></author>
157
- <content type="text/x-markdown">%s</content>
158
- %s%s
159
- <app:control>
160
- <app:draft>%s</app:draft>
161
- </app:control>
162
- </entry>
163
- XML
164
-
165
- unless updated.empty?
166
- updated = '<updated>' + updated + '</updated>'
151
+ builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
152
+ xml.entry('xmlns' => 'http://www.w3.org/2005/Atom',
153
+ 'xmlns:app' => 'http://www.w3.org/2007/app') do
154
+ xml.title title
155
+ xml.author do
156
+ xml.name author_name
157
+ end
158
+ xml.content(content, type: 'text/x-markdown')
159
+ xml.updated updated unless updated.empty? || updated.nil?
160
+ categories.each do |category|
161
+ xml.category(term: category)
162
+ end
163
+ xml['app'].control do
164
+ xml['app'].draft draft
165
+ end
166
+ end
167
167
  end
168
- categories_tag = categories.inject('') do |s, c|
169
- s + "<category term=\"#{c}\" />\n"
170
- end
171
- xml % [title, author_name, content, updated, categories_tag, draft]
168
+
169
+ builder.to_xml
172
170
  end
173
171
 
174
172
 
@@ -1,8 +1,45 @@
1
1
  require 'nokogiri'
2
+ require 'time'
2
3
 
3
4
  module Hatenablog
5
+ module AfterHook
6
+ # Register a hooking method for given methods.
7
+ # The hook method is executed after calling given methods.
8
+ # @param [Symbol] hooking method name
9
+ # @param [Array] hooked methods name array
10
+ def after_hook(hook, *methods)
11
+ methods.each do |method|
12
+ origin_method = "#{method}_origin".to_sym
13
+ if instance_methods.include? origin_method
14
+ raise NameError, "#{origin_method} isn't a unique name"
15
+ end
16
+
17
+ alias_method origin_method, method
18
+
19
+ define_method(method) do |*args, &block|
20
+ result = send(origin_method, *args, &block)
21
+ send(hook)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
4
27
  class Entry
5
- attr_reader :uri, :edit_uri, :id, :author_name, :title, :content, :updated
28
+ extend AfterHook
29
+
30
+ attr_accessor :uri, :author_name, :title, :content, :draft, :categories
31
+ attr_reader :edit_uri, :id, :updated
32
+
33
+ def updated=(date)
34
+ @updated = Time.parse(date)
35
+ end
36
+
37
+ def edit_uri=(uri)
38
+ @edit_uri = uri
39
+ @id = uri.split('/').last
40
+ end
41
+
42
+ after_hook :update_xml, :uri=, :edit_uri=, :author_name=, :title=, :content=, :updated=, :draft=, :categories=
6
43
 
7
44
  # Create a new blog entry from a XML string.
8
45
  # @param [String] xml XML string representation
@@ -23,8 +60,10 @@ module Hatenablog
23
60
  # @return [Hatenablog::Entry]
24
61
  def self.create(uri: '', edit_uri: '', author_name: '', title: '',
25
62
  content: '', draft: 'no', categories: [], updated: '')
26
- Hatenablog::Entry.new(self.build_xml(uri, edit_uri, author_name, title,
27
- content, draft, categories, updated))
63
+ entry = Hatenablog::Entry.new(self.build_xml(uri, edit_uri, author_name, title,
64
+ content, draft, categories, updated))
65
+ yield entry if block_given?
66
+ entry
28
67
  end
29
68
 
30
69
  # @return [Boolean]
@@ -48,34 +87,35 @@ module Hatenablog
48
87
  @document.to_s.gsub(/\"/, "'")
49
88
  end
50
89
 
51
-
52
- private
53
-
54
90
  def self.build_xml(uri, edit_uri, author_name, title, content, draft, categories, updated)
55
- xml = <<XML
56
- <?xml version='1.0' encoding='UTF-8'?>
57
- <entry xmlns:app='http://www.w3.org/2007/app' xmlns='http://www.w3.org/2005/Atom'>
58
- <link href='%s' rel='edit'/>
59
- <link href='%s' rel='alternate' type='text/html'/>
60
- <author><name>%s</name></author>
61
- <title>%s</title>
62
- <content type='text/x-markdown'>%s</content>
63
- %s%s
64
- <app:control>
65
- <app:draft>%s</app:draft>
66
- </app:control>
67
- </entry>
68
- XML
69
-
70
- unless updated.nil?
71
- updated = '<updated>' + updated + '</updated>'
91
+ builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
92
+ xml.entry('xmlns' => 'http://www.w3.org/2005/Atom',
93
+ 'xmlns:app' => 'http://www.w3.org/2007/app') do
94
+ xml.link(href: edit_uri, rel: 'edit')
95
+ xml.link(href: uri, rel: 'alternate', type: 'text/html')
96
+ xml.author do
97
+ xml.name author_name
98
+ end
99
+ xml.title title
100
+ xml.content(content, type: 'text/x-markdown')
101
+ xml.updated updated unless updated.nil? || updated.empty?
102
+ unless categories.nil?
103
+ categories.each do |category|
104
+ xml.category(term: category)
105
+ end
106
+ end
107
+ xml['app'].control do
108
+ xml['app'].draft draft
109
+ end
110
+ end
72
111
  end
73
- categories_tag = categories.inject('') do |s, c|
74
- s + "<category term=\"#{c}\" />\n"
75
- end
76
- xml % [edit_uri, uri, author_name, title, content, updated, categories_tag, draft]
112
+
113
+ builder.to_xml
77
114
  end
78
115
 
116
+
117
+ private
118
+
79
119
  def initialize(xml)
80
120
  @document = Nokogiri::XML(xml)
81
121
  parse_document
@@ -101,5 +141,39 @@ XML
101
141
  end
102
142
  categories
103
143
  end
144
+
145
+ def update_xml
146
+ @document.at_css('author name').content = @author_name
147
+ @document.at_css('title').content = @title
148
+ @document.at_css('link[@rel="alternate"]')['href'] = @uri
149
+ @document.at_css('link[@rel="edit"]')['href'] = @edit_uri
150
+ @document.at_css('content').content = @content
151
+ @document.at_css('entry app|control app|draft').content = @draft
152
+
153
+ unless @updated.nil? || @document.at_css('entry updated').nil?
154
+ @document.at_css('entry updated').content = @updated.iso8601
155
+ end
156
+
157
+ unless @categories.nil?
158
+ old_categories = @document.css('category')
159
+ return if old_categories.empty? || !categories_modified?(old_categories, @categories)
160
+
161
+ prev_node = @document.at_css('category').previous
162
+ old_categories.each do |category|
163
+ category.remove
164
+ end
165
+
166
+ @categories.each do |category|
167
+ prev_node.next = @document.create_element('category', term: category)
168
+ prev_node = prev_node.next
169
+ end
170
+ end
171
+ end
172
+
173
+ def categories_modified?(old_categories, new_categories)
174
+ old_set = Set.new(old_categories.map { |category| category['term'] })
175
+ new_set = Set.new(new_categories)
176
+ old_set != new_set
177
+ end
104
178
  end
105
179
  end
@@ -1,3 +1,3 @@
1
1
  module Hatenablog
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hatenablog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Yamamoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-17 00:00:00.000000000 Z
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  version: '0'
168
168
  requirements: []
169
169
  rubyforge_project:
170
- rubygems_version: 2.4.8
170
+ rubygems_version: 2.4.5.1
171
171
  signing_key:
172
172
  specification_version: 4
173
173
  summary: Hatenablog AtomPub API library