Module: MrMurano::Http

Included in:
Account, ProductBase, SolutionBase
Defined in:
lib/MrMurano/http.rb

Instance Method Summary collapse

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



145
146
147
148
# File 'lib/MrMurano/http.rb', line 145

def delete(path='', &block)
  uri = endPoint(path)
  workit(set_def_headers(Net::HTTP::Delete.new(uri)), &block)
end

#get(path = '', query = nil, &block) ⇒ Object



114
115
116
117
118
# File 'lib/MrMurano/http.rb', line 114

def get(path='', query=nil, &block)
  uri = endPoint(path)
  uri.query = URI.encode_www_form(query) unless query.nil?
  workit(set_def_headers(Net::HTTP::Get.new(uri)), &block)
end

#httpObject



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 not defined?(@http) or @http.nil? then
    @http = Net::HTTP.new(uri.host, uri.port)
    @http.use_ssl = true
    @http.start
  end
  @http
end

#http_resetObject



48
49
50
# File 'lib/MrMurano/http.rb', line 48

def http_reset
  @http = nil
end

#isJSON(data) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/MrMurano/http.rb', line 59

def isJSON(data)
  begin
    return true, JSON.parse(data, json_opts)
  rescue
    return false, data
  end
end

#json_optsObject



14
15
16
17
18
19
20
21
# File 'lib/MrMurano/http.rb', line 14

def json_opts
  return @json_opts unless not defined?(@json_opts) or @json_opts.nil?
  @json_opts = {
    :allow_nan => true,
    :symbolize_names => true,
    :create_additions => false
  }
end

#post(path = '', body = {}, &block) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/MrMurano/http.rb', line 120

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



128
129
130
131
132
133
134
135
# File 'lib/MrMurano/http.rb', line 128

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



137
138
139
140
141
142
143
# File 'lib/MrMurano/http.rb', line 137

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/MrMurano/http.rb', line 67

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.message}"
    response.each_capitalized{|k,v| puts "< #{k}: #{v}"}
  end
  isj, jsn = isJSON(response.body)
  resp = "Request Failed: #{response.code}: "
  if isj then
    if $cfg['tool.fullerror'] then
      resp << JSON.pretty_generate(jsn)
    else
      resp << "[#{jsn[:statusCode]}] " if jsn.has_key? :statusCode
      resp << jsn[:message] if jsn.has_key? :message
    end
  else
    resp << jsn
  end
  # assuming verbosing was included.
  error resp
end

#tokenObject



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

#workit(request, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/MrMurano/http.rb', line 94

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)
    end
  end
end