Class: Communication::Communicator

Inherits:
Object
  • Object
show all
Includes:
CommunicatorHelpers, Singleton
Defined in:
lib/communication/communicator.rb

Overview

Communication

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommunicatorHelpers

#auth_call?, #check_not_found, #check_not_success, #check_unauthorized, #handle_success, #relogin, #should_relogin

Constructor Details

#initializeCommunicator

Returns a new instance of Communicator.



33
34
35
# File 'lib/communication/communicator.rb', line 33

def initialize
  open
end

Instance Attribute Details

#cached_request_callbackObject

Returns the value of attribute cached_request_callback.



31
32
33
# File 'lib/communication/communicator.rb', line 31

def cached_request_callback
  @cached_request_callback
end

#cached_success_callbackObject

Returns the value of attribute cached_success_callback.



31
32
33
# File 'lib/communication/communicator.rb', line 31

def cached_success_callback
  @cached_success_callback
end

Class Method Details

.base_api_urlString

Returns the base JIRA API URL.

Returns:

  • (String)

    returns the base JIRA API URL.



129
130
131
# File 'lib/communication/communicator.rb', line 129

def self.base_api_url
  "/rest/api/#{Constants::JIRA_REST_API_VERSION}"
end

Instance Method Details

Adds a cookie to request if it's found in connection's header.

Parameters:

  • req

    The request object from Faraday lib.



95
96
97
# File 'lib/communication/communicator.rb', line 95

def add_cookie_if_needed(req)
  req.headers = { "Cookie" => conn.headers["Cookie"] } unless conn.headers["Cookie"].nil?
end

#cache_call(path, success_block, &callback) ⇒ Object

Caches and starts a request

Parameters:

  • callback (Proc)

    A completion callback.

  • success_block (Proc)

    The proc that will be executed upon success response.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/communication/communicator.rb', line 74

def cache_call(path, success_block, &callback)
  # Do not cache auth call
  if auth_call?(path)
    callback.call(success_block)
    return
  end

  self.cached_request_callback = callback
  self.cached_success_callback = success_block

  callback(success_block)
end

#delete(path, &success_block) ⇒ Object



63
64
65
66
67
68
# File 'lib/communication/communicator.rb', line 63

def delete(path, &success_block)
  cache_call(path, success_block) do
    res = conn.delete(path) { |req| add_cookie_if_needed(req) }
    handle_response(res) { |body| yield(body, res) }
  end
end

#get(path, &success_block) ⇒ Object

Makes a GET request.

Parameters:

  • path (String)

    The path of the request.



40
41
42
43
44
45
46
47
# File 'lib/communication/communicator.rb', line 40

def get(path, &success_block)
  cache_call(path, success_block) do
    res = conn.get(path) { |req| add_cookie_if_needed(req) }
    handle_response(res) do |body|
      yield(body, res)
    end
  end
end

#handle_response(res) ⇒ Object

Handles response by checking http status codes.

Parameters:

  • res

    The request object from Faraday lib.



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/communication/communicator.rb', line 136

def handle_response(res)
  # relogin if needed
  return relogin if should_relogin(res)

  check_unauthorized(res.status, res.env.url.to_s)
  check_not_found(res.status)
  check_not_success(res.status)
  handle_success(res.body) do |json_body|
    yield(json_body) if block_given?
  end
end

#log_work(issue_id, started, seconds_spent) ⇒ Hash

Adds a new worklog entry for the logged in user

Parameters:

  • issue_id (String)

    The Jira issue id.

  • started (String)

    The started date in ISO 8601 format (e.g. 2020-06-16T18:15:05.920+0000)

  • seconds_spent (Integer)

    The number of seconds spend on this issue

Returns:

  • (Hash)

    An object with an error message specified by 'error' key, or a { success: true }



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/communication/communicator.rb', line 105

def log_work(issue_id, started, seconds_spent)
  params = {
    started: started,
    timeSpentSeconds: seconds_spent
  }

  res = post("#{Communicator.base_api_url}/issue/#{issue_id}/worklog", params)

  if res.status == 200
    { success: true }
  else
    { error: "Something went wrong! (#{res.status})" }
  end
end

#parse_json(body) ⇒ Object

Parses the given body as JSON.



88
89
90
# File 'lib/communication/communicator.rb', line 88

def parse_json(body)
  JSON.parse(body, { symbolize_names: true })
end

#post(path, params = {}, &success_block) ⇒ Object

Makes a POST request.

Parameters:

  • path (String)

    The path of the request.

  • params (Hash) (defaults to: {})

    The params of post request.



53
54
55
56
57
58
59
60
61
# File 'lib/communication/communicator.rb', line 53

def post(path, params = {}, &success_block)
  cache_call(path, success_block) do
    res = conn.post(path) do |req|
      add_cookie_if_needed(req)
      req.body = params.to_json
    end
    handle_response(res) { |body| yield(body, res) }
  end
end


120
121
122
123
124
# File 'lib/communication/communicator.rb', line 120

def store_cookie(cookie)
  # Store cookie
  conn.headers["Cookie"] = cookie
  Utilities.store_cookie(cookie)
end