Sha256: b63b36f8be0b1108dc6a90a2f7462acd73f0d9b695110eb88b62d9d6c446c01c

Contents?: true

Size: 1.34 KB

Versions: 94

Compression:

Stored size: 1.34 KB

Contents

CODE_LANGUAGES = ['curl', 'node', 'java', 'dotnet', 'csharp', 'php', 'python', 'ruby']

class CorrectUriOfResources < ActiveRecord::Migration[5.1]
  def up
    Feedback::Resource.all.each do |resource|
      uri = URI(resource.uri)

      # Get the code_language from the URI. Will return nil if there isn't one
      code_language = CODE_LANGUAGES.detect { |language| language == uri.path.split('/').last }

      # Get the canonical_uri by removing query parameters and then chomp the code_language off the end if present
      canonical_uri = "#{uri.scheme}://#{uri.host}#{uri.path}".chomp("/#{code_language}")

      # Check if the current uri of the resource matches our canonical_uri, if so do nothing
      unless canonical_uri == resource.uri
        # Lets see if a resource with the canonical_uri already exists
        canonical_resource = Feedback::Resource.find_by_uri(canonical_uri)

        if canonical_resource
          # If we do have a canonical_resource update this resources feedbacks to belong to it and destroy this resource
          # since it will no longer be used.
          resource.feedbacks.update_all({ resource_id: canonical_resource.id })
          resource.destroy!
        else
          # Otherwise we'll simply correct the uri of this resource
          resource.update({ uri: canonical_uri })
        end
      end
    end
  end
end

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
station-0.5.16 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.15 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.14 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.13 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.12 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.11 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.10 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.9 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.8 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.7 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.6 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.5 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.4 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.3 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.2 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.1 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.5.0 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.4.9 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.4.8 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb
station-0.4.7 lib/nexmo_developer/db/migrate/20170915081222_correct_uri_of_resources.rb