Class: Ethscribe::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/ethscribe/api.rb

Overview

change/rename Api to Client - why? why not?

Defined Under Namespace

Classes: Content

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Api

Returns a new instance of Api.



23
24
25
26
# File 'lib/ethscribe/api.rb', line 23

def initialize( base )
  @base          = base
  @requests      = 0  ## count requests (for delay_in_s sleeping/throttling)
end

Class Method Details

.goerliObject



7
8
9
10
# File 'lib/ethscribe/api.rb', line 7

def self.goerli
  @goerli ||= new( 'https://goerli-api.ethscriptions.com/api' )
  @goerli
end

.mainnetObject

todo: add test alias - why? why not?



13
14
15
16
# File 'lib/ethscribe/api.rb', line 13

def self.mainnet
  @mainnet ||= new( 'https://api.ethscriptions.com/api' )
  @mainnet
end

Instance Method Details

#block_statusObject

Determine if the indexer is behind

{"current_block_number":18619883,"last_imported_block":18619883,"blocks_behind":0}


215
216
217
218
219
# File 'lib/ethscribe/api.rb', line 215

def block_status
  src = "#{@base}/block_status"
  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#configObject

convenience shortcut helper



20
# File 'lib/ethscribe/api.rb', line 20

def config() Ethscribe.config; end

#ethscription(id_or_num) ⇒ Object

note: use singular (overload method by args not possible in ruby)



137
138
139
140
141
# File 'lib/ethscribe/api.rb', line 137

def ethscription( id_or_num )  
  src = "#{@base}/ethscriptions/#{id_or_num}"
  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#ethscription_data(id_or_num) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ethscribe/api.rb', line 171

def ethscription_data( id_or_num )  
   src = "#{@base}/ethscriptions/#{id_or_num}/data"
   res = get( src )
 
   content_type   = res.content_type
   content_length = res.content_length

   ## note - content_length -- returns an integer (number)
   ## puts "content_length:"
   ## print content_length.inspect
   ## print " - #{content_length.class.name}\n"

    ## fix-fix-fix
    ##  if text/json/svg type etc. 
    ##  convert to utf8 (from binary blob why? why not?) - why? why not?
    ##   or add text accessor to content?

   content = Content.new(
                  res.blob,
                  content_type,
                  content_length )
   content
end

#ethscription_exists(sha) ⇒ Object

“result”:false,“ethscription”:null



205
206
207
208
209
# File 'lib/ethscribe/api.rb', line 205

def ethscription_exists( sha )
  src = "#{@base}/ethscriptions/exists/#{sha}"
  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#ethscriptions(page: nil, per_page: nil, sort_order: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ethscribe/api.rb', line 40

def ethscriptions( page: nil, per_page: nil, sort_order: nil )  
  src = "#{@base}/ethscriptions"
  params = []
  params << ['page',       page.to_s]                    if page 
  params << ['per_page',   per_page.to_s]                if per_page 
  params << ['sort_order', sort_order.to_s.downcase ]    if sort_order
  
  if params.size > 0
       src += "?" + params.map { |key,value| "#{key}=#{URI.encode_uri_component(value)}" }.join('&')
  end

  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#ethscriptions_owned_by(address) ⇒ Object

Get ethscriptions owned by address

/ethscriptions/owned_by/:address

Query parameters: page, per_page( max 1000), sort_order (asc / desc)


116
117
118
119
120
121
# File 'lib/ethscribe/api.rb', line 116

def ethscriptions_owned_by( address )  
  src = "#{@base}/ethscriptions/owned_by/#{address}"
 
  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#get(src) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ethscribe/api.rb', line 222

def get( src )
  @requests += 1

  if @requests > 1 && config.delay_in_s
    puts "request no. #{@requests}@#{@base}; sleeping #{config.delay_in_s} sec(s)..."
    sleep( config.delay_in_s )
  end

  res = Webclient.get( src )
 
  if res.status.ok?
    res
  else
    ## todo/fix: raise exception here!!!!
    puts "!! ERROR - HTTP #{res.status.code} #{res.status.message} - failed web request >#{src}<; sorry"
    exit 1
  end
end

#newer_ethscriptions(new_block_number, max_ethscriptions: 2500, max_blocks: 10000, mimetypes: [], count: 0) ⇒ Object

add “undocumented” endpoint

 /ethscriptions/newer_ethscriptions

see https://github.com/0xFacet/facet-vm/blob/main/app/models/ethscription_sync.rb


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ethscribe/api.rb', line 80

def newer_ethscriptions( new_block_number, 
                         max_ethscriptions: 2500,
                         max_blocks: 10000,
                         mimetypes: [],
                         count: 0
                       )
  src = "#{@base}/ethscriptions/newer_ethscriptions"

  params = []
  params << ['block_number', new_block_number.to_s]

  ## note: array MUST use rails convention with []
  mimetypes.each do |mimetype|
    params << ['mimetypes[]', mimetype]
  end

  params << ['max_ethscriptions',  max_ethscriptions.to_s] 
  params << ['max_blocks', max_blocks.to_s ] 
  params << ['past_ethscriptions_count', count.to_s]
 
  src += "?" + params.map do |key,value| 
                   "#{URI.encode_uri_component(key)}=#{URI.encode_uri_component(value)}" 
                end.join('&')

  res = get( src )
  res.json   ## return parsed json data - why? why not?
end

#newer_facet_txs(new_block_number, max: 2500, max_blocks: 10000, count: 0) ⇒ Object Also known as: newer_facet_txns

ContractTransaction.transaction_mimetype

  "application/vnd.facet.tx+json"
SystemConfigVersion.system_mimetype
  "application/vnd.facet.system+json"


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ethscribe/api.rb', line 61

def newer_facet_txs( new_block_number, 
                     max: 2500,
                     max_blocks: 10000,
                     count: 0 )
  newer_ethscriptions( new_block_number, 
                         max_ethscriptions: max,
                         max_blocks: max_blocks,
                         mimetypes: ['application/vnd.facet.tx+json',
                                     'application/vnd.facet.system+json'],
                         count: count
                       )
end