Class: Tumblr4r::Site

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

Overview

ConnectionオブジェクトとParserオブジェクトを組み合わせて、 TumblrAPIとRubyオブジェクトの相互変換を行う TODO: private な post だけを取得する API が無いのだなぁ

  • Webから更新したものがAPIで取得できるデータに反映されるには少しタイムラグがあるようだ

  • Webから更新しちゃうと、POST日時の秒が丸められてしまう

Constant Summary collapse

API_READ_LIMIT =

TODO: 変数名もうちょっと考える

50
@@default_log_level =
Logger::INFO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, email = nil, password = nil, http = nil, logger = nil) ⇒ Site

Returns a new instance of Site.



51
52
53
54
55
56
57
58
59
60
# File 'lib/tumblr4r.rb', line 51

def initialize(hostname, email=nil, password=nil, http = nil, logger = nil)
  @hostname = hostname
  @email = email
  @password = password
  @logger = logger || Logger.new(STDERR)
  @logger.level = @@default_log_level
  @conn = XMLConnection.new(http || @hostname, email, password, @logger)
  @parser = XMLParser.new
  self.site_info
end

Instance Attribute Details

#cnameObject

Returns the value of attribute cname.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def cname
  @cname
end

#descriptionObject

Returns the value of attribute description.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def description
  @description
end

#emailObject

Returns the value of attribute email.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def email
  @email
end

#feedsObject

Returns the value of attribute feeds.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def feeds
  @feeds
end

#hostnameObject

Returns the value of attribute hostname.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def hostname
  @hostname
end

#loggerObject

Returns the value of attribute logger.



30
31
32
# File 'lib/tumblr4r.rb', line 30

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def name
  @name
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def password
  @password
end

#timezoneObject

Returns the value of attribute timezone.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



28
29
30
# File 'lib/tumblr4r.rb', line 28

def title
  @title
end

Class Method Details

.find(hostname, email = nil, password = nil, http = nil, &block) ⇒ Object

TODO: unit test



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tumblr4r.rb', line 38

def find(hostname, email=nil, password=nil, http=nil, &block)
  site = self.new(hostname, email, password, http)
  result = site.find(:all)
  if block_given?
    result.each do |post|
      yield post
    end
  else
    return result
  end
end

Instance Method Details

#count(options = { }) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tumblr4r.rb', line 130

def count(options = { })
  params = { }
  [:id, :type, :filter, :tagged, :search].each do |option|
    params[option] = options[option] if options[option]
  end
  params[:num] = 1
  params[:start] = 0
  xml = @conn.get(params)
  posts, start, total = @parser.posts(xml)
  return total
end

#delete(post_id) ⇒ Object

Parameters:

  • post_id (Integer)


154
155
156
# File 'lib/tumblr4r.rb', line 154

def delete(post_id)
  return @conn.delete(post_id)
end

#find(id_or_type, options = { }) ⇒ Array<Post>|Post

TODO: ここの再帰取得ロジックはTumblrAPIとは独立してるので TumblrAPIとは独立した形に切り出したり、TumblrAPIとは切り離してテストを書きたいものだ

Parameters:

  • id_or_type (Symbol|Integer)

    :all, id

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tumblr4r.rb', line 66

def find(id_or_type, options = { })
  params = { }
  return result if options[:offset] && options[:offset].to_i < 0
  [:type, :filter, :tagged, :search].each do |option|
    params[option] = options[option] if options[option]
  end

  if id_or_type == :all
    result = []
    # 取得開始位置の初期化
    params[:start] = options[:offset] || 0
    # goal の設定
    total = self.count(options)
    if options[:limit]
      goal = [total - params[:start],
              options[:limit]].min
    else
      goal = total - params[:start]
    end
    # 取得件数の初期化
    if goal < 0
      return result
    elsif goal < API_READ_LIMIT
      params[:num] = goal
    else
      params[:num] = API_READ_LIMIT # :num を指定しないとデフォルトでは20件しかとれない
    end

    loop do
      xml = @conn.get(params)
      posts, start, total = @parser.posts(xml)
      @logger.info("size: #{posts.size}")
      @logger.info("start: #{start}")
      @logger.info("total: #{total}")
      result += posts
      if result.size >= goal || posts.size == 0
        # Tumblr API の total で得られる値は全く信用ならない。
        # 検索条件を考慮した件数を返してくれない。
        # (つまり、goalは信用ならない)ので、posts.sizeも終了判定に利用する。
        # TODO: もしくは:numの値を足し合わせていって、それとgoalを比較する?
        break
      end
      # 取得開始位置の調整
      params[:start] += params[:num]
      # 取得件数の調整
      if (goal - result.size) >= API_READ_LIMIT
        params[:num] = API_READ_LIMIT
      else
        params[:num] = goal - result.size
      end
    end
    return result
  elsif id_or_type.kind_of?(Integer)
    xml = @conn.get({:id => id_or_type})
    posts, start, total = @parser.posts(xml)
    @logger.info("size: #{posts.size}")
    @logger.info("start: #{start}")
    @logger.info("total: #{total}")
    return posts[0]
  else
    raise ArgumentError
  end
end

#save(post) ⇒ Object



147
148
149
150
151
# File 'lib/tumblr4r.rb', line 147

def save(post)
  post_id = @conn.write(post.params)
  new_post = self.find(post_id)
  return new_post
end

#site_infoObject



142
143
144
145
# File 'lib/tumblr4r.rb', line 142

def site_info
  xml = @conn.get(:num => 1)
  @parser.siteinfo(self, xml)
end