Class: Yammer::HttpAdapter
- Inherits:
-
Object
- Object
- Yammer::HttpAdapter
- Defined in:
- lib/yammer/http_adapter.rb
Instance Attribute Summary collapse
-
#connection_options ⇒ Object
Returns the value of attribute connection_options.
-
#site_url ⇒ Object
Returns the value of attribute site_url.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_url(path = '') ⇒ Object
- #host ⇒ Object
-
#initialize(site_url, opts = {}) ⇒ HttpAdapter
constructor
A new instance of HttpAdapter.
- #scheme ⇒ Object
- #send_request(method, path, opts = {}) ⇒ Object
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_options ⇒ Object
Returns the value of attribute connection_options.
26 27 28 |
# File 'lib/yammer/http_adapter.rb', line 26 def @connection_options end |
#site_url ⇒ Object
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 |
#host ⇒ Object
44 45 46 |
# File 'lib/yammer/http_adapter.rb', line 44 def host @host ||= parsed_url.host end |
#scheme ⇒ Object
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..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 |