Class: Logbook::GistStore

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

Constant Summary collapse

DATE_FMT =
"%Y%m%d".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGistStore

Returns a new instance of GistStore.



8
9
10
11
# File 'lib/gist_store/gist_store.rb', line 8

def initialize
  @valid = !!(ENV['GITHUB_USER'] && ENV['GITHUB_PASSWORD'])
  @error = @valid ? '' : "Please set up GITHUB_USER and GITHUB_PASSWORD in your env."
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/gist_store/gist_store.rb', line 6

def error
  @error
end

Instance Method Details

#all(id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gist_store/gist_store.rb', line 34

def all(id)
  gist = Gist.read_raw(id)
  return nil unless gist

  entries = gist['files'].reject{|k|  ["cover"].include? k }.map do |fname, v|
    { :date    => DateTime.strptime(v['filename'], DATE_FMT),
      :content => v['content'] }
  end

  { :id => id,
    :cover => gist['files']['cover']['content'],
    :entries => entries }
end

#create(covertext) ⇒ Object



25
26
27
28
# File 'lib/gist_store/gist_store.rb', line 25

def create(covertext)
  u = URI.parse Gist.write(gist_data(covertext, "cover"), true)
  u.path[1..-1]
end

#destroy(id) ⇒ Object



48
49
50
# File 'lib/gist_store/gist_store.rb', line 48

def destroy(id)
  Gist.delete(id)
end

#get(id, time) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/gist_store/gist_store.rb', line 17

def get(id, time)
  gist = Gist.read_raw(id)
  return nil unless gist
  file = gist['files'][gist_file(time)]
  return nil unless file
  file['content']
end

#update(id, time, page) ⇒ Object



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

def update(id, time, page)
  Gist.update(id, gist_data(page, gist_file(time)))
end

#valid?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/gist_store/gist_store.rb', line 13

def valid?
  @valid
end