Sha256: e5c1173f3f250768a79ded971a7c0903cf635958aa1fc45785c0b7dabe3f5ae4

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'rest_client'

module Flydata
  FLYDATA_API_HOST = ENV['FLYDATA_API_HOST'] || 'flydata.co'

  class ApiClient
    attr_reader :response
    attr_accessor :credentials

    def initialize(credentials=nil)
      @credentials = credentials || Flydata::Credentials.new
    end

    # row level api
    def post(path, params=nil)
      uri = "http://#{FLYDATA_API_HOST}#{path}"
      resource = RestClient::Resource.new(uri, resource_opts)
      @response = resource.post(params, :accept => :json)
    end
    def get(path)
      uri = "http://#{FLYDATA_API_HOST}#{path}"
      resource = RestClient::Resource.new(uri, resource_opts)
      @response = resource.get(:accept => :json)
    end

    # high level api
    def method_missing(cls_name, *args, &block)
      method_name = args.shift.to_s
      api_cls = "Flydata::Api::#{cls_name.to_s.camelize}".constantize
      api_cls.new(self)
    end
    def respond_to_missing?(method_name, include_private=false); true end

    private
    def resource_opts
      {:user => @credentials.user, :password => @credentials.password}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flydata-0.0.1.2011101801 lib/flydata/api_client.rb