Class: PasteHub::Entry

Inherits:
EntryBase show all
Defined in:
lib/pastehub/syncentry.rb

Instance Method Summary collapse

Methods inherited from EntryBase

#decode_body, #encode_body, #gen_header

Constructor Details

#initialize(hostname) ⇒ Entry

Returns a new instance of Entry.



67
68
69
# File 'lib/pastehub/syncentry.rb', line 67

def initialize( hostname )
  super( hostname )
end

Instance Method Details

#can_load?Boolean

check the file saved completely

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pastehub/syncentry.rb', line 84

def can_load?()
  if not File.exist?( @filepath )
    false
  else
    json = nil
    open( @filepath, "r" ) {|f|
      firstline = f.readline.chomp
      begin
        json = JSON.parse( firstline )
      rescue JSON::JSONError
        return false
      end
#         p "json[encodedBodySize]", json[ 'encodedBodySize' ]
      if not json[ 'encodedBodySize' ]
        return false
      end

      secondline = f.readline.chomp
#         p "secondline", secondline
#         p "secondline.size()", secondline.size()
      if json[ 'encodedBodySize' ] != secondline.size()
        return false # body is incomplete
      end
    }
    return true
  end
end

#loadObject

load from file



113
114
115
116
117
118
119
120
# File 'lib/pastehub/syncentry.rb', line 113

def load
  open( @filepath, "r" ) { |f|
    firstline  = f.readline.chomp
    h = JSON.parse( firstline )
    secondline = f.readline.chomp
    return [h, decode_body( secondline ) ]
  }
end

#save(bin) ⇒ Object

save as file



72
73
74
75
76
77
78
79
80
81
# File 'lib/pastehub/syncentry.rb', line 72

def save( bin )
  create_date = Time.now()
  encoded = encode_body( bin )
  json_str = gen_header( create_date, bin, encoded )
  open( @filepath, "w" ) { |f|
    f.puts json_str
    f.puts encoded
  }
  true
end