Class: SudachiInstaller::Dicts

Inherits:
Object
  • Object
show all
Defined in:
lib/sudachi-installer/dicts.rb

Overview

dictionary index and download

Instance Method Summary collapse

Constructor Details

#initialize(httpclient: Faraday) ⇒ Dicts

Returns a new instance of Dicts.

Parameters:

  • httpclient (Object) (defaults to: Faraday)


14
15
16
17
18
19
20
# File 'lib/sudachi-installer/dicts.rb', line 14

def initialize(httpclient: Faraday)
  @http = httpclient
  @endpoint = {
    index: "https://sudachi.s3-ap-northeast-1.amazonaws.com",
    zip: "http://sudachi.s3-website-ap-northeast-1.amazonaws.com"
  }
end

Instance Method Details

#download(type, param1, ...) ⇒ Object

Parameters:

  • type (Symbol)
  • param1 (String)
  • ... (String)


100
101
102
103
104
105
106
107
108
109
# File 'lib/sudachi-installer/dicts.rb', line 100

def download(type, *param)
  source = url(type, *param)
  filename = File.basename(source)

  Downloader.new.download(
    File.join(@endpoint[:zip], source),
    filename,
    SudachiInstaller.config.dict_dir
  )
end

#fetchstring

Returns:

  • (string)


25
26
27
# File 'lib/sudachi-installer/dicts.rb', line 25

def fetch
  @http.get(@endpoint[:index]).body
end

#index(document = fetch) ⇒ Array

parse xml Contents/ 以下

Parameters:

  • document (String) (defaults to: fetch)

Returns:

  • (Array)


71
72
73
74
75
76
77
78
79
80
# File 'lib/sudachi-installer/dicts.rb', line 71

def index(document = fetch)
  if !@index
    doc = REXML::Document.new(document)
    @index = REXML::XPath.match(doc, "//Contents").map { |content|
      XmlHasher.parse(content.to_s)[:Contents]
    }
  end

  @index
end

#prefixesArray

Returns:

  • (Array)


32
33
34
# File 'lib/sudachi-installer/dicts.rb', line 32

def prefixes
  [:dict, :synonym]
end

#re_template(type) ⇒ RegExp

Parameters:

  • type (Symbol)

Returns:

  • (RegExp)


40
41
42
43
44
45
46
47
# File 'lib/sudachi-installer/dicts.rb', line 40

def re_template(type)
  case type
  when :dict
    %r{\Asudachidict/sudachi-dictionary-(latest|[0-9.]+)-(core|full|small)\.zip\z}
  when :synonym
    %r{\Asudachisynonym/sudachi-synonym-(latest|[0-9.]+)\.zip\z}
  end
end

#revisions(type) ⇒ Array

Parameters:

  • type (Symbol)

Returns:

  • (Array)


86
87
88
89
90
91
92
# File 'lib/sudachi-installer/dicts.rb', line 86

def revisions(type)
  index.map { |e|
    re_template(type).match(e[:Key])
  }.compact.map { |m|
    m.to_a[1..]
  }
end

#url(type, param1, ...) ⇒ String

Parameters:

  • type (Symbol)
  • param1 (string)
  • ... (string)

Returns:

  • (String)


56
57
58
59
60
61
62
63
# File 'lib/sudachi-installer/dicts.rb', line 56

def url(type, *param)
  case type
  when :dict
    "sudachidict/sudachi-dictionary-#{param[0]}-#{param[1]}.zip"
  when :synonym
    "sudachisynonym/sudachi-synonym-#{param[0]}.zip"
  end
end