Class: Calldata
- Inherits:
-
Object
- Object
- Calldata
- Defined in:
- lib/calldata.rb,
lib/calldata/parser.rb,
lib/calldata/version.rb
Overview
note: change Calldata to a base class with concrete/subclasses!!
Defined Under Namespace
Constant Summary collapse
- MAJOR =
todo: namespace inside version or something - why? why not??
1
- MINOR =
0
- PATCH =
0
- VERSION =
[MAJOR,MINOR,PATCH].join('.')
Class Method Summary collapse
- .banner ⇒ Object
- .decode(hex) ⇒ Object (also: decode_hex)
- .encode(utf8) ⇒ Object (also: encode_utf8)
- .parse_data(utf8) ⇒ Object
-
.parse_hex(hex) ⇒ Object
check - use its own method (or check args to “automagically” decode_hex) - why? why not?.
- .root ⇒ Object
- .valid_data?(utf8) ⇒ Boolean (also: is_data?, data?)
- .valid_hex?(hex) ⇒ Boolean (also: is_hex?, hex?)
- .version ⇒ Object
Class Method Details
.banner ⇒ Object
12 13 14 |
# File 'lib/calldata/version.rb', line 12 def self. "calldata/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})" end |
.decode(hex) ⇒ Object Also known as: decode_hex
70 71 72 73 74 75 |
# File 'lib/calldata.rb', line 70 def self.decode( hex ) raise TypeError, "Calldata.decode - String expected; got #{hex.inspect} : #{hex.class.name}" unless hex.is_a?( String ) ## todo/check - add a regex/format check here - why? why not? ## must be string and hexchars only (0x/0X) hex_to_utf8( hex ) end |
.encode(utf8) ⇒ Object Also known as: encode_utf8
64 65 66 67 68 |
# File 'lib/calldata.rb', line 64 def self.encode( utf8 ) raise TypeError, "Calldata.encode - String expected; got #{utf8.inspect} : #{utf8.class.name}" unless utf8.is_a?( String ) ## note: no 0x upfront for now - why? why not? utf8_to_hex( utf8 ) end |
.parse_data(utf8) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/calldata/parser.rb', line 19 def self.parse_data( utf8 ) data, type = DataUri.parse( utf8 ) ## note: data, type order switched in datauris 1.0.1+ binary = if type if type.start_with?( 'image/svg+xml' ) false elsif type.start_with?( 'image/') || type.start_with?( 'application/octet-stream' ) true else false end else # no type (assume text) false end if binary blob = data.b Blob.new( blob, type ) else text = data.force_encoding( 'UTF-8' ) if type == '' ## "automagically - check if text is json or not - why? why not? if Msg.valid?( text ) Msg.parse( text ) else Text.new( text, 'text/plain' ) ## assume text/plain end else if type.start_with?( 'application/json') Msg.parse( text ) else Text.new( text, type ) end end end end |
.parse_hex(hex) ⇒ Object
check - use its own method (or check args to “automagically” decode_hex) - why? why not?
17 |
# File 'lib/calldata/parser.rb', line 17 def self.parse_hex( hex ) parse_data( decode( hex )); end |
.root ⇒ Object
16 17 18 |
# File 'lib/calldata/version.rb', line 16 def self.root File.( File.dirname(File.dirname(File.dirname(__FILE__))) ) end |
.valid_data?(utf8) ⇒ Boolean Also known as: is_data?, data?
4 5 6 |
# File 'lib/calldata/parser.rb', line 4 def self.valid_data?( utf8 ) DataUri.valid?( utf8 ) end |
.valid_hex?(hex) ⇒ Boolean Also known as: is_hex?, hex?
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/calldata.rb', line 79 def self.valid_hex?( hex ) ### cut-off optionial 0x/0X hex = hex[2..-1] if hex.start_with?( '0x' ) || hex.start_with?( '0X') ## allow 0x/0X - that is zero? as valid - why? why not? return true if hex.empty? ## note: a byte requires two hexchars (hexchars must be even!! 2,4,6,8,etc.) ## enforce this even requirement - why? why not? ## or turn a into a0 and 0 int 00 and so on - why? why not? /\A[0-9a-fA-F]+\z/.match?( hex ) ## && hex.length.even? end |
.version ⇒ Object
8 9 10 |
# File 'lib/calldata/version.rb', line 8 def self.version VERSION end |