Class: Hatenablog::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute requester to.



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.

Parameters:

  • config_file (String) (defaults to: DEFAULT_CONFIG_PATH)

    configuration file path

Yields:

  • (blog)

Returns:



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_entriesHatenablog::Entries

Get all blog entries.

Returns:



61
62
63
# File 'lib/hatenablog/client.rb', line 61

def all_entries
  Entries.new(self, nil)
end

#author_nameString

Get a author name.

Returns:

  • (String)

    blog author name



46
47
48
49
# File 'lib/hatenablog/client.rb', line 46

def author_name
  feed = Feed.load_xml(get_collection(collection_uri).body)
  feed.author_name
end

#categoriesArray

Get blog categories array.

Returns:

  • (Array)

    blog categories



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.

Parameters:

  • user_id (String) (defaults to: @user_id)

    Hatena user ID

  • blog_id (String) (defaults to: @blog_id)

    Hatenablog ID

Returns:

  • (String)

    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.

Parameters:

  • user_id (String) (defaults to: @user_id)

    Hatena user ID

  • blog_id (String) (defaults to: @blog_id)

    Hatenablog ID

Returns:

  • (String)

    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.

Parameters:

  • entry_id (String)

    deleted entry 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.

Parameters:

  • page (Fixnum) (defaults to: 0)

    page number to get

Returns:

Raises:

  • (ArgumentError)


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.

Parameters:

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

    entry title

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

    entry content

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

    entry categories

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

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

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

    entry updated datetime (ISO 8601)

  • author_name (String) (defaults to: @user_id)

    entry author name

Returns:

  • (String)

    XML string



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 = '', 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 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.

Parameters:

  • entry_id (String)

    entry ID

Returns:

  • (Hatenablog::BlogEntry)

    entry



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.

Parameters:

  • entry_id (String)

    entry ID

  • user_id (String) (defaults to: @user_id)

    Hatena user ID

  • blog_id (String) (defaults to: @blog_id)

    Hatenablog ID

Returns:

  • (String)

    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.

Parameters:

Returns:



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.

Parameters:

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

    entry title

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

    entry content

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

    entry categories

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

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

Returns:

  • (Hatenablog::BlogEntry)

    posted 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

#titleString

Get a blog title.

Returns:

  • (String)

    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.

Parameters:

  • entry_id (String)

    updated entry ID

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

    entry title

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

    entry content

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

    entry categories

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

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

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

    entry updated datetime (ISO 8601)

Returns:

  • (Hatenablog::BlogEntry)

    updated entry



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