Sha256: a3d29b22d243107e6228497fef9c669de9fa49c3e8f563d6bdb31794ae64f3a5
Contents?: true
Size: 1.75 KB
Versions: 3
Compression:
Stored size: 1.75 KB
Contents
# coding: utf-8 require 'net/http' require 'uri' require 'json' module HttpMachinegun class Client DEFAULT_PORT = 80 BASE_HEADERS = { #'content-type'=>'application/json' 'content-type'=>'text/plain' } def initialize(url, port, send_data = "") raise ArgumentError if url.nil? @url = URI.parse(url) @port = port || @url.port || DEFAULT_PORT @send_data = send_data end def basic_auth(username, password) if username && password @request_body.basic_auth username, password else raise ArgumentError end self rescue NoMethodError => ex puts "【error】set http medhod type before call" #@request_body = Net::HTTP::Get.new('/') #retry raise ex end def http_method(method) path = @url.path.empty? ? "/" : @url.path case method.to_sym when :get @request_body = Net::HTTP::Get.new(path) when :post @request_body = Net::HTTP::Post.new(path, BASE_HEADERS) when :put @request_body = Net::HTTP::Put.new(path, BASE_HEADERS) when :delete @request_body = Net::HTTP::Delete.new(path) else @request_body = Net::HTTP::Get.new(path) end self end def set_body @request_body.body = @send_data.to_json self end def execute() Net::HTTP.start(@url.host, @port) do |http| response = http.request(@request_body) if response.code =~ /^[^2]..$/ case response.code when '400' "BadRequestException" when '401' "UnauthorizedException" when '404' "NotFoundException" else end end response end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
http_machinegun-0.0.3 | lib/http_machinegun/client.rb |
http_machinegun-0.0.2 | lib/http_machinegun/client.rb |
http_machinegun-0.0.1 | lib/http_machinegun/client.rb |