class Hatenablog::Client

Constants

CATEGORY_URI
COLLECTION_URI
DEFAULT_CONFIG_PATH
MEMBER_URI

Attributes

requester[W]

@dynamic requester=

Public Class Methods

create(config_file = DEFAULT_CONFIG_PATH) { |blog| ... } click to toggle source

Create a new hatenablog AtomPub client from a configuration file. @param [String] config_file configuration file path @return [Hatenablog::Client] created hatenablog client

# File lib/hatenablog/client.rb, line 21
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
new(config = Configuration.new) { |config| ... } click to toggle source
# File lib/hatenablog/client.rb, line 28
def initialize(config = Configuration.new)
  if block_given?
    yield config
    config.check_valid_or_raise
  end
  @requester = Requester.create(config)
  @user_id = config.user_id
  @blog_id = config.blog_id
end

Public Instance Methods

all_entries() click to toggle source

Get all blog entries. @return [Hatenablog::Entries] enumerator of blog entries

# File lib/hatenablog/client.rb, line 62
def all_entries
  Entries.new(self, 0, :all)
end
author_name() click to toggle source

Get a author name. @return [String] blog author name

# File lib/hatenablog/client.rb, line 47
def author_name
  feed = Feed.load_xml(get_collection(collection_uri).body)
  feed.author_name
end
categories() click to toggle source

Get blog categories array. @return [Array] blog categories

# File lib/hatenablog/client.rb, line 78
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) click to toggle source

Get Hatenablog AtomPub category document URI. @param [String] user_id Hatena user ID @param [String] blog_id Hatenablog ID @return [String] Hatenablog AtomPub category document URI

# File lib/hatenablog/client.rb, line 145
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) click to toggle source

Get Hatenablog AtomPub collection URI. @param [String] user_id Hatena user ID @param [String] blog_id Hatenablog ID @return [String] Hatenablog AtomPub collection URI

# File lib/hatenablog/client.rb, line 128
def collection_uri(user_id = @user_id, blog_id = @blog_id)
  COLLECTION_URI % [user_id, blog_id]
end
delete_entry(entry_id) click to toggle source

Delete a blog entry specified by its ID. @param [String] entry_id deleted entry ID

# File lib/hatenablog/client.rb, line 120
def delete_entry(entry_id)
  delete(member_uri(entry_id))
end
entries(page = 0) click to toggle source

Get a enumerator of blog entries. @param [Fixnum] page page number to get @return [Hatenablog::Entries] enumerator of blog entries

# File lib/hatenablog/client.rb, line 55
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) click to toggle source

Build a entry XML from arguments. @param [String] title entry title @param [String] content entry content @param [Array] categories entry categories @param [String] draft this entry is draft if ‘yes’, otherwise it is not draft @param [String] updated entry updated datetime (ISO 8601) @param [String] author_name entry author name @return [String] XML string

# File lib/hatenablog/client.rb, line 157
def entry_xml(title = '', content = '', categories = [], draft = 'no', updated = '', author_name = @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.author do
        xml.name author_name
      end
      xml.content(content, type: 'text/x-markdown')
      xml.updated updated if updated && !updated.empty?
      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) click to toggle source

Get a blog entry specified by its ID. @param [String] entry_id entry ID @return [Hatenablog::BlogEntry] entry

# File lib/hatenablog/client.rb, line 86
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) click to toggle source

Get Hatenablog AtomPub member URI. @param [String] entry_id entry ID @param [String] user_id Hatena user ID @param [String] blog_id Hatenablog ID @return [String] Hatenablog AtomPub member URI

# File lib/hatenablog/client.rb, line 137
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) click to toggle source

Get the next feed of the given feed. Return the first feed if no argument is passed. @param [Hatenablog::Feed] feed blog feed @return [Hatenablog::Feed] next blog feed

# File lib/hatenablog/client.rb, line 70
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', updated = '') click to toggle source

Post a blog entry. @param [String] title entry title @param [String] content entry content @param [Array] categories entry categories @param [String] draft this entry is draft if ‘yes’, otherwise it is not draft @param [String] updated entry updated datetime (ISO 8601) @return [Hatenablog::BlogEntry] posted entry

# File lib/hatenablog/client.rb, line 98
def post_entry(title = '', content = '', categories = [], draft = 'no', updated = '')
  entry_xml = entry_xml(title, content, categories, draft, updated)
  response = post(entry_xml)
  Entry.load_xml(response.body)
end
title() click to toggle source

Get a blog title. @return [String] blog title

# File lib/hatenablog/client.rb, line 40
def title
  feed = Feed.load_xml(get_collection(collection_uri).body)
  feed.title
end
update_entry(entry_id, title = '', content = '', categories = [], draft = 'no', updated = '') click to toggle source

Update a blog entry specified by its ID. @param [String] entry_id updated entry ID @param [String] title entry title @param [String] content entry content @param [Array] categories entry categories @param [String] draft this entry is draft if ‘yes’, otherwise it is not draft @param [String] updated entry updated datetime (ISO 8601) @return [Hatenablog::BlogEntry] updated entry

# File lib/hatenablog/client.rb, line 112
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

Private Instance Methods

delete(uri) click to toggle source
# File lib/hatenablog/client.rb, line 204
def delete(uri)
  @requester.delete(uri)
end
get(uri) click to toggle source
# File lib/hatenablog/client.rb, line 181
def get(uri)
  @requester.get(uri)
end
get_category_doc() click to toggle source
# File lib/hatenablog/client.rb, line 192
def get_category_doc
  get(category_doc_uri)
end
get_collection(uri = collection_uri) click to toggle source
# File lib/hatenablog/client.rb, line 185
def get_collection(uri = collection_uri)
  unless uri.include?(collection_uri)
    raise ArgumentError.new('Invalid collection URI: ' + uri)
  end
  get(uri)
end
post(entry_xml, uri = collection_uri) click to toggle source
# File lib/hatenablog/client.rb, line 196
def post(entry_xml, uri = collection_uri)
  @requester.post(uri, entry_xml)
end
put(entry_xml, uri) click to toggle source
# File lib/hatenablog/client.rb, line 200
def put(entry_xml, uri)
  @requester.put(uri, entry_xml)
end