Sha256: 78ef0913c15d10c805f7c663afc3c68bc62aed56a19594343c8ce4be00fc49f0
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
require "playback/parser" require 'net/http' require 'json' module Playback class Request DEFAULT_CONTENT_TYPE = 'application/text' DEFAULT_USER_AGENT = 'From Playback rubygems' def initialize(base_uri) @base_uri = base_uri @parser = Playback::Parser end def run(line, return_type='') parsed_line = parse(line) method = parsed_line[:request][:method] path = parsed_line[:request][:path] referer = parsed_line[:referer] ||= '' user_agent = parsed_line[:user_agent] ||= DEFAULT_USER_AGENT res = request(method, path, referer, user_agent) unless (return_type == 'net-http') result = { method: method, path: path, status: res.code.to_i, } res = JSON.generate result end res rescue => e e.message end def parse(line) begin @parser.parse(line.chomp, 'combined') rescue begin @parser.parse(line.chomp, 'common') rescue => e raise e end end end def request(method, path, referer, user_agent) begin uri = URI.parse(@base_uri + path) rescue raise "it can not be recognized as a uri: <#{@base_uri + path}>" end http = Net::HTTP.new(uri.host, uri.port) query = uri.query ||= '' data = {'Content-Type' => DEFAULT_CONTENT_TYPE, 'Referer' => referer, 'User-Agent' => user_agent} case method when 'GET' http.get(path, data) when 'POST' http.post(uri.path, query, data) when 'PUT' http.put(uri.path, query, data) when 'DELETE' http.delete(path, data) when 'PATCH' http.patch(uri.path, query, data) when 'HEAD' http.head(path, data) else raise "it is not supported http method: <#{method}>" end end private :parse, :request end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
playback-1.0.0 | lib/playback/request.rb |