hatenablog 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hatenablog/client.rb +18 -20
- data/lib/hatenablog/entry.rb +101 -27
- data/lib/hatenablog/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f87d69430e84141add234b284afe132a93b62ec2
|
4
|
+
data.tar.gz: 917bc1f2cfdb6a26c892db351b2826f2ee0a03cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 274d6839f11d6cf7d74329986e99d09f319137d9fbe751138f645e82e7fe8ccdaa75bb0a8bac3d43a3156cc10866dc5c7c81b4989a46869b4d1e98b3afba152d
|
7
|
+
data.tar.gz: fdc4ce8d677bcd77edde50acc4f8b743b68fedd841157d076de8d2cfbe9b89bd1d5191f5eb1c55a4e00f88a5b78a884e48bad67dd51e01730b81fe4ef3ccb6ea
|
data/lib/hatenablog/client.rb
CHANGED
@@ -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
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
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
|
-
|
169
|
-
|
170
|
-
end
|
171
|
-
xml % [title, author_name, content, updated, categories_tag, draft]
|
168
|
+
|
169
|
+
builder.to_xml
|
172
170
|
end
|
173
171
|
|
174
172
|
|
data/lib/hatenablog/entry.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
74
|
-
|
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
|
data/lib/hatenablog/version.rb
CHANGED
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.
|
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-
|
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.
|
170
|
+
rubygems_version: 2.4.5.1
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Hatenablog AtomPub API library
|