Class: Multichain::Encoder
- Inherits:
-
Object
- Object
- Multichain::Encoder
- Defined in:
- lib/multichain/encoder.rb
Class Method Summary collapse
- .decode(hexified) ⇒ Object
- .dehexify(hexified) ⇒ Object
- .encode(url, headers = self.headers) ⇒ Object
- .extract(input_string) ⇒ Object
- .hash(url, headers = self.headers) ⇒ Object
- .headers ⇒ Object
- .hexify(url, headers = self.headers) ⇒ Object
- .input_string(url, headers = self.headers) ⇒ Object
- .sha256(url, headers = self.headers) ⇒ Object
- .verify(hexified) ⇒ Object
Class Method Details
.decode(hexified) ⇒ Object
56 57 58 |
# File 'lib/multichain/encoder.rb', line 56 def self.decode hexified dehexify hexified end |
.dehexify(hexified) ⇒ Object
52 53 54 |
# File 'lib/multichain/encoder.rb', line 52 def self.dehexify hexified hexified.scan(/../).map { |x| x.hex.chr }.join end |
.encode(url, headers = self.headers) ⇒ Object
48 49 50 |
# File 'lib/multichain/encoder.rb', line 48 def self.encode url, headers = self.headers hexify url, headers = self.headers end |
.extract(input_string) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/multichain/encoder.rb', line 27 def self.extract input_string parts = input_string.split '|' keys = [ :timestamp, :url, :headers, :hash ] h = {} keys.each_with_index do |k, i| h[k] = parts[i] end h end |
.hash(url, headers = self.headers) ⇒ Object
13 14 15 |
# File 'lib/multichain/encoder.rb', line 13 def self.hash url, headers = self.headers sha256 url, headers = self.headers end |
.headers ⇒ Object
3 4 5 6 7 |
# File 'lib/multichain/encoder.rb', line 3 def self.headers { 'Accept' => 'application/json' } end |
.hexify(url, headers = self.headers) ⇒ Object
44 45 46 |
# File 'lib/multichain/encoder.rb', line 44 def self.hexify url, headers = self.headers input_string(url).each_byte.map { |b| b.to_s(16) }.join end |
.input_string(url, headers = self.headers) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/multichain/encoder.rb', line 17 def self.input_string url, headers = self.headers a = [] a.push Time.new.to_i a.push url a.push headers.to_json a.push sha256(url) a.join '|' end |
.sha256(url, headers = self.headers) ⇒ Object
9 10 11 |
# File 'lib/multichain/encoder.rb', line 9 def self.sha256 url, headers = self.headers body = Digest::SHA256.hexdigest(HTTParty.get(url, headers: headers).body) end |
.verify(hexified) ⇒ Object
60 61 62 63 |
# File 'lib/multichain/encoder.rb', line 60 def self.verify hexified parts = extract dehexify hexified parts[:hash] == sha256(parts[:url]) end |