x2ch 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/x2ch.rb +48 -7
  2. metadata +3 -3
data/lib/x2ch.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'open-uri'
4
4
  require 'kconv'
5
+ require 'zlib'
5
6
 
6
7
  module X2CH
7
8
  class Bbs
@@ -78,14 +79,15 @@ module X2CH
78
79
  end
79
80
 
80
81
  class Thread
81
- attr_accessor :url, :name, :num
82
+ attr_accessor :url, :dat, :name, :num
82
83
 
83
84
  def initialize(url, dat, name, num)
84
85
  @url, @dat, @name, @num = url, dat, name, num
85
86
  end
86
87
 
87
- def posts
88
- Dat.parse(Dat.download(@url + "dat/" + @dat))
88
+ def posts(if_modified_since = nil, range = nil)
89
+ res = Dat.download(@url + "dat/" + @dat, if_modified_since, range)
90
+ ArrayResponse.new(Dat.parse(res), res.status, res.last_modified, res.content_encoding, res.body_size)
89
91
  end
90
92
 
91
93
  def each(&blk)
@@ -104,11 +106,50 @@ module X2CH
104
106
  end
105
107
 
106
108
  class Agent
107
- def self.download(url)
108
- open(url){|f| f.read}.toutf8
109
+ def self.download(url, if_modified_since = nil, range = nil)
110
+ header = {"User-Agent" => "Monazilla/1.00 (x2ch/0.9.1)", "Accept-Encoding" => 'gzip'}
111
+ if if_modified_since
112
+ header["If-Modified-Since"] = if_modified_since
113
+ end
114
+ if range
115
+ header["Range"] = range
116
+ end
117
+ begin
118
+ res = open(url, header){|f|
119
+ body = nil
120
+ if f.content_encoding.index('gzip')
121
+ body = Zlib::GzipReader.new(f).read.toutf8
122
+ else
123
+ body = f.read.toutf8
124
+ end
125
+ [body, f.status, f.last_modified, f.content_encoding, body.size]
126
+ }
127
+ rescue OpenURI::HTTPError => e
128
+ raise DownloadError.new(e.message)
129
+ end
130
+ StringResponse.new(res[0], res[1], res[2], res[3], res[4])
131
+ end
132
+ end
133
+
134
+ class DownloadError < StandardError ; end
135
+
136
+ module Response
137
+ attr_accessor :status, :last_modified, :content_encoding, :body_size
138
+
139
+ def initialize(obj, status, last_modified, content_encoding, body_size)
140
+ @status, @last_modified, @content_encoding, @body_size = status, last_modified, content_encoding, body_size
141
+ super(obj)
109
142
  end
110
143
  end
111
144
 
145
+ class StringResponse < String
146
+ include Response
147
+ end
148
+
149
+ class ArrayResponse < Array
150
+ include Response
151
+ end
152
+
112
153
  class BbsMenu
113
154
  IGNORE_CATEGORIES = ['特別企画', 'チャット', 'ツール類']
114
155
  IGNORE_BOARDS = ['2chプロジェクト', 'いろいろランク']
@@ -165,8 +206,8 @@ module X2CH
165
206
  end
166
207
 
167
208
  class Dat
168
- def self.download(url)
169
- Agent.download(url)
209
+ def self.download(url, if_modified_since = nil, range = nil)
210
+ Agent.download(url, if_modified_since, range)
170
211
  end
171
212
 
172
213
  def self.parse(dat)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x2ch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-21 00:00:00.000000000 Z
12
+ date: 2013-07-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: 2ch downloader and parser library
15
15
  email: mail@xmisao.com
@@ -18,7 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/x2ch.rb
21
- homepage: http://rubygems.org/gems/x2ch
21
+ homepage: https://github.com/xmisao/x2ch
22
22
  licenses: []
23
23
  post_install_message:
24
24
  rdoc_options: []