Class: Mooncats::GraphQL::Client
- Inherits:
-
Object
- Object
- Mooncats::GraphQL::Client
- 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
- #query(query, includes: []) ⇒ Object
- #query_bestsellers(first: 100, includes: []) ⇒ Object
- #query_cat_by_id(id:, includes: []) ⇒ Object
- #query_cat_by_wrapper_id(id:, includes: []) ⇒ Object
- #query_cats(first: 1000, last_id: '0x00', includes: []) ⇒ Object
- #query_cheap_mints_2017(first: 100, includes: []) ⇒ Object
- #query_latest_adoptions(first: 100, includes: []) ⇒ Object (also: #query_recent_adoptions)
- #query_latest_donations(first: 100, includes: []) ⇒ Object (also: #query_recent_donations)
- #query_mint_2017(first: 100, last_rescue_index: -1,, includes: []) ⇒ Object
- #query_top_collectors(first: 10, includes: []) ⇒ Object
- #query_wrapped(first: 100, last_rescue_index: -1,, includes: []) ⇒ Object
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 = " {\n cats(first: $first, orderBy: maxAdoptionPrice, orderDirection: desc) {\n id\n rescueIndex\n rescueBlock\n rescueTime\n name\n isGenesis\n maxAdoptionPrice\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice etc.\n }\n }\n" 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 = " {\n cat(id: $id) {\n id\n rescueIndex\n rescueTime\n name\n isGenesis\n maxAdoptionPrice\n wrapperTokenId\n owner {\n id\n }\n bid {\n price\n }\n ask {\n price\n }\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = " {\n cats(where: {wrapperTokenId: $wrapper_token_id}) {\n id\n rescueIndex\n rescueTime\n name\n isGenesis\n maxAdoptionPrice\n wrapperTokenId\n owner {\n id\n }\n bid {\n price\n }\n ask {\n price\n }\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = " {\n cats(first: $first, ## note: max is 1000\n where: { id_gt: $last_id }) {\n id\n rescueIndex\n rescueBlock\n rescueTime\n name\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = " {\n cats(first: $first, orderBy: askPrice,\n where: {rescueTime_lt: 1514764800, askPrice_gt: 0}) {\nid\nname\nrescueIndex\nrescueTime\naskPrice\n# @INCLUDES - lets you add more fields e.g. maxAdoptionPrice etc.\n }\n}\n" 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 ( first: 100, includes: [] ) query = " {\n adoptions(first: $first, orderBy: time, orderDirection: desc,\n where: {price_gt: 0}) {\n cat {\n id\n rescueIndex\n isGenesis\n }\n time\n price\n to {\n id\n }\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = " {\n adoptions(first: $first, orderBy: time, orderDirection: desc,\n where: {price: 0, isWrapping: false}) {\n cat {\n id\n rescueIndex\n isGenesis\n }\n time\n to {\n id\n }\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = " {\n cats(first: $first, ## note: max is 1000\n orderBy: rescueIndex,\n where:\n { rescueTime_lt: 1514764800,\n rescueIndex_gt: $last_rescue_index ## for paging in batches of 1000\n }) {\n id\n rescueIndex\n rescueBlock\n rescueTime\n name\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n }\n }\n" 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 = "{\nowners(first: $first, orderBy: catCount, orderDirection: desc,\n where: {isWrapper: false}) {\n id\n catCount\n}\n}\n" 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 = "{\ncats(first: $first, orderBy: rescueIndex,\n where:\n { wrapperTokenId_not: null,\n rescueIndex_gt: $last_rescue_index ## for paging in batches of 1000\n }) {\n id\n rescueIndex\n isGenesis\n name\n wrapperTokenId\n wrapperOwner {\n id\n }\n # @INCLUDES - lets you add more fields e.g. maxAdoptionPrice, askPrice, etc.\n}\n}\n" 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 |