Sha256: e5e1e62273d8275a186179998be1d765d30edfadb19c0e30a355561cb416ee1e

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

require 'restclient'
require 'multi_json'
require 'addressable/uri'

module Yammer
  class HttpAdapter

    attr_reader :site_url, :connection_options

    def initialize(site_url, opts={})
      unless site_url =~ /^https?/
        raise ArgumentError, "site_url must include either http or https scheme"
      end
      @site_url = site_url
      @connection_options = opts
    end

    # set the url to be used for creating an http connection
    # @param url [string]
    def site_url=(url)
      @site_url = url
      @host     = nil
      @scheme   = nil
    end

    def host
      @host ||= parsed_url.host
    end

    def scheme
      @scheme ||= parsed_url.scheme
    end

    def absolute_url(path='')
      "#{@site_url}#{path}"
    end

    def connection_options=(opts)
      raise ArgumentError, 'expected Hash' unless opts.is_a?(Hash)
      @connection_options = opts
    end

    def send_request(method, path, opts={})
      params  = opts.fetch(:params, {})

      req_opts = self.connection_options.merge({
        :method  => method,
        :headers => opts.fetch(:headers, {})
      })

      case method
      when :get, :delete
        query = Addressable::URI.form_encode(params)
        normalized_path = query.empty? ? path : [path, query].join("?")
        req_opts[:url]  = absolute_url(normalized_path)
      when :post, :put
        req_opts[:payload] = params
        req_opts[:url]     = absolute_url(path)
      else
        raise "Unsupported HTTP method, #{method}"
      end

      resp = RestClient::Request.execute(req_opts)

      result = Yammer::ApiResponse.new(resp.headers, resp.body, resp.code)
    end

  private
    def parsed_url
      Addressable::URI.parse(@site_url)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yammer-client-0.1.7 lib/yammer/http_adapter.rb
yammer-client-0.1.6 lib/yammer/http_adapter.rb
yammer-client-0.1.5 lib/yammer/http_adapter.rb
yammer-client-0.1.4 lib/yammer/http_adapter.rb
yammer-client-0.1.3 lib/yammer/http_adapter.rb