Class: GridCLI::FileStorage
- Inherits:
-
StorageBase
- Object
- StorageBase
- GridCLI::FileStorage
- Defined in:
- lib/gridcli/storage/files.rb
Instance Method Summary collapse
-
#append(posts) ⇒ Object
return sha of last post.
- #dates(type, year, month) ⇒ Object
- #files(type, start_date, end_date) ⇒ Object
- #has_type?(type) ⇒ Boolean
-
#initialize ⇒ FileStorage
constructor
A new instance of FileStorage.
- #list(type, start_date, end_date, output_format) ⇒ Object
- #max_date(type) ⇒ Object
- #min_date(type) ⇒ Object
- #months(type, year) ⇒ Object
- #open(type, date) ⇒ Object
- #run(pipes) ⇒ Object
- #search(type, query, start_date, end_date, output_format, fromuser = nil) ⇒ Object
- #write_post(post, type) ⇒ Object
- #years(type) ⇒ Object
Constructor Details
#initialize ⇒ FileStorage
Returns a new instance of FileStorage.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/gridcli/storage/files.rb', line 6 def initialize super @basedir = File.(File.join("~", ".grid")) @types = [ 'like', 'dislike', 'status', 'message' ] @types.each { |type| path = File.join(@basedir, type) Dir.mkdir(path) if not File.exists?(path) } @handles = {} end |
Instance Method Details
#append(posts) ⇒ Object
return sha of last post
18 19 20 21 22 23 24 25 26 |
# File 'lib/gridcli/storage/files.rb', line 18 def append(posts) obj = nil posts.each { |post| type = post.known_attributes.first obj = post.send(type) write_post obj, type } obj.sha end |
#dates(type, year, month) ⇒ Object
52 53 54 55 56 |
# File 'lib/gridcli/storage/files.rb', line 52 def dates(type, year, month) Dir.glob(File.join(@basedir, type, year.to_s, month.to_s, '*')).map { |f| File.basename(f).split('.').first.to_i } end |
#files(type, start_date, end_date) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gridcli/storage/files.rb', line 76 def files(type, start_date, end_date) types = (type.strip == "all") ? @types : [type] types.each { |t| next unless has_type?(t) dates = [start_date || min_date(t), end_date || max_date(t)] dates.min.upto(dates.max) { |d| path = File.join(@basedir, t, d.year.to_s, d.month.to_s, d.day.to_s + ".json") yield path if File.exists? path } } end |
#has_type?(type) ⇒ Boolean
72 73 74 |
# File 'lib/gridcli/storage/files.rb', line 72 def has_type?(type) Dir.glob(File.join(@basedir, type, "*")).length > 0 end |
#list(type, start_date, end_date, output_format) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/gridcli/storage/files.rb', line 102 def list(type, start_date, end_date, output_format) = output_format.nil? ? "" : "-o #{output_format}" files(type, start_date, end_date) { |path| run ["cat #{path}", "#{$0} pprint #{}"] } end |
#max_date(type) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/gridcli/storage/files.rb', line 65 def max_date(type) year = years(type).sort.last month = months(type, year).sort.last date = dates(type, year, month).sort.last Date.new year, month, date end |
#min_date(type) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/gridcli/storage/files.rb', line 58 def min_date(type) year = years(type).sort.first month = months(type, year).sort.first date = dates(type, year, month).sort.first Date.new year, month, date end |
#months(type, year) ⇒ Object
48 49 50 |
# File 'lib/gridcli/storage/files.rb', line 48 def months(type, year) Dir.glob(File.join(@basedir, type, year.to_s, '*')).map { |f| File.basename(f).to_i } end |
#open(type, date) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/gridcli/storage/files.rb', line 34 def open(type, date) dir = File.join(@basedir, type, date.year.to_s, date.month.to_s) path = File.join(dir, date.day.to_s + ".json") unless @handles.has_key? path FileUtils.mkdir_p dir @handles[path] = File.open(path, 'a') end @handles[path] end |
#run(pipes) ⇒ Object
88 89 90 |
# File 'lib/gridcli/storage/files.rb', line 88 def run(pipes) system pipes.join(" | ") end |
#search(type, query, start_date, end_date, output_format, fromuser = nil) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/gridcli/storage/files.rb', line 92 def search(type, query, start_date, end_date, output_format, fromuser=nil) = output_format.nil? ? "" : "-o #{output_format}" files(type, start_date, end_date) { |path| pipes = ["grep -Rhi \"#{query}\" #{path}"] pipes << "grep '\"from_username\":\"#{fromuser}\"'" unless fromuser.nil? pipes << "#{$0} pprint #{}" run pipes } end |
#write_post(post, type) ⇒ Object
28 29 30 31 32 |
# File 'lib/gridcli/storage/files.rb', line 28 def write_post(post, type) created_at = Date.parse(post.created_at) json = post.to_json(:root => type) + "\n" open(type, created_at).write json end |
#years(type) ⇒ Object
44 45 46 |
# File 'lib/gridcli/storage/files.rb', line 44 def years(type) Dir.glob(File.join(@basedir, type, '*')).map { |f| File.basename(f).to_i } end |