Class: TumblrApiWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr_api_wrapper.rb

Overview

TumblrのAPIを簡単にラップしたクラス。 RESTful APIを用いて手軽にアクセスすることを目的とする。

Author:

Version:

Constant Summary

API_BASE =
"http://api.tumblr.com/v2"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TumblrApiWrapper) initialize(api_key)

ラッパクラスの初期化メソッド。

Parameters:

  • api_key (String)

    TumblrのAPIキー。 TumblrのOAuth Consumerキーを指定する。 キーの取得の詳細はTumblrのドキュメントを参照。



16
17
18
# File 'lib/tumblr_api_wrapper.rb', line 16

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

- (String) api_key (readonly)

初期化時に設定したAPIキー

Returns:

  • (String)

    初期化時に設定したAPIキー



21
22
23
# File 'lib/tumblr_api_wrapper.rb', line 21

def api_key
  @api_key
end

Instance Method Details

- (Hash) get_blog_info(blog_domain) {|response| ... }

Tumblr ブログのドメインを渡すと、 そのブログの詳細な情報を返す。

Examples:

`foo.tumblr.com` のタイトル情報を取得する場合

api.get_blog_info "foo.tumblr.com" do |info|
  p "Title: " + info["blog"]["title"]
end

Parameters:

  • blog_domain (String)

    対象ブログのドメイン名

Yields:

  • (response)

    ブロックを伴うときは、 ブログのレスポンス情報がブロックパラメータとなる

Returns:

  • (Hash)

    そのブログの詳細情報

Raises:

  • (RuntimeError)

    もしブログが見つからないなど、 正しく情報が取得できなかった場合エラーとする



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tumblr_api_wrapper.rb', line 35

def get_blog_info(blog_domain)
  uri_string = \
    "#{API_BASE}/blog/#{blog_domain}/info" +
    "?api_key=#{api_key}"
  json = Net::HTTP.get(URI.parse(uri_string))
  response = JSON.parse(json)
  if response["meta"]["status"] != 200
    raise "Something is wrong"
  end
  yield(response["response"]) if block_given?
  response["response"]
end

- (Object) get_blog_info_by_user_name(name)

Deprecated.

昔のAPI。名前のみでは使いづらいため。 現バージョンでは #get_blog_info を利用せよ。



50
51
52
# File 'lib/tumblr_api_wrapper.rb', line 50

def get_blog_info_by_user_name(name)
  get_blog_info("#{name}.tumblr.com")
end