lib/yammer/client.rb in yammer-client-0.1.2 vs lib/yammer/client.rb in yammer-client-0.1.3

- old
+ new

@@ -1,9 +1,8 @@ -require 'multi_json' -require 'yammer/configurable' -require 'yammer/http_connection' require 'yammer/api' +require 'yammer/configurable' +require 'yammer/http_adapter' module Yammer class Client include Yammer::Configurable include Yammer::Api::User @@ -14,32 +13,28 @@ include Yammer::Api::Topic include Yammer::Api::Network include Yammer::Api::Search include Yammer::Api::Notification include Yammer::Api::Autocomplete + include Yammer::Api::PendingAttachment + + attr_reader :site_url, :headers, :connection_options - attr_writer :connection_options - attr_accessor :site_url, :http_adapter + attr_accessor :client_id, :client_secret, :access_token def initialize(opts={}) Yammer::Configurable.keys.each do |key| - instance_variable_set(:"@#{key}", opts.fetch(key, Yammer.instance_variable_get(:"@#{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 - # @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 @@ -62,40 +57,30 @@ 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::Response] + # @return [Yammer::ApiResponse] # @!visibility private - def request(method, path, params={}, opts={}) - headers = opts.fetch(:headers, {}).merge({ - 'Authorization' => "Bearer #{@access_token}" + def request(method, path, params={}) + @headers['Authorization'] ||= "Bearer #{@access_token}" + result = http_client.send_request(method, path, { + :params => params, + :headers => @headers }) - 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) + result end end end