Module: Nkrb
- Defined in:
- lib/nkrb.rb,
lib/nkrb/version.rb
Constant Summary collapse
- VERSION =
"0.1.7"
Class Method Summary collapse
-
.download_image(url, dir, filename = nil) ⇒ Object
画像のダウンロード 概要 特定の URL の画像をダウンロードする 引数 url: ダウンロードする画像の URL dir: ダウンロード先の dir filename: ダウンロード後の filename.
- .faraday_connection(url, logger = false) ⇒ Object
- .nokogiri(url, charset = 'utf-8') ⇒ Object
- .output_tsv(collection, delimiter: "\t", required_header: true) ⇒ Object
-
.pluck(collection, key:) ⇒ Object
pluck 概要 コレクションからあるキーの値を抽出し, 配列として返す 引数 collection: pluck 対象のコレクション key: pluck するキー名 仕様 1.
- .random_str(len = 5) ⇒ Object
-
.read_file(filename, delimiter: nil) ⇒ Object
file 読み込み 概要 ファイルを一行づつ読み込み, 一行を 1 つの要素とした配列を返す 引数 filename: ファイル名 delimiter: 区切り文字 仕様 1.
-
.read_tsv(filename, delimiter: "\t") ⇒ Object
tsv 読み込み 概要 tsv ファイルを読み込み, 一行目の文字列を key とした collection を返す 引数 filename: ファイル名 仕様 1.
- .remove_ctr(str, replace: "") ⇒ Object
Class Method Details
.download_image(url, dir, filename = nil) ⇒ Object
画像のダウンロード概要特定の URL の画像をダウンロードする引数url: ダウンロードする画像の URL dir: ダウンロード先の dir filename: ダウンロード後の filename
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/nkrb.rb', line 106 def download_image(url, dir, filename = nil) extention = url.split(".").last filename = filename.to_s + "." + extention || File.basename(url) begin open(dir + filename, 'wb+') do |output| open(url) do |data| output.write(data.read) end end rescue => e nil end end |
.faraday_connection(url, logger = false) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/nkrb.rb', line 120 def faraday_connection(url, logger=false) Faraday.new(:url => url) do |faraday| faraday.request :url_encoded faraday.response :logger if logger faraday.adapter Faraday.default_adapter end end |
.nokogiri(url, charset = 'utf-8') ⇒ Object
128 129 130 |
# File 'lib/nkrb.rb', line 128 def nokogiri(url, charset='utf-8') Nokogiri::HTML.parse(open(url), nil, charset) end |
.output_tsv(collection, delimiter: "\t", required_header: true) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/nkrb.rb', line 141 def output_tsv(collection, delimiter: "\t", required_header: true) return if collection.empty? keys = collection.first.keys print "#{keys.join(delimiter)}\n" if required_header collection.each do |item| vals = keys.map do |key| item[key] end print "#{vals.join(delimiter)}\n" end end |
.pluck(collection, key:) ⇒ Object
pluck 概要コレクションからあるキーの値を抽出し, 配列として返す引数collection: pluck 対象のコレクションkey: pluck するキー名仕様
-
引数の collection が配列でなければ nil を返す
-
引数の key が nil であれば nil を返す
-
その他処理の途中でエラーが起これば nil を返す
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/nkrb.rb', line 86 def pluck(collection, key:) return nil if key.nil? || collection.class != Array begin collection.map do |element| element[key] end rescue p "Cannot pluck." nil end end |
.random_str(len = 5) ⇒ Object
132 133 134 135 |
# File 'lib/nkrb.rb', line 132 def random_str(len=5) o = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten (0...len).map { o[rand(o.length)] }.join end |
.read_file(filename, delimiter: nil) ⇒ Object
file 読み込み概要ファイルを一行づつ読み込み, 一行を 1 つの要素とした配列を返す引数filename: ファイル名delimiter: 区切り文字仕様
-
filename が nil または空文字の場合は nil を返す
-
その他処理の途中でエラーが起これば nil を返す
-
一行をある区切り文字で split して欲しい場合は delimiter でその区切り文字を指定する
-
delimiter に指定がなければ (delimiter が nil) 一行をそのまま返す
-
末尾にある改行コードは削除する
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nkrb.rb', line 21 def read_file(filename, delimiter: nil) is_invalid_filename = filename.nil? || filename == '' return nil if is_invalid_filename begin lines = [] File.open(filename) do |f| f.each_line do |line| line = delimiter.nil? ? line.chomp : line.chomp.split(delimiter, -1) lines.push(line) end end lines rescue #p "Error has occured. Unread file." #p "filename: #{filename}" nil end end |
.read_tsv(filename, delimiter: "\t") ⇒ Object
tsv 読み込み概要tsv ファイルを読み込み, 一行目の文字列を key とした collection を返す引数filename: ファイル名仕様
-
filename が nil または空文字の場合は nil を返す
-
その他処理の途中でエラーが起これば nil を返す
-
collection の key となるのは一行目の文字列を t で split した文字列群である
-
末尾にある改行コードは削除する
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nkrb.rb', line 54 def read_tsv(filename, delimiter: "\t") tsv_data = read_file(filename, delimiter: delimiter) return nil if tsv_data.nil? begin headers = tsv_data.shift collection = [] tsv_data.each do |data| obj = {} headers.each_with_index do |header, i| obj[header] = data[i] end collection.push obj end collection rescue #p "Cannot convert to collection!" nil end end |
.remove_ctr(str, replace: "") ⇒ Object
137 138 139 |
# File 'lib/nkrb.rb', line 137 def remove_ctr(str, replace: "") str.gsub(/[[:cntrl:]]/, replace) end |