Sha256: c31b42196964b924bae82f5fe541f3111e49488be8accc5be81eb4ca091ba448

Contents?: true

Size: 1.74 KB

Versions: 14

Compression:

Stored size: 1.74 KB

Contents

require "net/http"
require "uri"
require "json"

class Importmap::Packager
  Error        = Class.new(StandardError)
  HTTPError    = Class.new(Error)
  ServiceError = Error.new(Error)

  singleton_class.attr_accessor :endpoint
  self.endpoint = URI("https://api.jspm.io/generate")

  def initialize(importmap_path = "config/importmap.rb")
    @importmap_path = importmap_path
  end

  def import(*packages, env: "production", from: "jspm")
    response = post_json({
      "install"      => Array(packages), 
      "flattenScope" => true,
      "env"          => [ "browser", "module", env ],
      "provider"     => from.to_s,
    })

    case response.code
    when "200"        then extract_parsed_imports(response)
    when "404", "401" then nil
    else                   handle_failure_response(response)
    end
  end

  def pin_for(package, url)
    %(pin "#{package}", to: "#{url}")
  end

  def packaged?(package)
    importmap.match(/^pin "#{package}".*$/)
  end

  private
    def extract_parsed_imports(response)
      JSON.parse(response.body).dig("map", "imports")
    end
      
    def handle_failure_response(response)
      if error_message = parse_service_error(response)
        raise ServiceError, error_message
      else
        raise HTTPError, "Unexpected response code (#{response.code})"
      end
    end
  
    def parse_service_error(response)
      JSON.parse(response.body.to_s)["error"]
    rescue JSON::ParserError
      nil
    end

    def post_json(body)
      Net::HTTP.post(self.class.endpoint, body.to_json, { "Content-Type" => "application/json" })
    rescue => error
      raise HTTPError, "Unexpected transport error (#{error.class}: #{error.message})"
    end

    def importmap
      @importmap ||= File.read(@importmap_path)
    end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
importmap-rails-0.7.6 lib/importmap/packager.rb
importmap-rails-0.7.5 lib/importmap/packager.rb
importmap-rails-0.7.4 lib/importmap/packager.rb
importmap-rails-0.7.3 lib/importmap/packager.rb
importmap-rails-0.7.2 lib/importmap/packager.rb
importmap-rails-0.7.1 lib/importmap/packager.rb
importmap-rails-0.7.0 lib/importmap/packager.rb
importmap-rails-0.6.3 lib/importmap/packager.rb
importmap-rails-0.6.2 lib/importmap/packager.rb
importmap-rails-0.6.1 lib/importmap/packager.rb
importmap-rails-0.6.0 lib/importmap/packager.rb
importmap-rails-0.5.3 lib/importmap/packager.rb
importmap-rails-0.5.2 lib/importmap/packager.rb
importmap-rails-0.5.1 lib/importmap/packager.rb