Module: WaffleAPI::Helpers

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

Overview

List of helpers used in WaffleAPI

Constant Summary collapse

WAFFLE_POOL_HOST =
'wafflepool.com'
NOT_FOUND_ERROR =
'Invalid BTC Address!'
INVALIDATION_TIME =

Seconds

30

Instance Method Summary collapse

Instance Method Details

#call_uriObject



14
15
16
17
18
19
# File 'lib/waffle_api/helpers.rb', line 14

def call_uri
  URI(
    "#{@https_only ? 'https' : 'http'}://#{WAFFLE_POOL_HOST}" \
    "/tmp_api?address=#{@address}"
  )
end

#data_recent!Object



39
40
41
# File 'lib/waffle_api/helpers.rb', line 39

def data_recent!
  @retrieved_at = Time.now
end

#data_recent?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/waffle_api/helpers.rb', line 35

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

#request_jsonObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/waffle_api/helpers.rb', line 21

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'

  return json
rescue OpenSSL::SSL::SSLError
  raise Error::HttpsNotSupportedYet
end

#stats(key, force_update = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/waffle_api/helpers.rb', line 43

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