Class: FerrisBueller::JiraAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/ferris-bueller/jira_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JiraAPI

Returns a new instance of JiraAPI.



11
12
13
14
15
16
17
18
# File 'lib/ferris-bueller/jira_api.rb', line 11

def initialize options={}
  @user      = options.fetch :user
  @pass      = options.fetch :pass
  @api_url   = options.fetch :api_url
  @base_path = options.fetch :base_path, '/rest/api/2'
  @logger    = options.fetch :logger, Slog.new
  log.debug event: 'Jira API client initialized'
end

Instance Method Details

#send(path, data = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ferris-bueller/jira_api.rb', line 20

def send path, data={}
  uri = URI File.join(@api_url, @base_path, path)
  http = Net::HTTP.new uri.hostname, uri.port
  http.use_ssl if uri.scheme == 'https'
  req = Net::HTTP::Post.new uri
  req.basic_auth @user, @pass
  req['Content-Type'] = 'application/json'
  req['Accept'] = 'application/json'
  req.body = JSON.generate data
  log.debug \
    event: 'sending Jira API request',
    path: path,
    data: data
  raw_res = http.request(req).body
  begin
    return nil unless raw_res
    res = JSON.parse raw_res, symbolize_names: true
    log.debug \
      event: 'Jira API request returned',
      path: path,
      data: data,
      response: res
    res
  rescue => e
    log.error \
      event: 'exception parsing jira response',
      response: raw_res.inspect,
      exception: e.class,
      message: e.message.inspect,
      backtrace: e.backtrace
    raise e
  end
end