bearcat 1.5.36 → 1.5.38

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c1aa4cc552b9089c7aeba0bac9d8c6e6607f249397e0d4e3b9351346dc467b9
4
- data.tar.gz: 467d992e8040ea5f79a89fda52bdce0ed944efd59fff85df165aa48c65604f44
3
+ metadata.gz: 93e58f81247055b990fa17d5d98f823c253a28c383e26a8bb5e908befb5d845d
4
+ data.tar.gz: a1179323c73208512485325e1c4263e4e49bb9e3c7d9682c5147e3a3d9fc32aa
5
5
  SHA512:
6
- metadata.gz: 8394cf85848a727ebd888c20d76ad27f5d6335556eb949a1763b6ab584e746e9e3daf4195412c863333ae54d8d6abb792efe7b26ab79b3e0bf263972d3081f71
7
- data.tar.gz: 04d083c01d107b8d0b71fe32e42a94e832c8c72b5ef48ff3b7958b6713c56b1b1d683626032557541dafd9995f8ce3c6278863aeb380e13422e921345881322d
6
+ metadata.gz: d26752bb5749819f4aedf02873ee118834b6bc69aaaec52e8ae8281dc9fe239e4947ae401a9022850ba21f2c06d7e53f1907a8cc90938b4602af42918eda89e1
7
+ data.tar.gz: ba0107db287c5962e3d3291b2b8dc0695887ce74d14f4217c2956e5d648c85219249564db5fba43d7b347e238d1d654001a5e1574b5cc0e15e6aa797af9235e0
@@ -1,17 +1,67 @@
1
1
  module Bearcat
2
+ class GraphqlError < StandardError
3
+ attr_reader :response
4
+
5
+ def initialize(message, response: nil)
6
+ super(message)
7
+ @response = response
8
+ end
9
+ end
10
+
2
11
  class Client < Footrest::Client
3
- # Request body format should be: { query: "query string" }. More info in README.md
12
+ # Request body format should be:
13
+ # - { query: "query string" }
14
+ # - "query string"
15
+ # - "path/to/gql/file", may be absolute, relative to application root, or relative to `app/graphql`. `.gql` extension is optional.
4
16
  module GraphQL
5
-
6
- def graphql_query(query)
17
+ def graphql_query(query, args = {})
7
18
  if query.is_a?(String)
19
+ if /\A\w+\Z/.match?(query) && !query.include?("{")
20
+ query = query.underscore
21
+ @@gql_cache ||= {}
22
+
23
+ query = cache_on_class("gql:#{query}") do
24
+ paths = []
25
+
26
+ pn = Pathname.new(query)
27
+ if pn.absolute?
28
+ paths << query
29
+ else
30
+ paths << Rails.root.join("app", "graphql", query)
31
+ paths << Rails.root.join(query)
32
+ end
33
+
34
+ paths = paths.flat_map do |path|
35
+ [path, "#{path}.gql"]
36
+ end
37
+
38
+ query = paths.find do |path|
39
+ File.exist?(path)
40
+ end
41
+
42
+ File.read(query)
43
+ end
44
+ end
45
+
46
+ args.symbolize_keys!
47
+
48
+ query = query.gsub(/\{\{(.*?)\}\}/) do |_m|
49
+ match = Regexp.last_match
50
+ key = match[1].strip.to_sym
51
+ args[key]
52
+ end
53
+
8
54
  query = {
9
55
  query: query
10
56
  }
11
57
  end
12
- post('/api/graphql', query)
13
- end
14
58
 
59
+ result = post('/api/graphql', query)
60
+ raise GraphqlError.new("Error running GraphQL query:\n#{result["errors"].to_json}", response: result) if result["errors"].present?
61
+
62
+ # TODO: It'd be nice to unwrap and return result["data"] directly, but we want to keep backwards compatibility
63
+ result
64
+ end
15
65
  end
16
66
  end
17
67
  end
@@ -39,6 +39,19 @@ module Bearcat
39
39
  connection.builder.delete(Footrest::RaiseFootrestErrors)
40
40
  end
41
41
 
42
+ def self.cache_on_self(key, &block)
43
+ @cache ||= {}
44
+ if Rails.env.development? || Rails.env.test?
45
+ block.call
46
+ else
47
+ @cache[key] ||= block.call
48
+ end
49
+ end
50
+
51
+ def cache_on_class(key, &block)
52
+ self.class.cache_on_self(key, &block)
53
+ end
54
+
42
55
  protected
43
56
 
44
57
  def rate_limited_request
File without changes
@@ -97,10 +97,13 @@ module Bearcat::SpecHelpers
97
97
  end
98
98
  when String
99
99
  raise "Cannot use method :auto when passing string endpoint" if method == :auto
100
- { method: method, url: Regexp.escape(endpoint) }
100
+ endpoint = Regexp.escape(endpoint)
101
+ endpoint = endpoint.gsub("\\*\\*", ".+")
102
+ endpoint = endpoint.gsub("\\*", "[^/]+")
103
+ { method: method, url: endpoint }
101
104
  when Regexp
102
105
  raise "Cannot use method :auto when passing regex endpoint" if method == :auto
103
- { method: method, url: Regexp.escape(endpoint) }
106
+ { method: method, url: endpoint.to_s }
104
107
  end
105
108
  end
106
109
 
@@ -1,3 +1,3 @@
1
1
  module Bearcat
2
- VERSION = '1.5.36' unless defined?(Bearcat::VERSION)
2
+ VERSION = '1.5.38' unless defined?(Bearcat::VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bearcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.36
4
+ version: 1.5.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - Instructure CustomDev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-13 00:00:00.000000000 Z
11
+ date: 2025-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake