Class: Stream::StreamHTTPClient
- Inherits:
-
Object
- Object
- Stream::StreamHTTPClient
- Defined in:
- lib/stream/client.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(url_generator) ⇒ StreamHTTPClient
constructor
A new instance of StreamHTTPClient.
- #make_http_request(method, relative_url, params = nil, data = nil, headers = nil) ⇒ Object
Constructor Details
#initialize(url_generator) ⇒ StreamHTTPClient
Returns a new instance of StreamHTTPClient.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/stream/client.rb', line 134 def initialize(url_generator) @options = url_generator. @conn = Faraday.new(url: url_generator.url) do |faraday| faraday.use RaiseHttpException faraday.[:open_timeout] = @options[:default_timeout] faraday.[:timeout] = @options[:default_timeout] faraday.adapter Faraday.default_adapter end @base_path = url_generator.base_path @conn.path_prefix = base_path end |
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
132 133 134 |
# File 'lib/stream/client.rb', line 132 def base_path @base_path end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
130 131 132 |
# File 'lib/stream/client.rb', line 130 def conn @conn end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
131 132 133 |
# File 'lib/stream/client.rb', line 131 def @options end |
Instance Method Details
#make_http_request(method, relative_url, params = nil, data = nil, headers = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/stream/client.rb', line 146 def make_http_request(method, relative_url, params = nil, data = nil, headers = nil) headers['Content-Type'] = 'application/json' headers['X-Stream-Client'] = "stream-ruby-client-#{Stream::VERSION}" base_url = [base_path, relative_url].join('/').gsub(%r{/+}, '/') url = "#{base_url}?#{URI.encode_www_form(params)}" body = data.to_json if %w[post put].include? method.to_s response = @conn.run_request( method, url, body, headers ) case response[:status].to_i when 200..203 return ::JSON.parse(response[:body]) end end |