Class: Communication::Communicator
- Inherits:
-
Object
- Object
- Communication::Communicator
- Includes:
- CommunicatorHelpers, Singleton
- Defined in:
- lib/communication/communicator.rb
Overview
Communication
Instance Attribute Summary collapse
-
#cached_request_callback ⇒ Object
Returns the value of attribute cached_request_callback.
-
#cached_success_callback ⇒ Object
Returns the value of attribute cached_success_callback.
Class Method Summary collapse
-
.base_api_url ⇒ String
Returns the base JIRA API URL.
Instance Method Summary collapse
-
#add_cookie_if_needed(req) ⇒ Object
Adds a cookie to request if it's found in connection's header.
-
#cache_call(path, success_block, &callback) ⇒ Object
Caches and starts a request.
- #delete(path, &success_block) ⇒ Object
-
#get(path, &success_block) ⇒ Object
Makes a GET request.
-
#handle_response(res) ⇒ Object
Handles response by checking http status codes.
-
#initialize ⇒ Communicator
constructor
A new instance of Communicator.
-
#log_work(issue_id, started, seconds_spent) ⇒ Hash
Adds a new worklog entry for the logged in user.
-
#parse_json(body) ⇒ Object
Parses the given body as JSON.
-
#post(path, params = {}, &success_block) ⇒ Object
Makes a POST request.
- #store_cookie(cookie) ⇒ Object
Methods included from CommunicatorHelpers
#auth_call?, #check_not_found, #check_not_success, #check_unauthorized, #handle_success, #relogin, #should_relogin
Constructor Details
#initialize ⇒ Communicator
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_callback ⇒ Object
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_callback ⇒ Object
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_url ⇒ 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
#add_cookie_if_needed(req) ⇒ Object
Adds a cookie to request if it's found in connection's header.
95 96 97 |
# File 'lib/communication/communicator.rb', line 95 def (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
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| (req) } handle_response(res) { |body| yield(body, res) } end end |
#get(path, &success_block) ⇒ Object
Makes a GET 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| (req) } handle_response(res) do |body| yield(body, res) end end end |
#handle_response(res) ⇒ Object
Handles response by checking http status codes.
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) (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
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.
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| (req) req.body = params.to_json end handle_response(res) { |body| yield(body, res) } end end |
#store_cookie(cookie) ⇒ Object
120 121 122 123 124 |
# File 'lib/communication/communicator.rb', line 120 def () # Store cookie conn.headers["Cookie"] = Utilities.() end |