Sha256: 087c67435b4642abfd55eca5241cae048bfb0ec676003613ee6d75aaed87ab7f

Contents?: true

Size: 1 KB

Versions: 14

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

require 'open-uri'
require 'json'
require 'yaml'
require 'active_support/all'
require_relative '../lib/komeda'

class DataUpdater
  API_ENDPOINT = 'https://eu.komeda.co.jp/v1/hp/menu'
  LARGE_TYPES_MAP = {
    '1-2' => 'drinks',
    '1-3' => 'desserts',
    '1-4' => 'foods'
  }.freeze

  class << self
    def run
      data = fetch_data
      menus = generate_menu_data(data)

      File.write(Komeda::MENUS_FILE_PATH, menus.to_yaml)
    end

    private

    def fetch_data
      response = URI.parse(API_ENDPOINT).open
      JSON.parse(response.read)['menus']
    end

    def generate_menu_data(data)
      initial_hash = Hash.new { |h, k| h[k] = [] }
      data.sort_by { _1['id'] }.each_with_object(initial_hash) do |row, hash|
        category_name = LARGE_TYPES_MAP[row['large_type']]

        next if category_name.nil?

        item = row.slice('name', 'description')

        hash['all'].push(item)
        hash[category_name].push(item)
      end
    end
  end
end

DataUpdater.run

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
komeda-0.1.14 script/update_menu_data.rb
komeda-0.1.13 script/update_menu_data.rb
komeda-0.1.12 script/update_menu_data.rb
komeda-0.1.10 script/update_menu_data.rb
komeda-0.1.9 script/update_menu_data.rb
komeda-0.1.8 script/update_menu_data.rb
komeda-0.1.7 script/update_menu_data.rb
komeda-0.1.6 script/update_menu_data.rb
komeda-0.1.5 script/update_menu_data.rb
komeda-0.1.4 script/update_menu_data.rb
komeda-0.1.3 script/update_menu_data.rb
komeda-0.1.2 script/update_menu_data.rb
komeda-0.1.1 script/update_menu_data.rb
komeda-0.1.0 script/update_menu_data.rb