Class: Intercom::Request
- Inherits:
-
Object
- Object
- Intercom::Request
- Defined in:
- lib/intercom/request.rb
Instance Attribute Summary (collapse)
-
- (Object) net_http_method
Returns the value of attribute net_http_method.
-
- (Object) path
Returns the value of attribute path.
Class Method Summary (collapse)
- + (Object) append_query_string_to_url(url, params)
- + (Object) default_headers
- + (Object) delete(path, params)
- + (Object) get(path, params)
- + (Object) method_with_body(http_method, path, params)
- + (Object) post(path, form_data)
- + (Object) put(path, form_data)
Instance Method Summary (collapse)
- - (Object) client(uri)
- - (Object) decode(content_encoding, body)
- - (Object) execute(target_base_url = nil)
-
- (Request) initialize(path, net_http_method)
constructor
A new instance of Request.
- - (Object) raise_errors_on_failure(res)
- - (Object) set_common_headers(method, base_uri)
Constructor Details
- (Request) initialize(path, net_http_method)
A new instance of Request
8 9 10 11 |
# File 'lib/intercom/request.rb', line 8 def initialize(path, net_http_method) self.path = path self.net_http_method = net_http_method end |
Instance Attribute Details
- (Object) net_http_method
Returns the value of attribute net_http_method
6 7 8 |
# File 'lib/intercom/request.rb', line 6 def net_http_method @net_http_method end |
- (Object) path
Returns the value of attribute path
6 7 8 |
# File 'lib/intercom/request.rb', line 6 def path @path end |
Class Method Details
+ (Object) append_query_string_to_url(url, params)
88 89 90 91 92 |
# File 'lib/intercom/request.rb', line 88 def self.append_query_string_to_url(url, params) return url if params.empty? query_string = params.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&') url + "?#{query_string}" end |
+ (Object) default_headers
42 43 44 |
# File 'lib/intercom/request.rb', line 42 def self.default_headers {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/json'} end |
+ (Object) delete(path, params)
27 28 29 |
# File 'lib/intercom/request.rb', line 27 def self.delete(path, params) new(path, Net::HTTP::Delete.new(append_query_string_to_url(path, params), default_headers)) end |
+ (Object) get(path, params)
19 20 21 |
# File 'lib/intercom/request.rb', line 19 def self.get(path, params) new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers)) end |
+ (Object) method_with_body(http_method, path, params)
35 36 37 38 39 40 |
# File 'lib/intercom/request.rb', line 35 def self.method_with_body(http_method, path, params) request = http_method.send(:new, path, default_headers) request.body = params.to_json request["Content-Type"] = "application/json" request end |
+ (Object) post(path, form_data)
23 24 25 |
# File 'lib/intercom/request.rb', line 23 def self.post(path, form_data) new(path, method_with_body(Net::HTTP::Post, path, form_data)) end |
+ (Object) put(path, form_data)
31 32 33 |
# File 'lib/intercom/request.rb', line 31 def self.put(path, form_data) new(path, method_with_body(Net::HTTP::Put, path, form_data)) end |
Instance Method Details
- (Object) client(uri)
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/intercom/request.rb', line 46 def client(uri) net = Net::HTTP.new(uri.host, uri.port) if uri.is_a?(URI::HTTPS) net.use_ssl = true net.verify_mode = OpenSSL::SSL::VERIFY_PEER net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem') end net.read_timeout = 30 net.open_timeout = 3 net end |
- (Object) decode(content_encoding, body)
71 72 73 74 |
# File 'lib/intercom/request.rb', line 71 def decode(content_encoding, body) return body if (!body) || body.empty? || content_encoding != 'gzip' Zlib::GzipReader.new(StringIO.new(body)).read end |
- (Object) execute(target_base_url = nil)
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/intercom/request.rb', line 58 def execute(target_base_url=nil) base_uri = URI.parse(target_base_url) set_common_headers(net_http_method, base_uri) client(base_uri).start do |http| response = http.request(net_http_method) raise_errors_on_failure(response) decoded = decode(response['content-encoding'], response.body) JSON.parse(decoded) end rescue Timeout::Error raise Intercom::ServiceUnavailableError end |
- (Object) raise_errors_on_failure(res)
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/intercom/request.rb', line 76 def raise_errors_on_failure(res) if res.code.to_i.eql?(404) raise Intercom::ResourceNotFound elsif res.code.to_i.eql?(401) raise Intercom::AuthenticationError elsif res.code.to_i.eql?(503) raise Intercom::ServiceUnavailableError elsif res.code.to_i.eql?(500) raise Intercom::ServerError end end |
- (Object) set_common_headers(method, base_uri)
13 14 15 16 17 |
# File 'lib/intercom/request.rb', line 13 def set_common_headers(method, base_uri) method.basic_auth(CGI.unescape(base_uri.user), CGI.unescape(base_uri.password)) method.add_field('Accept', 'application/json') method.add_field('AcceptEncoding', 'gzip, deflate') end |