Module: WaffleAPI::Helpers

Included in:
Client
Defined in:
lib/waffle_api/helpers.rb

Overview

List of helpers used in WaffleAPI

Constant Summary collapse

NOT_FOUND_ERROR =
'Invalid BTC Address!'
INVALIDATION_TIME =

Seconds

30

Instance Method Summary collapse

Instance Method Details

#call_uriObject



13
14
15
# File 'lib/waffle_api/helpers.rb', line 13

def call_uri
  URI "#{WAFFLEPOOL_URL}#{WAFFLEPOOL_API_PATH}?address=#{@address}"
end

#data_recent!Object



32
33
34
# File 'lib/waffle_api/helpers.rb', line 32

def data_recent!
  @retrieved_at = Time.now
end

#data_recent?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/waffle_api/helpers.rb', line 28

def data_recent?
  @retrieved_at && @retrieved_at > Time.now - INVALIDATION_TIME
end

#request_jsonObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/waffle_api/helpers.rb', line 17

def request_json
  response = Net::HTTP.get_response call_uri
  fail Error::EndPointMoved if response.is_a? Net::HTTPNotFound

  json = JSON.parse response.body
  fail Error::AddressNotFound, @address if json['error'] == NOT_FOUND_ERROR
  fail Error::UnknownError, json['error'] unless json['error'].empty?
  json.delete 'error'
  json
end

#stats(key, force_update = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/waffle_api/helpers.rb', line 36

def stats(key, force_update = false)
  @datas ||= []

  if !data_recent? || force_update
    @datas = request_json
    data_recent!
  end

  fail Error::UnknownKey, key if @datas[key].nil?

  @datas[key]
end