Sha256: 3cc661d168342b3b7a667500588540f9a2df15468cd17dccbb88266c5814ce41
Contents?: true
Size: 1.29 KB
Versions: 2
Compression:
Stored size: 1.29 KB
Contents
require "net/http" require "json" module Zonesync class HTTP < Struct.new(:base) def initialize(...) super @before_request = [] @after_response = [] end def get path request("get", path) end def post path, body request("post", path, body) end def patch path, body request("patch", path, body) end def delete path request("delete", path) end def before_request &block @before_request << block end def after_response &block @after_response << block end def request method, path, body=nil uri = URI.parse("#{base}#{path}") request = Net::HTTP.const_get(method.to_s.capitalize).new(uri.path) @before_request.each do |block| block.call(request, uri, body) end response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| body = JSON.dump(body) if request.fetch("Content-Type", "").include?("application/json") http.request(request, body) end @after_response.each do |block| call(response) end raise response.body unless response.code =~ /^20.$/ if response["Content-Type"].include?("application/json") JSON.parse(response.body) else response.body end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
zonesync-0.9.0 | lib/zonesync/http.rb |
zonesync-0.8.0 | lib/zonesync/http.rb |