Class: Hackpad::Cli::Pad

Inherits:
Object
  • Object
show all
Defined in:
lib/hackpad/cli/pad.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Pad

Returns a new instance of Pad.



15
16
17
# File 'lib/hackpad/cli/pad.rb', line 15

def initialize(id)
  @id = id
end

Instance Attribute Details

#cached_atObject (readonly)

Returns the value of attribute cached_at.



13
14
15
# File 'lib/hackpad/cli/pad.rb', line 13

def cached_at
  @cached_at
end

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/hackpad/cli/pad.rb', line 13

def content
  @content
end

#guest_policyObject (readonly)

Returns the value of attribute guest_policy.



13
14
15
# File 'lib/hackpad/cli/pad.rb', line 13

def guest_policy
  @guest_policy
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/hackpad/cli/pad.rb', line 13

def id
  @id
end

#moderatedObject (readonly)

Returns the value of attribute moderated.



13
14
15
# File 'lib/hackpad/cli/pad.rb', line 13

def moderated
  @moderated
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/hackpad/cli/pad.rb', line 60

def cached?
  Store.exists? 'meta', @id
end

#charsObject



23
24
25
# File 'lib/hackpad/cli/pad.rb', line 23

def chars
  @content.length if @content
end

#linesObject



27
28
29
# File 'lib/hackpad/cli/pad.rb', line 27

def lines
  @content.lines.count if @content
end

#load(ext, refresh = false, save = true) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/hackpad/cli/pad.rb', line 31

def load(ext, refresh = false, save = true)
  fail UnknownFormat unless FORMATS.include? ext
  fail UndefinedPad unless @id
  if refresh || !Store.exists?(ext, @id)
    load_from_api ext, save
  else
    load_from_cache ext
  end
end

#load_from_api(ext, dosave = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/hackpad/cli/pad.rb', line 41

def load_from_api(ext, dosave = true)
  @content = Api.read @id, ext
  dosave && Store.save(self, ext)
  options = Api.read_options @id
  @guest_policy = options['options']['guestPolicy']
  @moderated = options['options']['isModerated']
  options['cached_at'] = Time.now
  @cached_at = options['cached_at']
  dosave && Store.save_options(@id, options)
end

#load_from_cache(ext) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/hackpad/cli/pad.rb', line 52

def load_from_cache(ext)
  @content = Store.read @id, ext
  options = Store.read_options @id
  @guest_policy = options['options']['guestPolicy']
  @moderated = options['options']['isModerated']
  @cached_at = options['cached_at']
end

#titleObject



19
20
21
# File 'lib/hackpad/cli/pad.rb', line 19

def title
  @title ||= (@content.lines.first.strip if @content)
end