Sha256: cfaffd3c1ca33a0b0df23782bf0eae5cd0e6fd03f2b487ef0d61230fc691ba88

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 Bytes

Contents

require 'uri'
require 'json'
require 'net/http'

module Activite
  module HTTP
    BASE_URI = 'https://wbsapi.withings.net'

    class Request
      def initialize(access_token, headers)
        @access_token = access_token
        @headers = headers
      end

      def get(path, options = {})
        uri = "#{BASE_URI}#{path}?#{hash_to_query(options)}"
        response = @access_token.get(uri, @headers)
        if response.code.to_i < 200 or response.code.to_i >= 400
          raise Activite::Error::ClientConfigurationError, response.body
        end
        body = JSON.parse(response.body)
        if body['status'].to_i != 0
          raise Activite::Error::InvalidResponseError, "#{body['status']} - #{body['error']}"
        end
        body['body']
      end
      
      protected

      def hash_to_query(hash)
        return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activite-0.1.0 lib/activite/http/request.rb