Module: Simple2ch
- Defined in:
- lib/simple2ch.rb,
lib/simple2ch/bbs.rb,
lib/simple2ch/dat.rb,
lib/simple2ch/board.rb,
lib/simple2ch/regex.rb,
lib/simple2ch/thread.rb,
lib/simple2ch/version.rb,
lib/simple2ch/response.rb,
lib/simple2ch/simple2ch_error.rb
Defined Under Namespace
Classes: BBS, Board, Dat, DatParseError, KakoLogError, NoThreGivenError, NotA2chBoardUrlError, NotA2chThreadUrlError, NotA2chUrlError, Regex, Response, Simple2chError, Thread
Constant Summary collapse
- VERSION =
'1.1.0'
Class Method Summary collapse
-
.fetch(url, encode = nil) ⇒ String
HTTPでGETする.
- .generate_url(type, url, params = {}) ⇒ Object
-
.type_of_2ch(url) ⇒ Symbol
2chのタイプを返す.
Instance Method Summary collapse
Class Method Details
.fetch(url, encode = nil) ⇒ String
HTTPでGETする
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple2ch.rb', line 23 def self.fetch(url, encode = nil) url_obj = URI.parse(url.to_s) encode ||= encoded_in_sjis?(url) ? 'sjis' : 'utf-8' errors_to_retry = [OpenURI::HTTPError, SocketError] Retryable.retryable(tries: 5, on: errors_to_retry, sleep: 3) do OpenURI.open_uri(url_obj, "r:#{encode}") .read .encode('utf-8', undef: :replace, invalid: :replace, replace: '〓') end end |
.generate_url(type, url, params = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/simple2ch.rb', line 56 def self.generate_url(type, url, params = {}) # bbs: http://www.2ch.sc/, http://open2ch.net/ # board: http://viper.2ch.sc/news4vip/, http://viper.open2ch.net/news4vip/ # dat: http://viper.2ch.sc/news4vip/dat/9990000001.dat, http://viper.open2ch.net/news4vip/dat/1439127670.dat # subject: http://viper.2ch.sc/news4vip/subject.txt, http://viper.open2ch.net/news4vip/subject.txt # setting: http://viper.2ch.sc/news4vip/SETTING.TXT, http://viper.open2ch.net/news4vip/SETTING.TXT # thread: http://viper.2ch.sc/test/read.cgi/news4vip/9990000001/, http://viper.open2ch.net/test/read.cgi/news4vip/1439127670 url_obj = Bbs2chUrlValidator::URL.parse(url.to_s) domain = "#{url_obj.open? ? 'open' : ''}2ch.#{url_obj.tld}" board_name = params[:board_name] ? params[:board_name] : url_obj.board_name case type when :bbs "http://www.#{domain}.#{url_obj.tld}/" when :board "http://#{url_obj.server_name}.#{domain}/#{board_name}/" when :dat url_obj.dat when :subject url_obj.subject when :setting url_obj.setting when :thread thread_key = params[:thread_key] ? params[:thread_key] : url_obj.thread_key "http://#{url_obj.server_name}.#{domain}/test/read.cgi/#{board_name}/#{thread_key}/" end end |
.type_of_2ch(url) ⇒ Symbol
2chのタイプを返す
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simple2ch.rb', line 38 def self.type_of_2ch(url) parsed_url = Bbs2chUrlValidator::URL.parse(url) raise NotA2chUrlError, "Given URL: #{url}" unless parsed_url case true when parsed_url.is_open && parsed_url.tld == 'net' :open when !parsed_url.is_open && parsed_url.tld == 'net' :net when !parsed_url.is_open && parsed_url.tld == 'sc' :sc else binding.pry raise NotA2chUrlError, "Given URL: #{url}" end end |
Instance Method Details
#a_bone_data ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/simple2ch/response.rb', line 126 def a_bone_data { author: 'あぼーん', mail: 'あぼーん', contents: 'あぼーん', date: 'あぼーん', author_id: 'あぼーん', } end |