class Tierion::HashApi::Client
Attributes
hash_items[RW]
debug_output $stdout
Public Class Methods
new(uname = ENV['TIERION_USERNAME'], pwd = ENV['TIERION_PASSWORD'])
click to toggle source
# File lib/tierion/hash_api/client.rb, line 13 def initialize(uname = ENV['TIERION_USERNAME'], pwd = ENV['TIERION_PASSWORD']) @auth = { username: uname, password: pwd } @access_token = nil @expires_at = Time.now.utc - 1 @refresh_token = nil @hash_items = [] auth end
Public Instance Methods
auth()
click to toggle source
# File lib/tierion/hash_api/client.rb, line 22 def auth options = { body: @auth } response = self.class.post('/auth/token', options) if response.success? extract_auth_tokens(response) else raise_error(response) end end
auth_refresh()
click to toggle source
# File lib/tierion/hash_api/client.rb, line 33 def auth_refresh if expired_auth? options = { body: { 'refreshToken' => @refresh_token } } response = self.class.post('/auth/refresh', options) if response.success? extract_auth_tokens(response) else raise_error(response) end else auth end end
create_block_subscription(callback_url, label = nil)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 158 def create_block_subscription(callback_url, label = nil) auth_refresh unless logged_in? options = { body: { 'callbackUrl' => callback_url }, headers: { 'Authorization' => "Bearer #{@access_token}" } } options[:body]['label'] = label unless label.blank? response = self.class.post('/blocksubscriptions', options) if response.success? parsed = response.parsed_response Hashie.symbolize_keys!(parsed) return parsed else raise_error(response) end end
delete_block_subscription(id)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 199 def delete_block_subscription(id) auth_refresh unless logged_in? options = { headers: { 'Authorization' => "Bearer #{@access_token}" } } response = self.class.delete("/blocksubscriptions/#{id}", options) if response.success? parsed = response.parsed_response Hashie.symbolize_keys!(parsed) return parsed else raise_error(response) end end
get_block_subscription(id)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 142 def get_block_subscription(id) auth_refresh unless logged_in? options = { headers: { 'Authorization' => "Bearer #{@access_token}" } } response = self.class.get("/blocksubscriptions/#{id}", options) if response.success? parsed = response.parsed_response Hashie.symbolize_keys!(parsed) return parsed else raise_error(response) end end
get_block_subscriptions()
click to toggle source
Returns a hash of all subscriptions, with :id, :callbackUrl, :label
# File lib/tierion/hash_api/client.rb, line 126 def get_block_subscriptions auth_refresh unless logged_in? options = { headers: { 'Authorization' => "Bearer #{@access_token}" } } response = self.class.get("/blocksubscriptions", options) if response.success? parsed = response.parsed_response parsed = parsed.collect{ |s| Hashie.symbolize_keys!(s) } return parsed else raise_error(response) end end
logged_in?()
click to toggle source
# File lib/tierion/hash_api/client.rb, line 119 def logged_in? @access_token.present? && @refresh_token.present? && @expires_at >= Time.now.utc end
receipt(h)
click to toggle source
Retrieve the receipt for a specific HashItem
# File lib/tierion/hash_api/client.rb, line 84 def receipt(h) unless h.is_a?(Tierion::HashApi::HashItem) raise ArgumentError, 'is not a Tierion::HashApi::HashItem object' end auth_refresh unless logged_in? options = { headers: { 'Authorization' => "Bearer #{@access_token}" } } response = self.class.get("/receipts/#{h.id}", options) if response.success? && response.parsed_response['receipt'].present? receipt = JSON.parse(response.parsed_response['receipt']) Hashie.symbolize_keys!(receipt) if receipt.key?(:type) || receipt.key?('@type') r = Tierion::HashApi::Receipt.new(receipt) if h.hash == r.targetHash && r.valid? return r else raise 'Invalid Receipt. Merkle tree proof validation failed.' end else raise 'Invalid Receipt. Missing type key. Old chainpoint?' end else return nil end end
receipt_from_id_and_hash(id, hash)
click to toggle source
Retrieve a receipt from its HashItem#id and the original SHA256 hash used to create that HashItem
.
# File lib/tierion/hash_api/client.rb, line 114 def receipt_from_id_and_hash(id, hash) hi = Tierion::HashApi::HashItem.new(id: id, hash: hash, timestamp: Time.now.utc.to_i) receipt(hi) end
receipts()
click to toggle source
send(hash)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 48 def send(hash) unless hash =~ /^[a-f0-9]{64}$/ raise ArgumentError, 'is not a valid SHA256 hex hash string' end auth_refresh unless logged_in? options = { body: { 'hash' => hash }, headers: { 'Authorization' => "Bearer #{@access_token}" } } response = self.class.post('/hashitems', options) if response.success? parsed = response.parsed_response Hashie.symbolize_keys!(parsed) parsed.merge!({hash: hash}) h = Tierion::HashApi::HashItem.new(parsed) @hash_items << h return h else raise_error(response) end end
update_block_subscription(id, callback_url = nil, label = nil)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 176 def update_block_subscription(id, callback_url = nil, label = nil) if callback_url.blank? && label.blank? raise ArgumentError, 'callback_url and label cannot both be blank' end auth_refresh unless logged_in? options = { body: {}, headers: { 'Authorization' => "Bearer #{@access_token}" } } options[:body].merge!({ 'callbackUrl' => callback_url }) if callback_url.present? options[:body].merge!({ 'label' => label }) if label.present? response = self.class.put("/blocksubscriptions/#{id}", options) if response.success? parsed = response.parsed_response Hashie.symbolize_keys!(parsed) return parsed else raise_error(response) end end
Private Instance Methods
expired_auth?()
click to toggle source
# File lib/tierion/hash_api/client.rb, line 226 def expired_auth? @access_token.present? && @refresh_token.present? && @expires_at < Time.now.utc end
extract_auth_tokens(resp)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 232 def extract_auth_tokens(resp) if resp && resp.parsed_response && resp.parsed_response.is_a?(Hash) && resp.parsed_response.key?('access_token') && resp.parsed_response.key?('refresh_token') && resp.parsed_response.key?('expires_in') @access_token = resp.parsed_response['access_token'] @refresh_token = resp.parsed_response['refresh_token'] @expires_at = Time.now.utc + resp.parsed_response['expires_in'] return true end end
raise_error(response)
click to toggle source
# File lib/tierion/hash_api/client.rb, line 217 def raise_error(response) if response['error'].present? raise response['error'] else raise 'Unknown Fatal Error' end end