importmap-rails 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97b5ca5be348b06c3b3b04b5aaccc99fa5bc55a8a65c664d25e37cf6f8be2586
4
- data.tar.gz: 3c377792bec10f97d819640e801dc9a36112ef009da0902eba36a2d873d67623
3
+ metadata.gz: 80b9cda4f79989099769db06ef9eeb785a7f9906cfa9eacbd3d6bf1dcd437edd
4
+ data.tar.gz: 77972151d15109c1ee373331fa71f42e3957afb97af470fb8683f2c7e027df73
5
5
  SHA512:
6
- metadata.gz: 66c0ee6695329e2cba7e6bdf545d4415fa3bc92218fff791ec21977f53fe8294c536cf5fefba0d00c7ceb5859529e32c2cadbbad0097c21bd82efba84402a0f7
7
- data.tar.gz: 95be4c078375c1e11f7061dedc195c9bbec3f57e870104ef429e64586100f8872ba4ab94398b4b7e6003e07a77514d7a74682428b8e1b85d889bf028c43a8683
6
+ metadata.gz: 5a1c26adbcb471b81dcd1504771f36ac73dfb542a41c379dd257f01a6ec633545030ea0a4d33d49ff40fe56f30c336a0cee8e990aa58f1a3088acf7f0738c46b
7
+ data.tar.gz: d1c94f03f8c844303887d08af37028e78b667120ce7fa04cbf3c7d7c52b1f56bd10ba69ed9ce0de8ea8b6af234443f96c03c0a3da21d2e0ba301a7997b00ecd4
@@ -1,26 +1,31 @@
1
- require "httparty"
2
- require "minitest/mock"
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
3
4
 
4
5
  class Importmap::Packager
5
- include HTTParty
6
- base_uri "https://api.jspm.io"
6
+ Error = Class.new(StandardError)
7
+ HTTPError = Class.new(Error)
8
+ ServiceError = Error.new(Error)
9
+
10
+ singleton_class.attr_accessor :endpoint
11
+ self.endpoint = URI("https://api.jspm.io/generate")
7
12
 
8
13
  def initialize(importmap_path = "config/importmap.rb")
9
14
  @importmap_path = importmap_path
10
15
  end
11
16
 
12
17
  def import(*packages, env: "production", from: "jspm")
13
- response = self.class.post("/generate", body: {
18
+ response = post_json({
14
19
  "install" => Array(packages),
15
20
  "flattenScope" => true,
16
21
  "env" => [ "browser", "module", env ],
17
- "provider" => from.to_s
18
- }.to_json)
19
-
22
+ "provider" => from.to_s,
23
+ })
24
+
20
25
  case response.code
21
- when 200 then response.dig("map", "imports")
22
- when 404 then nil
23
- else response.send(:throw_exception)
26
+ when "200" then extract_parsed_imports(response)
27
+ when "404", "401" then nil
28
+ else handle_failure_response(response)
24
29
  end
25
30
  end
26
31
 
@@ -33,6 +38,30 @@ class Importmap::Packager
33
38
  end
34
39
 
35
40
  private
41
+ def extract_parsed_imports(response)
42
+ JSON.parse(response.body).dig("map", "imports")
43
+ end
44
+
45
+ def handle_failure_response(response)
46
+ if error_message = parse_service_error(response)
47
+ raise ServiceError, error_message
48
+ else
49
+ raise HTTPError, "Unexpected response code (#{response.code})"
50
+ end
51
+ end
52
+
53
+ def parse_service_error(response)
54
+ JSON.parse(response.body.to_s)["error"]
55
+ rescue JSON::ParserError
56
+ nil
57
+ end
58
+
59
+ def post_json(body)
60
+ Net::HTTP.post(self.class.endpoint, body.to_json, { "Content-Type" => "application/json" })
61
+ rescue => error
62
+ raise HTTPError, "Unexpected transport error (#{error.class}: #{error.message})"
63
+ end
64
+
36
65
  def importmap
37
66
  @importmap ||= File.read(@importmap_path)
38
67
  end
@@ -1,3 +1,3 @@
1
1
  module Importmap
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -1,7 +1,3 @@
1
- # Use Action Cable channels (remember to import "@rails/actionable" in your application.js)
2
- # pin "@rails/actioncable", to: "actioncable.esm.js"
3
- # pin_all_from "app/javascript/channels", under: "channels"
4
-
5
1
  # Use direct uploads for Active Storage (remember to import "@rails/activestorage" in your application.js)
6
2
  # pin "@rails/activestorage", to: "activestorage.esm.js"
7
3
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: importmap-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-01 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.0.0
27
- - !ruby/object:Gem::Dependency
28
- name: httparty
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0.16'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0.16'
41
27
  description:
42
28
  email: david@loudthinking.com
43
29
  executables: []