Module: MrMurano::Http
- Included in:
- Account, ProductBase, SolutionBase
- Defined in:
- lib/MrMurano/http.rb
Instance Method Summary collapse
- #curldebug(request) ⇒ Object
- #delete(path = '', &block) ⇒ Object
- #get(path = '', &block) ⇒ Object
- #http ⇒ Object
- #http_reset ⇒ Object
- #json_opts ⇒ Object
- #post(path = '', body = {}, &block) ⇒ Object
- #postf(path = '', form = {}, &block) ⇒ Object
- #put(path = '', body = {}, &block) ⇒ Object
- #set_def_headers(request) ⇒ Object
- #showHttpError(request, response) ⇒ Object
- #token ⇒ Object
- #tryToPrettyJSON(data) ⇒ Object
- #workit(request, &block) ⇒ Object
Instance Method Details
#curldebug(request) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/MrMurano/http.rb', line 23 def curldebug(request) if $cfg['tool.curldebug'] then a = [] a << %{curl -s } if request.key?('Authorization') then a << %{-H 'Authorization: #{request['Authorization']}'} end a << %{-H 'User-Agent: #{request['User-Agent']}'} a << %{-H 'Content-Type: #{request.content_type}'} a << %{-X #{request.method}} a << %{'#{request.uri.to_s}'} a << %{-d '#{request.body}'} unless request.body.nil? puts a.join(' ') end end |
#delete(path = '', &block) ⇒ Object
133 134 135 136 |
# File 'lib/MrMurano/http.rb', line 133 def delete(path='', &block) uri = endPoint(path) workit(set_def_headers(Net::HTTP::Delete.new(uri)), &block) end |
#get(path = '', &block) ⇒ Object
103 104 105 106 |
# File 'lib/MrMurano/http.rb', line 103 def get(path='', &block) uri = endPoint(path) workit(set_def_headers(Net::HTTP::Get.new(uri)), &block) end |
#http ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/MrMurano/http.rb', line 39 def http uri = URI('https://' + $cfg['net.host']) if @http.nil? then @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = true @http.start end @http end |
#http_reset ⇒ Object
48 49 50 |
# File 'lib/MrMurano/http.rb', line 48 def http_reset @http = nil end |
#json_opts ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/MrMurano/http.rb', line 14 def json_opts return @json_opts unless @json_opts.nil? @json_opts = { :allow_nan => true, :symbolize_names => true, :create_additions => false } end |
#post(path = '', body = {}, &block) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/MrMurano/http.rb', line 108 def post(path='', body={}, &block) uri = endPoint(path) req = Net::HTTP::Post.new(uri) set_def_headers(req) req.body = JSON.generate(body) workit(req, &block) end |
#postf(path = '', form = {}, &block) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/MrMurano/http.rb', line 116 def postf(path='', form={}, &block) uri = endPoint(path) req = Net::HTTP::Post.new(uri) set_def_headers(req) req.content_type = 'application/x-www-form-urlencoded; charset=utf-8' req.form_data = form workit(req, &block) end |
#put(path = '', body = {}, &block) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/MrMurano/http.rb', line 125 def put(path='', body={}, &block) uri = endPoint(path) req = Net::HTTP::Put.new(uri) set_def_headers(req) req.body = JSON.generate(body) workit(req, &block) end |
#set_def_headers(request) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/MrMurano/http.rb', line 52 def set_def_headers(request) request.content_type = 'application/json' request['Authorization'] = 'token ' + token request['User-Agent'] = "MrMurano/#{MrMurano::VERSION}" request end |
#showHttpError(request, response) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/MrMurano/http.rb', line 68 def showHttpError(request, response) if $cfg['tool.debug'] then puts "Sent #{request.method} #{request.uri.to_s}" request.each_capitalized{|k,v| puts "> #{k}: #{v}"} if request.body.nil? then else puts " > #{request.body[0..156]}" end puts "Got #{response.code} #{response.}" response.each_capitalized{|k,v| puts "< #{k}: #{v}"} end say_error "Request Failed: #{response.code}: #{tryToPrettyJSON(response.body)}" end |
#token ⇒ Object
7 8 9 10 11 12 |
# File 'lib/MrMurano/http.rb', line 7 def token return @token unless @token.nil? @token = Account.new.token raise "Not logged in!" if @token.nil? @token end |
#tryToPrettyJSON(data) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/MrMurano/http.rb', line 59 def tryToPrettyJSON(data) begin ret = JSON.parse(data, json_opts) return JSON.pretty_generate(ret) rescue return response.body end end |
#workit(request, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/MrMurano/http.rb', line 82 def workit(request, &block) curldebug(request) if block_given? then return yield request, http() else response = http().request(request) case response when Net::HTTPSuccess return {} if response.body.nil? begin return JSON.parse(response.body, json_opts) rescue return response.body end else showHttpError(request, response) raise response end end end |