gemstar 0.0.1 → 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ module Gemstar
2
+ class RequestLogger
3
+ def initialize(app, io: $stderr)
4
+ @app = app
5
+ @io = io
6
+ end
7
+
8
+ def call(env)
9
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
10
+ status, headers, body = @app.call(env)
11
+ log_request(env, status, started_at)
12
+ [status, headers, body]
13
+ rescue StandardError => e
14
+ log_request(env, 500, started_at, error: e)
15
+ raise
16
+ end
17
+
18
+ private
19
+
20
+ def log_request(env, status, started_at, error: nil)
21
+ duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000).round(1)
22
+ path = env["PATH_INFO"].to_s
23
+ query = env["QUERY_STRING"].to_s
24
+ full_path = query.empty? ? path : "#{path}?#{query}"
25
+ method = env["REQUEST_METHOD"].to_s
26
+ suffix = error ? " #{error.class}: #{error.message}" : ""
27
+
28
+ @io.puts "[gemstar] #{method} #{full_path} -> #{status} in #{duration_ms}ms#{suffix}"
29
+ end
30
+ end
31
+ end
@@ -10,49 +10,65 @@ module Gemstar
10
10
 
11
11
  attr_reader :gem_name
12
12
 
13
- def meta
14
- @meta ||=
15
- begin
16
- url = "https://rubygems.org/api/v1/gems/#{URI.encode_www_form_component(gem_name)}.json"
17
- Cache.fetch("rubygems-#{gem_name}") do
18
- URI.open(url).read
19
- end.then { |json|
20
- begin
21
- JSON.parse(json) if json
22
- rescue
23
- nil
24
- end }
13
+ def meta(cache_only: false)
14
+ return @meta if !cache_only && defined?(@meta)
15
+
16
+ json = if cache_only
17
+ Cache.peek("rubygems-#{gem_name}")
18
+ else
19
+ url = "https://rubygems.org/api/v1/gems/#{URI.encode_www_form_component(gem_name)}.json"
20
+ Cache.fetch("rubygems-#{gem_name}") do
21
+ URI.open(url).read
25
22
  end
23
+ end
24
+
25
+ parsed = begin
26
+ JSON.parse(json) if json
27
+ rescue
28
+ nil
29
+ end
30
+
31
+ @meta = parsed unless cache_only
32
+ parsed
26
33
  end
27
34
 
28
- def repo_uri
29
- return nil unless meta
35
+ def repo_uri(cache_only: false)
36
+ resolved_meta = meta(cache_only: cache_only)
37
+ return nil unless resolved_meta
38
+
39
+ return @repo_uri if !cache_only && defined?(@repo_uri)
40
+
41
+ repo = begin
42
+ uri = resolved_meta["source_code_uri"]
43
+
44
+ if uri.nil?
45
+ uri = resolved_meta["homepage_uri"]
46
+ if uri.include?("github.com")
47
+ uri = uri[%r{http[s?]://github\.com/[^/]+/[^/]+}]
48
+ end
49
+ end
30
50
 
31
- @repo_uri ||= begin
32
- uri = meta["source_code_uri"]
51
+ uri ||= ""
33
52
 
34
- if uri.nil?
35
- uri = meta["homepage_uri"]
36
- if uri.include?("github.com")
37
- uri = uri[%r{http[s?]://github\.com/[^/]+/[^/]+}]
38
- end
39
- end
53
+ uri = uri.sub("http://", "https://")
40
54
 
41
- uri ||= ""
55
+ uri = uri.gsub(/\.git$/, "")
42
56
 
43
- uri = uri.sub("http://", "https://")
57
+ if uri.include?("github.io")
58
+ uri = uri.sub(%r{\Ahttps?://([\w-]+)\.github\.io/([^/]+)}) do
59
+ "https://github.com/#{$1}/#{$2}"
60
+ end
61
+ end
44
62
 
45
- uri = uri.gsub(/\.git$/, "")
63
+ if uri.include?("github.com")
64
+ uri = uri[%r{\Ahttps?://github\.com/[^/]+/[^/]+}] || uri
65
+ end
46
66
 
47
- if uri.include?("github.io")
48
- # Convert e.g. https://socketry.github.io/console/ to https://github.com/socketry/console/
49
- uri = uri.sub(%r{\Ahttps?://([\w-]+)\.github\.io/([^/]+)}) do
50
- "https://github.com/#{$1}/#{$2}"
51
- end
52
- end
67
+ uri
68
+ end
53
69
 
54
- uri
55
- end
70
+ @repo_uri = repo unless cache_only
71
+ repo
56
72
  end
57
73
 
58
74
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gemstar # :nodoc:
4
- VERSION = "0.0.1"
4
+ VERSION = "1.0"
5
5
 
6
6
  def self.debug?
7
7
  return @debug if defined?(@debug)