require 'yammer/api' require 'yammer/configurable' require 'yammer/http_adapter' module Yammer class Client include Yammer::Configurable include Yammer::Api::User include Yammer::Api::Group include Yammer::Api::GroupMembership include Yammer::Api::Message include Yammer::Api::Thread include Yammer::Api::Topic include Yammer::Api::Network include Yammer::Api::Search include Yammer::Api::Notification include Yammer::Api::Autocomplete include Yammer::Api::Invitation include Yammer::Api::PendingAttachment attr_reader :site_url, :headers, :connection_options attr_accessor :client_id, :client_secret, :access_token def initialize(opts={}) Yammer::Configurable.keys.each do |key| case key when :headers, :connection_options value = Yammer.instance_variable_get(:"@#{key}").merge(opts.fetch(key, {})) else value = opts.fetch(key, Yammer.instance_variable_get(:"@#{key}")) end instance_variable_set(:"@#{key}", value) end end # (see #request) # @note makes a GET request def get(path, params={}) request(:get, path, params) end # (see #request) # @note makes a PUT request def put(path, params={}) request(:put, path, params) end # (see #request) # @note makes a POST request def post(path, params={}) request(:post, path, params) end # (see #request) # @note makes a DELETE request def delete(path, params={}) request(:delete, path, params) end private # returns an instance of the http adapter # if none is specified, the default is Yammer::HttpConnection # @!visibility private def http_client @http_client ||= @http_adapter.new(@site_url, @connection_options) end # Makes an HTTP request using the provided parameters # @raise [Yammer::Error::Unauthorized] # @param method [string] # @param path [string] # @param params [Hash] # @param opts [Hash] # @return [Yammer::ApiResponse] # @!visibility private def request(method, path, params={}) @headers['Authorization'] ||= "Bearer #{@access_token}" result = http_client.send_request(method, path, { :params => params, :headers => @headers }) result end end end