require 'multi_json' require 'yammer/configurable' require 'yammer/http_connection' require 'yammer/api' 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 attr_writer :connection_options attr_accessor :site_url, :http_adapter def initialize(opts={}) Yammer::Configurable.keys.each do |key| instance_variable_set(:"@#{key}", opts.fetch(key, Yammer.instance_variable_get(:"@#{key}"))) end end # @return [Hash] the options used to setup the http connection def connection_options @connection_options ||= {} end # set the url to be used for creating an http connection # @param url [string] def site_url=(url) @connection = nil @site_url = url 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 # 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::Response] # @!visibility private def request(method, path, params={}, opts={}) headers = opts.fetch(:headers, {}).merge({ 'Authorization' => "Bearer #{@access_token}" }) response = connection.send_request(method, path, { :params => params, :headers => headers }) result = Yammer::Response.new(response) status = result.code case status when 200...400 result else message = result.empty? ? '' : result.body[:response][:message] raise Yammer::Error.from_status(status).new(message) end end # returns an instance of the http adapter # if none is specified, the default is Yammer::HttpConnection # @!visibility private def connection @connection ||= @http_adapter.new(@site_url, connection_options) end end end