module DebounceIo::Client

Constants

API_KEY
BASE_ENDPOINT

Public Instance Methods

get(path: '', params: {}) click to toggle source
# File lib/debounce_io/client.rb, line 13
def get(path: '', params: {})
  build_request_uri(path, params)
    .then { |uri| submit(uri) }
    .then { |response| decode(response.body) }
    .then { |response| validate(response) }
end

Private Instance Methods

build_request_uri(path, params) click to toggle source
# File lib/debounce_io/client.rb, line 22
def build_request_uri(path, params)
  uri = URI.parse(BASE_ENDPOINT + path)
  uri.query = URI.encode_www_form(params.merge(api: API_KEY))
  uri
end
decode(response) click to toggle source
# File lib/debounce_io/client.rb, line 35
def decode(response)
  JSON.parse(response)
rescue JSON::ParserError
  raise Error::Parsing
end
parse_error(error_hash) click to toggle source
# File lib/debounce_io/client.rb, line 49
def parse_error(error_hash)
  case error_hash['code']
  when '0' then raise Error::WrongCredentials, "DebounceIo error: #{error_hash['error']}"
  else raise Error::API, error_hash['error']
  end
end
submit(uri) click to toggle source
# File lib/debounce_io/client.rb, line 28
def submit(uri)
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    request = Net::HTTP::Get.new(uri)
    http.request(request)
  end
end
validate(response) click to toggle source
# File lib/debounce_io/client.rb, line 41
def validate(response)
  case response['success']
  when '0' then parse_error(response['debounce'])
  when '1' then response['debounce']
  when nil then response
  end
end