Class: OpenCoinage::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/opencoinage/token.rb

Overview

A digital currency token.

Constant Summary

RDF_TYPE =
Vocabulary[:Token]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Token) initialize(identifier, signature = nil)

Initializes a new token.

Parameters:

  • (Integer, #to_i) identifier

    a unique token identifier of arbitrary length

  • (Integer, #to_i) signature (defaults to: nil)

    the issuer's digital signature



36
37
38
39
# File 'lib/opencoinage/token.rb', line 36

def initialize(identifier, signature = nil)
  @identifier = identifier.to_i
  @signature  = signature ? signature.to_i : nil
end

Instance Attribute Details

- (Integer) identifier (readonly) Also known as: id

The token's unique identifier.

Returns:

  • (Integer)


45
46
47
# File 'lib/opencoinage/token.rb', line 45

def identifier
  @identifier
end

- (Integer) signature (readonly)

The issuer's digital signature.

Returns:

  • (Integer)


52
53
54
# File 'lib/opencoinage/token.rb', line 52

def signature
  @signature
end

Class Method Details

+ (Token) parse(input)

Parses and returns the given Base62-encoded token.

Examples:

Token.parse("deadbeef")   #=> Token.new(139648545098945, nil)
Token.parse("deadbeef:")  #=> Token.new(139648545098945, nil)
Token.parse("dead:beef")  #=> Token.new(9450823, 8974417)

Parameters:

  • (String, #to_str) input

    a Base62-based string representation of the token

Returns:

  • (Token)

    the parsed token

Raises:

  • (ArgumentError)

    if input is not a Base62 string



19
20
21
22
23
24
25
26
27
# File 'lib/opencoinage/token.rb', line 19

def self.parse(input)
  identifier, signature = (input = Bitcache.read(input).strip).split(':', 2)
  signature = nil if signature && signature.empty?
  if Base62.regexp === identifier && (signature.nil? || Base62.regexp === signature)
    self.new(Base62.decode(identifier), signature ? Base62.decode(signature) : nil)
  else
    raise ArgumentError, "expected a Base62-encoded token, but got #{input.inspect}"
  end
end

Instance Method Details

- (Boolean) signed?

Returns true if this token contains a digital signature, false otherwise.

Returns:

  • (Boolean)

    true if the token is digitally signed, false otherwise



60
61
62
# File 'lib/opencoinage/token.rb', line 60

def signed?
  !!signature
end

- (Array) to_a

Returns the array representation of this token.

Examples:

identifier, signature = token.to_a

Returns:

  • (Array)

    a two-element array containing the identifier and signature



88
89
90
# File 'lib/opencoinage/token.rb', line 88

def to_a
  [identifier, signature]
end

- (Hash) to_hash

Returns the hash table representation of this token.

Returns:

  • (Hash)


96
97
98
# File 'lib/opencoinage/token.rb', line 96

def to_hash
  {:identifier => identifier, :signature => signature}
end

- (Integer) to_i

Returns the integer representation of this token.

Returns:

  • (Integer)

    the token identifier



68
69
70
# File 'lib/opencoinage/token.rb', line 68

def to_i
  identifier
end

- (String) to_json

Returns the JSON representation of this token.



106
107
108
109
# File 'lib/opencoinage/token.rb', line 106

def to_json
  require 'json' unless defined?(::JSON)
  to_hash.to_json
end

- (RDF::Graph) to_rdf(options = {})

Returns the RDF representation of this token.

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

  • (RDF::Resource) :context — default: nil
  • (RDF::Resource) :subject — default: RDF::Node(object_id)

Returns:

  • (RDF::Graph)

See Also:



120
121
122
123
124
125
126
127
# File 'lib/opencoinage/token.rb', line 120

def to_rdf(options = {})
  RDF::Graph.new(options[:context]) do |graph|
    subject = options[:subject] || RDF::Node(object_id)
    graph << [subject, RDF.type,                self.class.const_get(:RDF_TYPE)]
    graph << [subject, Vocabulary[:identifier], identifier]
    graph << [subject, Vocabulary[:signature],  signature] if signed?
  end
end

- (String) to_s

Returns the string representation of this token.

Returns:

  • (String)

    a Base62 string



76
77
78
# File 'lib/opencoinage/token.rb', line 76

def to_s
  to_a.compact.map { |n| Base62.encode(n) }.join(':') # FIXME
end