Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert URLS to names #130

Merged
merged 11 commits into from
Jan 4, 2020
Merged

Convert URLS to names #130

merged 11 commits into from
Jan 4, 2020

Conversation

bagelbits
Copy link
Collaborator

Overview

A redo of #125
This is a second stab at #121. I still need to fully review the output and could use some help. Also there are few files I had to skip because the either didn't have a name (Levels, Spellcasting, StartingEquipment) or didn't make sense (Test). I've also done some manual fixing as well.

Here is the silly Ruby script I wrote to generate this change. Thanks to @ogregoire for the naming convention.

# frozen_string_literal: true

require 'json'

FILENAMES = %w[
  5e-SRD-Ability-Scores.json
  5e-SRD-Classes.json
  5e-SRD-Conditions.json
  5e-SRD-Damage-Types.json
  5e-SRD-Equipment-Categories.json
  5e-SRD-Equipment.json
  5e-SRD-Features.json
  5e-SRD-Languages.json
  5e-SRD-Levels.json
  5e-SRD-Magic-Schools.json
  5e-SRD-Monsters.json
  5e-SRD-Proficiencies.json
  5e-SRD-Races.json
  5e-SRD-Skills.json
  5e-SRD-Spellcasting.json
  5e-SRD-Spells.json
  5e-SRD-StartingEquipment.json
  5e-SRD-Subclasses.json
  5e-SRD-Subraces.json
  5e-SRD-Test.json
  5e-SRD-Traits.json
  5e-SRD-Weapon-Properties.json
].freeze

FILENAMES_TO_ITERATE = %w[
  5e-SRD-Ability-Scores.json
  5e-SRD-Classes.json
  5e-SRD-Conditions.json
  5e-SRD-Damage-Types.json
  5e-SRD-Equipment-Categories.json
  5e-SRD-Equipment.json
  5e-SRD-Features.json
  5e-SRD-Languages.json
  5e-SRD-Magic-Schools.json
  5e-SRD-Monsters.json
  5e-SRD-Proficiencies.json
  5e-SRD-Races.json
  5e-SRD-Skills.json
  5e-SRD-Spells.json
  5e-SRD-Subclasses.json
  5e-SRD-Subraces.json
  5e-SRD-Weapon-Properties.json
].freeze

def convert_to_id(name)
  id = name.downcase.strip
  id = id.gsub(%r{\/}, ' ')
  id = id.gsub(/[:,\(\)]+/, '')
  id = id.gsub(/(?:(?<=\bo)'|'(?=s\b)|^\W+|\W+$)/, '')
  id.gsub(/\s+/, '-')
end

def replace_in_all(original_url, new_url)
  FILENAMES.each do |file_name|
    text = File.read(file_name)
    new_contents = text.gsub(/#{original_url}(?=")/, new_url)

    # To write changes to the file, use:
    File.open(file_name, 'w') { |file| file.puts new_contents }
  end
end

# iterate files
FILENAMES_TO_ITERATE.each do |file_name|
  puts "Current file: #{file_name}"
  file_contents = IO.readlines(file_name).join
  json = JSON.parse(file_contents)
  json.length.times do |index|
    object_index = json[index]['index']
    # puts json[index]['name']
    id = convert_to_id(json[index]['name'])
    file_contents = file_contents.gsub(/"index": #{object_index},/, "\"id\": \"#{id}\",")

    original_url = json[index]['url']
    new_url = original_url.gsub(%r{(?<=\/)#{object_index}}, id)
    file_contents = file_contents.gsub(/#{original_url}(?=")/, new_url)

    # regenerate file
    File.open(file_name, 'w') do |line|
      line.puts file_contents
    end

    # change all files
    replace_in_all(original_url, new_url)

    file_contents = IO.readlines(file_name).join
    json = JSON.parse(file_contents)
  end
end

Here's a fun image for your troubles

random photo - update me

@bagelbits bagelbits mentioned this pull request Dec 31, 2019
Copy link
Collaborator

@ogregoire ogregoire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an impressive and excellent work, again :-)

@bagelbits
Copy link
Collaborator Author

@ogregoire Since the changes we are making are now live with the API, I'm a little worried about changing the index key to id, as it will break everything. I'll probably switch the key back to index but keep the string ids for now.

@bagelbits bagelbits force-pushed the bagelbits.update-ids branch 2 times, most recently from a10aa1c to 3d71edc Compare January 2, 2020 17:52
@bagelbits
Copy link
Collaborator Author

I want to test this locally with a running API to make sure I don't just BREAK EVERYTHING.

@bagelbits
Copy link
Collaborator Author

This needs to be pushed in tandem with 5e-bits/5e-srd-api#26 so it doesn't break everything immediately.

@bagelbits
Copy link
Collaborator Author

@ogregoire I think everything is ready to go for this. Both DB and API side. But I'm terrified of pulling those levers.

@bagelbits
Copy link
Collaborator Author

https://lever.holdings

@bagelbits bagelbits merged commit c4fbd1b into master Jan 4, 2020
@bagelbits bagelbits deleted the bagelbits.update-ids branch January 4, 2020 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants