Sha256: 3d69184970eb4c839a5c9e76f8ac7d523651d5e7c8a0f049b1d5041628fb517a

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'typhoeus'
require 'json'
require 'open-uri'
require 'uri'

class Firebase
  class Request

    attr_accessor :base_uri, :auth

    def initialize(base_uri, auth=nil)
      @base_uri = base_uri
      @auth = auth
    end

    def get(path)
      process(:get, path)
    end

    def put(path, value)
      process(:put, path, :body => value.to_json)
    end

    def post(path, value)
      process(:post, path, :body => value.to_json)
    end

    def delete(path)
      process(:delete, path)
    end

    def patch(path, value)
      process(:patch, path, :body => value.to_json)
    end

    def build_url(path)
      path = "#{path}.json"
      query_string = auth ? "?auth=#{auth}" : ""
      url = URI.join(base_uri, path, query_string)

      url.to_s
    end

    private

    def process(method, path, options={})
      @@hydra ||= Typhoeus::Hydra.new
      request = Typhoeus::Request.new(build_url(path),
                                      :body => options[:body],
                                      :method => method)
      @@hydra.queue(request)
      @@hydra.run

      Firebase::Response.new(request.response)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
firebase-0.1.6 lib/firebase/request.rb