Class: Mooncats::GraphQL::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncats/graphql/query.rb

Constant Summary collapse

BASE_URL =

generic query via HTTP POST

'https://api.thegraph.com/subgraphs/name/merklejerk/moon-cats-rescue'

Instance Method Summary collapse

Instance Method Details

#query(query, includes: []) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/mooncats/graphql/query.rb', line 276

def query( query, includes: [] )
  if includes.size > 0
    ## check for end-of-line comments with @INCLUDES marker
     query = query.gsub( /[#]+[ ]+@INCLUDES[^\n\r]+/,
                           includes.join( ' ' ) )
  end

  res = Webclient.post( BASE_URL, json: {
                                    query: query } )

  if res.status.nok?   ## e.g. != 200
    puts "!! ERROR: HTTP #{res.status.code} #{res.status.message}:"
    pp res.raw   ## note: dump inner (raw) response (NOT the wrapped)
    exit 1
  end

  res.json  ## return body (utf-8 enconded text) parsed as json
end

#query_bestsellers(first: 100, includes: []) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mooncats/graphql/query.rb', line 7

def query_bestsellers( first: 100,
                       includes: [] )
  query = <<GRAPHQL
  {
    cats(first: $first, orderBy: maxAdoptionPrice, orderDirection: desc) {
      id
      rescueIndex
      rescueBlock
      rescueTime
      name
      isGenesis
      maxAdoptionPrice
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice etc.
    }
  }
GRAPHQL
  query = query.gsub( '$first',   first.to_s )

  data = query( query, includes: includes )
  data['data']['cats']   ## return nested data
end

#query_cat_by_id(id:, includes: []) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mooncats/graphql/query.rb', line 103

def query_cat_by_id( id:,
                     includes: [] )
  query = <<GRAPHQL
  {
    cat(id: $id) {
      id
      rescueIndex
      rescueTime
      name
      isGenesis
      maxAdoptionPrice
      wrapperTokenId
      owner {
        id
      }
      bid {
        price
      }
      ask {
        price
      }
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
    }
  }
GRAPHQL
  query = query.gsub( '$id',  %Q<"#{id}"> )

  data = query( query, includes: includes )
  data['data']['cat']   ## note: returns single (one-only) cat record
end

#query_cat_by_wrapper_id(id:, includes: []) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mooncats/graphql/query.rb', line 135

def query_cat_by_wrapper_id( id:,
                               includes: [] )
  query = <<GRAPHQL
  {
    cats(where: {wrapperTokenId: $wrapper_token_id}) {
      id
      rescueIndex
      rescueTime
      name
      isGenesis
      maxAdoptionPrice
      wrapperTokenId
      owner {
        id
      }
      bid {
        price
      }
      ask {
        price
      }
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
    }
  }
GRAPHQL
  query = query.gsub( '$wrapper_token_id',  id.to_s )

  data = query( query, includes: includes )
  data['data']['cats'][0]   # note: try to get first record only (by default) - why? why not?
end

#query_cats(first: 1000, last_id: '0x00', includes: []) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mooncats/graphql/query.rb', line 79

def query_cats( first: 1000,
                 last_id: '0x00',
                 includes: [] )
  query = <<GRAPHQL
  {
    cats(first: $first,   ## note: max is 1000
          where: { id_gt: $last_id }) {
      id
      rescueIndex
      rescueBlock
      rescueTime
      name
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.
    }
  }
GRAPHQL
  query = query.gsub( '$first',   first.to_s )
  query = query.gsub( '$last_id',  %Q<"#{last_id}"> )

  data = query( query, includes: includes )
  data['data']['cats']
end

#query_cheap_mints_2017(first: 100, includes: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mooncats/graphql/query.rb', line 29

def query_cheap_mints_2017( first: 100,
                            includes: [] )
  query = <<GRAPHQL
  {
  cats(first: $first, orderBy: askPrice,
   where: {rescueTime_lt: 1514764800, askPrice_gt: 0}) {
id
name
rescueIndex
rescueTime
askPrice
#  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice etc.
  }
}
GRAPHQL
  query = query.gsub( '$first',   first.to_s )

  data = query( query, includes: includes )
  data['data']['cats']   ## return nested data
end

#query_latest_adoptions(first: 100, includes: []) ⇒ Object Also known as: query_recent_adoptions



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mooncats/graphql/query.rb', line 166

def query_latest_adoptions( first: 100,
                             includes: [] )
   query = <<GRAPHQL
  {
    adoptions(first: $first, orderBy: time, orderDirection: desc,
              where: {price_gt: 0}) {
      cat {
        id
        rescueIndex
        isGenesis
      }
      time
      price
      to {
        id
      }
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
    }
  }
GRAPHQL
  query = query.gsub( '$first',  first.to_s )

  data = query( query, includes: includes )
  data['data']['adoptions']
end

#query_latest_donations(first: 100, includes: []) ⇒ Object Also known as: query_recent_donations



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/mooncats/graphql/query.rb', line 194

def query_latest_donations( first: 100,
                            includes: [] )
  query = <<GRAPHQL
  {
    adoptions(first: $first, orderBy: time, orderDirection: desc,
       where: {price: 0, isWrapping: false}) {
      cat {
        id
        rescueIndex
        isGenesis
      }
      time
      to {
        id
      }
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
    }
  }
GRAPHQL
  query = query.gsub( '$first',  first.to_s )

  data = query( query, includes: includes )
  data['data']['adoptions']
end

#query_mint_2017(first: 100, last_rescue_index: -1,, includes: []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mooncats/graphql/query.rb', line 51

def query_mint_2017( first: 100,
                     last_rescue_index: -1,
                     includes: [] )
  query = <<GRAPHQL
  {
    cats(first: $first,     ## note: max is 1000
          orderBy: rescueIndex,
          where:
          { rescueTime_lt: 1514764800,
            rescueIndex_gt: $last_rescue_index   ## for paging in batches of 1000
          }) {
      id
      rescueIndex
      rescueBlock
      rescueTime
      name
      #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
    }
  }
GRAPHQL
  query = query.gsub( '$first',              first.to_s )
  query = query.gsub( '$last_rescue_index',  last_rescue_index.to_s )

  data = query( query, includes: includes )
  data['data']['cats']
end

#query_top_collectors(first: 10, includes: []) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mooncats/graphql/query.rb', line 221

def query_top_collectors( first: 10,
                          includes: [] )
   query = <<GRAPHQL
{
owners(first: $first, orderBy: catCount, orderDirection: desc,
  where: {isWrapper: false}) {
  id
  catCount
}
}
GRAPHQL
  query = query.gsub( '$first',  first.to_s )

  data = query( query, includes: includes )
  data['data']['owners']
end

#query_wrapped(first: 100, last_rescue_index: -1,, includes: []) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/mooncats/graphql/query.rb', line 239

def query_wrapped( first: 100,
                   last_rescue_index: -1,
                   includes: [] )
query = <<GRAPHQL
{
cats(first: $first, orderBy: rescueIndex,
      where:
      { wrapperTokenId_not: null,
        rescueIndex_gt: $last_rescue_index   ## for paging in batches of 1000
       }) {
  id
  rescueIndex
  isGenesis
  name
  wrapperTokenId
  wrapperOwner {
    id
  }
  #  @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice,  etc.
}
}
GRAPHQL
  query = query.gsub( '$first',              first.to_s )
  query = query.gsub( '$last_rescue_index',  last_rescue_index.to_s )

  data = query( query, includes: includes )
  data['data']['cats']
end