Class: GlyDevKit::GlytoucanDataHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlytoucanDataHandler

Returns a new instance of GlytoucanDataHandler.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 11

def initialize
    @gtcfilepath = File.join(Dir.home, '.glycobook', 'gtcdata.json')
    if File.exist?(@gtcfilepath)
    creation_time = File.ctime(@gtcfilepath)
    one_hours_ago = Time.now - (1 * 60 * 60) # 1時間前の時刻
    if creation_time > one_hours_ago
        @all_data = JSON.parse(File.read(@gtcfilepath))
        return
    end
    end
    @all_data = save_glytoucan_data
end

Instance Attribute Details

#all_dataObject (readonly)

Returns the value of attribute all_data.



9
10
11
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 9

def all_data
  @all_data
end

Instance Method Details

#decode(encoded_string) ⇒ Object



41
42
43
44
45
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 41

def decode(encoded_string)
  decoder = Base64.getUrlDecoder()
  compressed_data = decoder.decode(encoded_string)
  Zlib::Inflate.inflate(String.from_java_bytes(compressed_data))
end

#encode(input_string) ⇒ Object



35
36
37
38
39
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 35

def encode(input_string)
  compressed_data = Zlib::Deflate.deflate(input_string)
  encoder = Base64.getUrlEncoder()
  encoder.encodeToString(compressed_data.to_java_bytes)
end

#get_gtcid_by_wurcs(w) ⇒ Object



30
31
32
33
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 30

def get_gtcid_by_wurcs(w)
    return "not found" if @all_data["encode-wurcs-key"][encode(w)].nil?
    @all_data["encode-wurcs-key"][encode(w)]
end

#get_wurcs_by_gtcid(id) ⇒ Object



24
25
26
27
28
# File 'lib/glydevkit/glytoucan_data_handler.rb', line 24

def get_wurcs_by_gtcid(id)
    return "not found" if @all_data["wurcs"][id].nil?
    decode_lambda = ->(encoded_str) { decode(encoded_str) }
    @all_data["wurcs"][id]&.map(&decode_lambda)
end