Class: DbchainClient::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/dbchain_client/reader.rb

Constant Summary collapse

QueryRoot =
'/dbchain'

Instance Method Summary collapse

Constructor Details

#initialize(base_url, private_key, address = nil) ⇒ Reader

Returns a new instance of Reader.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dbchain_client/reader.rb', line 8

def initialize(base_url, private_key, address=nil)
  @rest_lib = DbchainClient::RestLib.new(base_url)

  if private_key.instance_of? String
    @private_key = PrivateKey.new(private_key)
  else
    @private_key = private_key
  end

  @public_key = @private_key.public_key
  @from_address = address || @public_key.address
end

Instance Method Details

#generate_access_code(time = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/dbchain_client/reader.rb', line 28

def generate_access_code(time=nil)
  encoded_public_key = Base58.binary_to_base58(@public_key.to_raw, :bitcoin)
  time ||= (Time.now.to_f * 1000).to_i.to_s
  signature = @private_key.sign(time)
  encoded_signature = Base58.binary_to_base58(signature.compact, :bitcoin)
  "#{encoded_public_key}:#{time}:#{encoded_signature}"
end

#get_row(app_code, table_name, id) ⇒ Object



21
22
23
24
25
26
# File 'lib/dbchain_client/reader.rb', line 21

def get_row(app_code, table_name, id)
  uri = uri_builder("find", app_code, table_name, id)
  response = @rest_lib.rest_get(uri)
  h = JSON.parse(response.body)
  return h['result']
end

#uri_builder(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dbchain_client/reader.rb', line 36

def uri_builder(*args)
  raise "At least one parameter is needed!" if args.size < 1
  access_token = generate_access_code
  args.insert(1, access_token)
  args.unshift(QueryRoot)
  args.join('/')
end