Class: Hatenablog::Client
- Inherits:
-
Object
- Object
- Hatenablog::Client
- Defined in:
- lib/hatenablog/client.rb
Constant Summary collapse
- DEFAULT_CONFIG_PATH =
'./config.yml'.freeze
- COLLECTION_URI =
"https://blog.hatena.ne.jp/%s/%s/atom/entry".freeze
- MEMBER_URI =
"https://blog.hatena.ne.jp/%s/%s/atom/entry/%s".freeze
- CATEGORY_URI =
"https://blog.hatena.ne.jp/%s/%s/atom/category".freeze
Instance Attribute Summary collapse
-
#requester ⇒ Object
writeonly
Sets the attribute requester.
Class Method Summary collapse
-
.create(config_file = DEFAULT_CONFIG_PATH) {|blog| ... } ⇒ Hatenablog::Client
Create a new hatenablog AtomPub client from a configuration file.
Instance Method Summary collapse
-
#all_entries ⇒ Hatenablog::Entries
Get all blog entries.
-
#author_name ⇒ String
Get a author name.
-
#categories ⇒ Array
Get blog categories array.
-
#category_doc_uri(user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub category document URI.
-
#collection_uri(user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub collection URI.
-
#delete_entry(entry_id) ⇒ Object
Delete a blog entry specified by its ID.
-
#entries(page = 0) ⇒ Hatenablog::Entries
Get a enumerator of blog entries.
-
#entry_xml(title = '', content = '', categories = [], draft = 'no', updated = '', author_name = @user_id) ⇒ String
Build a entry XML from arguments.
-
#get_entry(entry_id) ⇒ Hatenablog::BlogEntry
Get a blog entry specified by its ID.
-
#initialize(config = nil) ⇒ Client
constructor
A new instance of Client.
-
#member_uri(entry_id, user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub member URI.
-
#next_feed(feed = nil) ⇒ Hatenablog::Feed
Get the next feed of the given feed.
-
#post_entry(title = '', content = '', categories = [], draft = 'no') ⇒ Hatenablog::BlogEntry
Post a blog entry.
-
#title ⇒ String
Get a blog title.
-
#update_entry(entry_id, title = '', content = '', categories = [], draft = 'no', updated = '') ⇒ Hatenablog::BlogEntry
Update a blog entry specified by its ID.
Constructor Details
#initialize(config = nil) ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 34 35 |
# File 'lib/hatenablog/client.rb', line 27 def initialize(config = nil) if block_given? yield config = Configuration.new config.check_valid_or_raise end @requester = Requester.create(config) @user_id = config.user_id @blog_id = config.blog_id end |
Instance Attribute Details
#requester=(value) ⇒ Object (writeonly)
Sets the attribute requester
15 16 17 |
# File 'lib/hatenablog/client.rb', line 15 def requester=(value) @requester = value end |
Class Method Details
.create(config_file = DEFAULT_CONFIG_PATH) {|blog| ... } ⇒ Hatenablog::Client
Create a new hatenablog AtomPub client from a configuration file.
20 21 22 23 24 25 |
# File 'lib/hatenablog/client.rb', line 20 def self.create(config_file = DEFAULT_CONFIG_PATH) config = Configuration.create(config_file) blog = Hatenablog::Client.new(config) return blog unless block_given? yield blog end |
Instance Method Details
#all_entries ⇒ Hatenablog::Entries
Get all blog entries.
61 62 63 |
# File 'lib/hatenablog/client.rb', line 61 def all_entries Entries.new(self, nil) end |
#author_name ⇒ String
Get a author name.
46 47 48 49 |
# File 'lib/hatenablog/client.rb', line 46 def feed = Feed.load_xml(get_collection(collection_uri).body) feed. end |
#categories ⇒ Array
Get blog categories array.
77 78 79 80 |
# File 'lib/hatenablog/client.rb', line 77 def categories categories_doc = Category.new(get_category_doc.body) categories_doc.categories end |
#category_doc_uri(user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub category document URI.
143 144 145 |
# File 'lib/hatenablog/client.rb', line 143 def category_doc_uri(user_id = @user_id, blog_id = @blog_id) CATEGORY_URI % [user_id, blog_id] end |
#collection_uri(user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub collection URI.
126 127 128 |
# File 'lib/hatenablog/client.rb', line 126 def collection_uri(user_id = @user_id, blog_id = @blog_id) COLLECTION_URI % [user_id, blog_id] end |
#delete_entry(entry_id) ⇒ Object
Delete a blog entry specified by its ID.
118 119 120 |
# File 'lib/hatenablog/client.rb', line 118 def delete_entry(entry_id) delete(member_uri(entry_id)) end |
#entries(page = 0) ⇒ Hatenablog::Entries
Get a enumerator of blog entries.
54 55 56 57 |
# File 'lib/hatenablog/client.rb', line 54 def entries(page = 0) raise ArgumentError.new('page must be non-negative') if page < 0 Entries.new(self, page) end |
#entry_xml(title = '', content = '', categories = [], draft = 'no', updated = '', author_name = @user_id) ⇒ String
Build a entry XML from arguments.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/hatenablog/client.rb', line 155 def entry_xml(title = '', content = '', categories = [], draft = 'no', updated = '', = @user_id) 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.title title xml. do xml.name end xml.content(content, type: 'text/x-markdown') xml.updated updated unless updated.empty? || updated.nil? categories.each do |category| xml.category(term: category) end xml['app'].control do xml['app'].draft draft end end end builder.to_xml end |
#get_entry(entry_id) ⇒ Hatenablog::BlogEntry
Get a blog entry specified by its ID.
85 86 87 88 |
# File 'lib/hatenablog/client.rb', line 85 def get_entry(entry_id) response = get(member_uri(entry_id)) Entry.load_xml(response.body) end |
#member_uri(entry_id, user_id = @user_id, blog_id = @blog_id) ⇒ String
Get Hatenablog AtomPub member URI.
135 136 137 |
# File 'lib/hatenablog/client.rb', line 135 def member_uri(entry_id, user_id = @user_id, blog_id = @blog_id) MEMBER_URI % [user_id, blog_id, entry_id] end |
#next_feed(feed = nil) ⇒ Hatenablog::Feed
Get the next feed of the given feed. Return the first feed if no argument is passed.
69 70 71 72 73 |
# File 'lib/hatenablog/client.rb', line 69 def next_feed(feed = nil) return Feed.load_xml(get_collection(collection_uri).body) if feed.nil? return nil unless feed.has_next? Feed.load_xml(get_collection(feed.next_uri).body) end |
#post_entry(title = '', content = '', categories = [], draft = 'no') ⇒ Hatenablog::BlogEntry
Post a blog entry.
96 97 98 99 100 |
# File 'lib/hatenablog/client.rb', line 96 def post_entry(title = '', content = '', categories = [], draft = 'no') entry_xml = entry_xml(title, content, categories, draft) response = post(entry_xml) Entry.load_xml(response.body) end |
#title ⇒ String
Get a blog title.
39 40 41 42 |
# File 'lib/hatenablog/client.rb', line 39 def title feed = Feed.load_xml(get_collection(collection_uri).body) feed.title end |
#update_entry(entry_id, title = '', content = '', categories = [], draft = 'no', updated = '') ⇒ Hatenablog::BlogEntry
Update a blog entry specified by its ID.
110 111 112 113 114 |
# File 'lib/hatenablog/client.rb', line 110 def update_entry(entry_id, title = '', content = '', categories = [], draft = 'no', updated = '') entry_xml = entry_xml(title, content, categories, draft, updated) response = put(entry_xml, member_uri(entry_id)) Entry.load_xml(response.body) end |