Class: Yammer::HttpAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/yammer/http_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_url, opts = {}) ⇒ HttpAdapter

Returns a new instance of HttpAdapter.



28
29
30
31
32
33
34
# File 'lib/yammer/http_adapter.rb', line 28

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

Instance Attribute Details

#connection_optionsObject

Returns the value of attribute connection_options.



26
27
28
# File 'lib/yammer/http_adapter.rb', line 26

def connection_options
  @connection_options
end

#site_urlObject

Returns the value of attribute site_url.



26
27
28
# File 'lib/yammer/http_adapter.rb', line 26

def site_url
  @site_url
end

Class Method Details

.log=(output) ⇒ Object



22
23
24
# File 'lib/yammer/http_adapter.rb', line 22

def self.log=(output)
  RestClient.log = output
end

Instance Method Details

#absolute_url(path = '') ⇒ Object



52
53
54
# File 'lib/yammer/http_adapter.rb', line 52

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

#hostObject



44
45
46
# File 'lib/yammer/http_adapter.rb', line 44

def host
  @host ||= parsed_url.host
end

#schemeObject



48
49
50
# File 'lib/yammer/http_adapter.rb', line 48

def scheme
  @scheme ||= parsed_url.scheme
end

#send_request(method, path, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/yammer/http_adapter.rb', line 61

def send_request(method, path, opts={})
  begin
    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)
  rescue => e
    if e.is_a?(RestClient::ExceptionWithResponse)
      e.response
    else
      raise e
    end
  end
end