Class: Tierion::HashApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tierion/hash_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uname = ENV['TIERION_USERNAME'], pwd = ENV['TIERION_PASSWORD']) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#hash_itemsObject

debug_output $stdout



11
12
13
# File 'lib/tierion/hash_api/client.rb', line 11

def hash_items
  @hash_items
end

Instance Method Details

#authObject



22
23
24
25
26
27
28
29
30
31
# 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_refreshObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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_subscriptionsObject

Returns a hash of all subscriptions, with :id, :callbackUrl, :label



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# 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) ⇒ Object

Retrieve the receipt for a specific HashItem



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object

Retrieve a receipt from its HashItem#id and the original SHA256 hash used to create that HashItem.



114
115
116
117
# 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

#receiptsObject

Get a Receipt for each HashItem that doesn’t have one and return the collection of Receipts.



74
75
76
77
78
79
80
81
# File 'lib/tierion/hash_api/client.rb', line 74

def receipts
  @hash_items.each do |h|
    next if h.receipt.present?
    h.receipt = receipt(h)
  end

  @hash_items.collect(&:receipt).compact
end

#send(hash) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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