Module: JiraMule::Http

Included in:
JiraUtils
Defined in:
lib/jiraMule/http.rb

Instance Method Summary collapse

Instance Method Details

#curldebug(request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jiraMule/http.rb', line 17

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



140
141
142
143
# File 'lib/jiraMule/http.rb', line 140

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

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



108
109
110
111
112
113
# File 'lib/jiraMule/http.rb', line 108

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

#httpObject



33
34
35
36
37
38
39
40
41
# File 'lib/jiraMule/http.rb', line 33

def http
  uri = URI($cfg['net.url'])
  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



42
43
44
# File 'lib/jiraMule/http.rb', line 42

def http_reset
  @http = nil
end

#isJSON(data) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/jiraMule/http.rb', line 53

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

#json_optsObject



8
9
10
11
12
13
14
15
# File 'lib/jiraMule/http.rb', line 8

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



115
116
117
118
119
120
121
# File 'lib/jiraMule/http.rb', line 115

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



123
124
125
126
127
128
129
130
# File 'lib/jiraMule/http.rb', line 123

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



132
133
134
135
136
137
138
# File 'lib/jiraMule/http.rb', line 132

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



46
47
48
49
50
51
# File 'lib/jiraMule/http.rb', line 46

def set_def_headers(request)
  request.content_type = 'application/json'
  request.basic_auth(username(), password())
  request['User-Agent'] = "JiraMule/#{JiraMule::VERSION}"
  request
end

#showHttpError(request, response) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jiraMule/http.rb', line 61

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
  say_error resp
end

#workit(request, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jiraMule/http.rb', line 87

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