Class: WfhLib
- Inherits:
-
Object
- Object
- WfhLib
- Defined in:
- lib/wfhcli.rb
Instance Attribute Summary collapse
-
#title_colour ⇒ Object
Returns the value of attribute title_colour.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#format_date(str, inc_time = false) ⇒ Object
TODO: Make private once we are able to properly test methods which use use this method.
-
#generate_header_and_body(title, body) ⇒ Object
TODO: Make private once we are able to properly test methods which use use this method.
-
#generate_table(content) ⇒ Object
TODO: Make private once we are able to properly test methods which use use this method.
-
#get_json(uri) ⇒ Object
TODO: Make private once we are able to properly test methods which use use this method.
-
#initialize ⇒ WfhLib
constructor
A new instance of WfhLib.
- #list_categories ⇒ Object
- #list_companies(page = nil) ⇒ Object
- #list_jobs(page = nil, category_id = nil) ⇒ Object
- #show_company(company_id) ⇒ Object
- #show_job(job_id) ⇒ Object
-
#truncate(str, len) ⇒ Object
TODO: Make private once we are able to properly test methods which use use this method.
Constructor Details
#initialize ⇒ WfhLib
Returns a new instance of WfhLib.
9 10 11 12 13 |
# File 'lib/wfhcli.rb', line 9 def initialize @shell = Thor::Shell::Color.new @title_colour = :magenta @url = 'https://www.wfh.io/api' end |
Instance Attribute Details
#title_colour ⇒ Object
Returns the value of attribute title_colour.
7 8 9 |
# File 'lib/wfhcli.rb', line 7 def title_colour @title_colour end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/wfhcli.rb', line 7 def url @url end |
Instance Method Details
#format_date(str, inc_time = false) ⇒ Object
TODO: Make private once we are able to properly test methods which use
use this method.
17 18 19 20 21 22 |
# File 'lib/wfhcli.rb', line 17 def format_date(str, inc_time=false) format = '%Y-%m-%d' format = format + ' %H:%M' if inc_time == true d = DateTime.parse(str) d.strftime(format) end |
#generate_header_and_body(title, body) ⇒ Object
TODO: Make private once we are able to properly test methods which use
use this method.
151 152 153 |
# File 'lib/wfhcli.rb', line 151 def generate_header_and_body(title, body) "#{@shell.set_color(title, @title_colour)}\n#{body}" end |
#generate_table(content) ⇒ Object
TODO: Make private once we are able to properly test methods which use
use this method.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wfhcli.rb', line 26 def generate_table(content) cell_widths = Array.new(content[0].size, 0) # We do cell.to_s.size as cell could be an integer and 8.size == 8, # which is not what we want. content.each do |row| row.each_with_index do |cell, index| if cell.to_s.size > cell_widths[index] cell_widths[index] = cell.to_s.size end end end lines = '' content.each_with_index do |row, row_index| if row_index == 1 lines << '|' cell_widths.each do |c| # We use c + 2 to account for the spaces inside each cell lines << '-' * (c + 2) lines << '|' end lines << "\n" end lines << '|' row.each_with_index do |cell, cell_index| formatted = cell.to_s.ljust(cell_widths[cell_index]) if row_index == 0 lines << " #{@shell.set_color(formatted, @title_colour)} |" else lines << " #{formatted} |" end end lines << "\n" end return lines end |
#get_json(uri) ⇒ Object
TODO: Make private once we are able to properly test methods which use
use this method.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wfhcli.rb', line 69 def get_json(uri) begin r = RestClient.get "#{@url}#{uri}", { accept: :json } rescue RestClient::ResourceNotFound puts "The resource #{uri} was not found" exit! rescue => e puts e exit! else JSON.parse(r) end end |
#list_categories ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wfhcli.rb', line 83 def list_categories categories = get_json('/categories') if categories.size > 0 content = [] content[0] = %w{ID Name} categories.each do |category| content << [category['id'], category['name']] end puts generate_table(content) else puts 'No categories found' end end |
#list_companies(page = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/wfhcli.rb', line 100 def list_companies(page=nil) uri = '/companies' uri = uri + "?page=#{page}" if page companies = get_json(uri) if companies.size > 0 content = [] content[0] = %w{ID Name} companies.each do |company| content << [company['id'], company['name']] end puts generate_table(content) else puts 'No companies found' end end |
#list_jobs(page = nil, category_id = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/wfhcli.rb', line 120 def list_jobs(page=nil, category_id=nil) if category_id.nil? uri = '/jobs' uri = uri + "?page=#{page}" if page else uri = "/categories/#{category_id}/jobs" uri = uri + "?page=#{page}" if page end jobs = get_json(uri) if jobs.size > 0 content = [] content[0] = %w{ID Posted Category Company Title} jobs.each do |job| content << [job['id'], format_date(job['created_at']), "#{job['category']['name']} (#{job['category']['id']})", "#{job['company']['name']} (#{job['company']['id']})", truncate(job['title'], 30)] end puts generate_table(content) else puts 'No jobs found' end end |
#show_company(company_id) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/wfhcli.rb', line 155 def show_company(company_id) company = get_json("/companies/#{company_id}") puts generate_header_and_body('Name', company['name']) puts generate_header_and_body('URL', company['url']) unless company['country'].nil? puts generate_header_and_body('Headquarters', company['country']['name']) end unless company['twitter'].nil? || company['twitter'].empty? puts generate_header_and_body('Twitter', company['twitter']) end unless company['showcase_url'].nil? || company['showcase_url'].empty? puts generate_header_and_body('Showcase URL', company['showcase_url']) end end |
#show_job(job_id) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/wfhcli.rb', line 171 def show_job(job_id) job = get_json("/jobs/#{job_id}") if job['country'].nil? || job['country'].empty? country = 'Anywhere' else country = job['country']['name'] end title = "#{job['title']} @ #{job['company']['name']} " \ "(#{job['company']['id']})" category = "#{job['category']['name']} (#{job['category']['id']})" posted = format_date(job['created_at'], true) puts generate_header_and_body('Title', title) puts generate_header_and_body('Category', category) puts generate_header_and_body('Posted', posted) puts generate_header_and_body('Description', job['description']) puts generate_header_and_body('Application Info', job['application_info']) puts generate_header_and_body('Country', country) unless job['location'].nil? || job['location'].empty? puts generate_header_and_body('Location', job['location']) end end |
#truncate(str, len) ⇒ Object
TODO: Make private once we are able to properly test methods which use
use this method.
197 198 199 200 201 202 203 |
# File 'lib/wfhcli.rb', line 197 def truncate(str, len) if str.size > len str[0..(len - 4)] + '...' else str end end |