Class: WfhLib
- Inherits:
-
Object
- Object
- WfhLib
- Defined in:
- lib/wfhcli.rb
Constant Summary collapse
- TITLE_COLOUR =
:magenta- URL =
'https://www.wfh.io/api'
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.
10 11 12 |
# File 'lib/wfhcli.rb', line 10 def initialize @shell = Thor::Shell::Color.new 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.
16 17 18 19 20 21 |
# File 'lib/wfhcli.rb', line 16 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.
147 148 149 |
# File 'lib/wfhcli.rb', line 147 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.
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 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wfhcli.rb', line 25 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.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/wfhcli.rb', line 68 def get_json(uri) begin r = RestClient.get "#{URL}#{uri}", {:accept => :json} rescue => e puts e exit! else JSON.parse(r) end end |
#list_categories ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wfhcli.rb', line 79 def list_categories categories = get_json('/categories') if categories.size > 0 content = [] content[0] = ['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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/wfhcli.rb', line 96 def list_companies(page=nil) uri = '/companies' uri = uri + "?page=#{page}" if page companies = get_json(uri) if companies.size > 0 content = [] content[0] = ['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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/wfhcli.rb', line 116 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] = ['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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/wfhcli.rb', line 151 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? or company['twitter'].empty? puts generate_header_and_body('Twitter', company['twitter']) end unless company['showcase_url'].nil? or company['showcase_url'].empty? puts generate_header_and_body('Showcase URL', company['showcase_url']) end end |
#show_job(job_id) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/wfhcli.rb', line 167 def show_job(job_id) job = get_json("/jobs/#{job_id}") if job['country'].nil? or job['country'].empty? country = 'Anywhere' else country = job['country']['name'] end puts generate_header_and_body('Title', "#{job['title']} @ #{job['company']['name']}") puts generate_header_and_body('Category', "#{job['category']['name']} (#{job['category']['id']})") puts generate_header_and_body('Posted', format_date(job['created_at'], inc_time=true)) 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? or 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.
188 189 190 191 192 193 194 |
# File 'lib/wfhcli.rb', line 188 def truncate(str, len) if str.size > len str[0..(len-4)] + "..." else str end end |