dhs 1.2.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca31fdaf95b8fbff1a8de50591e9861554d00fed99645e79f01cd2363de9e8b7
4
- data.tar.gz: 06d8d350c5da6878fd6c54a76826bb56c4fb506f6bc8c9be732743014b875f50
3
+ metadata.gz: 1ed15754a20492dd70e8d58fba832f6db2c747d4f13d04ad23457ab72fd81eda
4
+ data.tar.gz: 2626f410568a801f540f592e32c8f2f6ca5f9c21fa9e1a74229d9f0866139f21
5
5
  SHA512:
6
- metadata.gz: 00035a95ea3b634fc97f6af5c7592b32e09a7135a371d29e234c54ee240da99b1b71ce8305b6342aa61b7fb9103c26576a6e729cfead930d7754745d106b5bda
7
- data.tar.gz: 8f844da13a8f5d011b6179310b5ac1629853f3be24496eeba52b425412a5d7159fd2cb89744953afa2a0d615ef696c04993e3b3f921891bac060bd8d1702d63d
6
+ metadata.gz: 86b1ecd19fe674a0464ad2032337bd5801c9a4a6ef5209515e11b4f38b16d14a896f1b577e0cd37d2399159a7bb7c7a1cb2b0c2cd0d4dac1925616940d22bcb1
7
+ data.tar.gz: a663b02f76a6571a9f50ee00af2268be77c16d8c7606d01391e2d36c20d5e81fd0ba66b38f71226c9f643a403d8bd84b5162fa581447c68713c2a1748be6b324
data/README.md CHANGED
@@ -155,6 +155,107 @@ GET https://service.example.com/records
155
155
 
156
156
  **Be aware that, if you configure ambigious endpoints accross multiple classes, the order of things is not deteministic. Ambigious endpoints accross multiple classes need to be avoided.**
157
157
 
158
+ #### GraphQL Endpoints
159
+
160
+ You can use DHS also to fetch records from GraphQL Endpoints:
161
+
162
+ ```ruby
163
+ # app/models/record.rb
164
+
165
+ class Record < DHS::Record
166
+
167
+ configuration items_keys: [:data, :ethereum, :address, 0, :balances]
168
+
169
+ endpoint 'https://graphql.bitquery.io/',
170
+ graphql: {
171
+ query: %Q{
172
+ query ($network: EthereumNetwork!, $address: String!) {
173
+ ethereum(network: $network) {
174
+ address(address: {is: $address}) {
175
+ balances {
176
+ currency {
177
+ address
178
+ name
179
+ symbol
180
+ decimals
181
+ tokenType
182
+ }
183
+ value
184
+ }
185
+ }
186
+ }
187
+ }
188
+ },
189
+ variables: [:network, :address]
190
+ }
191
+
192
+ end
193
+ ```
194
+
195
+ ```ruby
196
+ # app/controllers/some_controller.rb
197
+
198
+ records = Record.where(network: 'ethereum', address: '0x317D875cA3B9f8d14f960486C0d1D1913be74e90')
199
+ ```
200
+
201
+ ```
202
+ POST https://graphql.bitquery.io/
203
+
204
+ BODY
205
+ {
206
+ "query": "
207
+ query ($network: EthereumNetwork!, $address: String!) {
208
+ ethereum(network: $network) {
209
+ address(address: {is: $address}) {
210
+ balances {
211
+ currency {
212
+ address
213
+ name
214
+ symbol
215
+ decimals
216
+ tokenType
217
+ }
218
+ value
219
+ }
220
+ }
221
+ }
222
+ }
223
+ ",
224
+ "variables": {
225
+ "network": "ethereum",
226
+ "address": "0x317D875cA3B9f8d14f960486C0d1D1913be74e90"
227
+ }
228
+ }
229
+
230
+ RESPONSE
231
+ "data": {
232
+ "ethereum": {
233
+ "address": [
234
+ {
235
+ balances: [
236
+ {
237
+ "currency": {
238
+ "address": "-",
239
+ "name": "Ether",
240
+ "decimals": 18,
241
+ "symbol": "ETH",
242
+ "tokenType": ""
243
+ },
244
+ "value": 0.11741978
245
+ }
246
+ ]
247
+ }
248
+ ]
249
+ }
250
+ }
251
+ ```
252
+
253
+ ```ruby
254
+ # app/controllers/some_controller.rb
255
+
256
+ records.first.currency.name # Ethereum
257
+ ```
258
+
158
259
  ### Provider
159
260
 
160
261
  Providers in DHS allow you to group shared endpoint options under a common provider.
@@ -2851,4 +2952,3 @@ expect(
2851
2952
  ## License
2852
2953
 
2853
2954
  [GNU General Public License Version 3.](https://www.gnu.org/licenses/gpl-3.0.en.html)
2854
-
data/dhs.gemspec CHANGED
@@ -24,14 +24,14 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_dependency 'activemodel'
26
26
  s.add_dependency 'activesupport', '>= 6'
27
- s.add_dependency 'dhc'
27
+ s.add_dependency 'dhc', '>= 2'
28
28
  s.add_dependency 'local_uri'
29
29
 
30
30
  s.add_development_dependency 'capybara'
31
31
  s.add_development_dependency 'json', '>= 1.8.2'
32
32
  s.add_development_dependency 'pry'
33
33
  s.add_development_dependency 'pry-byebug'
34
- s.add_development_dependency 'rails', '>= 6'
34
+ s.add_development_dependency 'rails', '>= 6', '< 7'
35
35
  s.add_development_dependency 'rollbar', '<= 2.24.0'
36
36
  s.add_development_dependency 'rspec-rails', '>= 3.7.0'
37
37
  s.add_development_dependency 'rubocop'
@@ -469,6 +469,7 @@ class DHS::Record
469
469
  options = (provider_options || {})
470
470
  .deep_merge(endpoint.options || {})
471
471
  .deep_merge(options)
472
+ set_graphql_options!(options) if options.dig(:graphql).present?
472
473
  options[:url] = compute_url!(options[:params]) unless options.key?(:url)
473
474
  merge_explicit_params!(options[:params])
474
475
  options.delete(:params) if options[:params]&.empty?
@@ -476,6 +477,19 @@ class DHS::Record
476
477
  options
477
478
  end
478
479
 
480
+ def set_graphql_options!(options)
481
+ options[:method] = :post
482
+ variables = {}
483
+ options.dig(:graphql, :variables).each do |key|
484
+ variables[key] = options[:params][key]
485
+ options[:params].delete(key)
486
+ end
487
+ options[:body] = {
488
+ query: options.dig(:graphql, :query).squish,
489
+ variables: variables.to_json
490
+ }
491
+ end
492
+
479
493
  def inject_interceptors!(options)
480
494
  if DHS.config.request_cycle_cache_enabled
481
495
  inject_interceptor!(
@@ -8,7 +8,7 @@ class DHS::Record
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  included do
11
- class <<self
11
+ class << self
12
12
  alias_method :update, :create
13
13
  alias_method :update!, :create!
14
14
  end
data/lib/dhs/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DHS
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe 'main graphql support' do
6
+ let(:network) { 'ethereum' }
7
+ let(:address) { '0x317D875cA3B9f8d14f960486C0d1D1913be74e90' }
8
+
9
+ let!(:stubbed_request) do
10
+ stub_request(:post, 'https://graphql.bitquery.io/')
11
+ .with(
12
+ body: {
13
+ query: %{
14
+ query ($network: EthereumNetwork!, $address: String!) {
15
+ ethereum(network: $network) {
16
+ address(address: {is: $address}) {
17
+ balances {
18
+ currency {
19
+ address
20
+ name
21
+ symbol
22
+ decimals
23
+ tokenType
24
+ }
25
+ value
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }.squish,
31
+ variables: {
32
+ "network": network,
33
+ "address": address
34
+ }.to_json
35
+ }.to_json
36
+ ).to_return(body: {
37
+ "data": {
38
+ "ethereum": {
39
+ "address": [
40
+ {
41
+ balances: [
42
+ {
43
+ "currency": {
44
+ "address": '-',
45
+ "name": 'Ether',
46
+ "decimals": 18,
47
+ "symbol": 'ETH',
48
+ "tokenType": ''
49
+ },
50
+ "value": 0.11741978
51
+ },
52
+ {
53
+ "currency": {
54
+ "address": '0xb63b606ac810a52cca15e44bb630fd42d8d1d83d',
55
+ "name": 'Monaco',
56
+ "decimals": 8,
57
+ "symbol": 'MCO',
58
+ "tokenType": 'ERC20'
59
+ },
60
+ "value": 0
61
+ },
62
+ {
63
+ "currency": {
64
+ "address": '0x06012c8cf97bead5deae237070f9587f8e7a266d',
65
+ "name": 'CryptoKitties',
66
+ "decimals": 0,
67
+ "symbol": 'CK',
68
+ "tokenType": 'ERC721'
69
+ },
70
+ "value": 90
71
+ },
72
+ {
73
+ "currency": {
74
+ "address": '0xdac17f958d2ee523a2206206994597c13d831ec7',
75
+ "name": 'Tether USD',
76
+ "decimals": 6,
77
+ "symbol": 'USDT',
78
+ "tokenType": 'ERC20'
79
+ },
80
+ "value": 10
81
+ }
82
+ ]
83
+ }
84
+ ]
85
+ }
86
+ }
87
+ }.to_json)
88
+ end
89
+
90
+ before do
91
+ class Record < DHS::Record
92
+
93
+ configuration items_key: [:data, :ethereum, :address, 0, :balances]
94
+
95
+ endpoint 'https://graphql.bitquery.io/',
96
+ graphql: {
97
+ query: %{
98
+ query ($network: EthereumNetwork!, $address: String!) {
99
+ ethereum(network: $network) {
100
+ address(address: {is: $address}) {
101
+ balances {
102
+ currency {
103
+ address
104
+ name
105
+ symbol
106
+ decimals
107
+ tokenType
108
+ }
109
+ value
110
+ }
111
+ }
112
+ }
113
+ }
114
+ },
115
+ variables: %i[network address]
116
+ }
117
+ end
118
+ end
119
+
120
+ it 'fetches data from graphql and converts it into DHS Record structure' do
121
+ records = Record.where(network: 'ethereum', address: '0x317D875cA3B9f8d14f960486C0d1D1913be74e90').fetch
122
+ expect(records.first.currency.name).to eq 'Ether'
123
+ end
124
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/DePayFi/dhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-06 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: local_uri
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -129,6 +129,9 @@ dependencies:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '6'
132
+ - - "<"
133
+ - !ruby/object:Gem::Version
134
+ version: '7'
132
135
  type: :development
133
136
  prerelease: false
134
137
  version_requirements: !ruby/object:Gem::Requirement
@@ -136,6 +139,9 @@ dependencies:
136
139
  - - ">="
137
140
  - !ruby/object:Gem::Version
138
141
  version: '6'
142
+ - - "<"
143
+ - !ruby/object:Gem::Version
144
+ version: '7'
139
145
  - !ruby/object:Gem::Dependency
140
146
  name: rollbar
141
147
  requirement: !ruby/object:Gem::Requirement
@@ -429,6 +435,7 @@ files:
429
435
  - spec/dummy/public/favicon.ico
430
436
  - spec/endpoint/for_url_spec.rb
431
437
  - spec/extended_rollbar_spec.rb
438
+ - spec/graphql/main_spec.rb
432
439
  - spec/item/access_errors_spec.rb
433
440
  - spec/item/accessors_spec.rb
434
441
  - spec/item/add_error_spec.rb
@@ -571,7 +578,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
571
578
  version: '0'
572
579
  requirements:
573
580
  - Ruby >= 2.7.2
574
- rubygems_version: 3.2.3
581
+ rubygems_version: 3.2.22
575
582
  signing_key:
576
583
  specification_version: 4
577
584
  summary: 'REST services accelerator: Rails gem providing an easy, active-record-like
@@ -665,6 +672,7 @@ test_files:
665
672
  - spec/dummy/public/favicon.ico
666
673
  - spec/endpoint/for_url_spec.rb
667
674
  - spec/extended_rollbar_spec.rb
675
+ - spec/graphql/main_spec.rb
668
676
  - spec/item/access_errors_spec.rb
669
677
  - spec/item/accessors_spec.rb
670
678
  - spec/item/add_error_spec.rb