Module: HaasuSutoonData

Defined in:
lib/haasu_sutoon_data/version.rb,
lib/haasu_sutoon_data/file_generator.rb

Overview

get json file from Hearthstonejson.com and generate output_file

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.create_dictonary(from_cards, to_cards) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/haasu_sutoon_data/file_generator.rb', line 37

def self.create_dictonary(from_cards, to_cards)
  puts 'Starting creating card dictonary.'

  from_list = create_id_name_hash(from_cards)
  to_list = create_id_name_hash(to_cards)

  result = []

  from_list.each do |id, en_name|
    result << { 'id' => id, 'en_name' => en_name, 'ja_name' => to_list[id] }
  end

  result.sort do |a, b|
    b['en_name'].size <=> a['en_name'].size
  end
end

.create_id_name_hash(json) ⇒ Object



54
55
56
# File 'lib/haasu_sutoon_data/file_generator.rb', line 54

def self.create_id_name_hash(json)
  Hash[*json.map { |c| [c['id'], c['name']] }.flatten]
end

.fetch_card_list(lang) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/haasu_sutoon_data/file_generator.rb', line 27

def self.fetch_card_list(lang)
  puts "Starting #{lang} card data download."

  # jaJP => https://api.hearthstonejson.com/v1/latest/jaJP/cards.json
  # enUS => https://api.hearthstonejson.com/v1/latest/enUS/cards.json
  target_uri = URI("https://api.hearthstonejson.com/v1/latest/#{lang}/cards.json")

  Net::HTTP.get(target_uri)
end

.file_idObject



5
6
7
# File 'lib/haasu_sutoon_data/version.rb', line 5

def self.file_id
  "#{HaasuSutoonData::VERSION}/#{Time.now.strftime('%Y%m%d%H%M%S')}"
end

.generate_file(file_path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/haasu_sutoon_data/file_generator.rb', line 6

def self.generate_file(file_path)
  File.open(file_path, 'w') do |f|
    f.write "console.log('card data version: #{HaasuSutoonData.file_id}');\n"
    f.write "var CardList = [\n"
    translate_cards_list.each do |card|
      id = card['id']
      en_name = card['en_name'].gsub("'", "\\\\'")
      ja_name = card['ja_name'].gsub("'", "\\\\'")
      f.write "{'id':'#{id}','en_name':'#{en_name}','ja_name':'#{ja_name}'},\n"
    end
    f.write "]\n"
  end
end

.translate_cards_listObject



20
21
22
23
24
25
# File 'lib/haasu_sutoon_data/file_generator.rb', line 20

def self.translate_cards_list
  en_cards = JSON.parse(fetch_card_list('enUS'))
  ja_cards = JSON.parse(fetch_card_list('jaJP'))

  create_dictonary(en_cards, ja_cards)
end