wfhcli 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/wfhcli +8 -8
  2. data/lib/wfhcli.rb +138 -117
  3. metadata +28 -12
data/bin/wfhcli CHANGED
@@ -11,34 +11,34 @@ class WfhCli < Thor
11
11
 
12
12
  desc 'categories', 'List categories'
13
13
  def categories
14
- @wfh.list_categories
14
+ puts @wfh.display_categories
15
15
  end
16
16
 
17
17
  desc 'companies', 'List companies'
18
- option :page
18
+ option :page, default: 1
19
19
  def companies
20
- @wfh.list_companies(options[:page])
20
+ puts @wfh.display_companies(options[:page])
21
21
  end
22
22
 
23
23
  desc 'company ID', 'Show company with ID'
24
24
  def company(id)
25
- @wfh.show_company(id)
25
+ puts @wfh.display_company(id)
26
26
  end
27
27
 
28
28
  desc 'jobs', 'List jobs'
29
29
  option :category
30
- option :page
30
+ option :page, default: 1
31
31
  def jobs
32
32
  if options[:category]
33
- @wfh.list_jobs(options[:page], options[:category])
33
+ puts @wfh.display_jobs(options[:page], options[:category])
34
34
  else
35
- @wfh.list_jobs(options[:page])
35
+ puts @wfh.display_jobs(options[:page])
36
36
  end
37
37
  end
38
38
 
39
39
  desc 'job ID', 'Show job with ID'
40
40
  def job(id)
41
- @wfh.show_job(id)
41
+ puts @wfh.display_job(id)
42
42
  end
43
43
  end
44
44
 
@@ -12,76 +12,22 @@ class WfhLib
12
12
  @url = 'https://www.wfh.io/api'
13
13
  end
14
14
 
15
- # TODO: Make private once we are able to properly test methods which use
16
- # use this method.
17
- def format_date(str, inc_time=false)
18
- format = '%Y-%m-%d'
19
- format = format + ' %H:%M' if inc_time == true
20
- d = DateTime.parse(str)
21
- d.strftime(format)
15
+ def categories
16
+ get_json('/categories')
22
17
  end
23
18
 
24
- # TODO: Make private once we are able to properly test methods which use
25
- # use this method.
26
- def generate_table(content)
27
- cell_widths = Array.new(content[0].size, 0)
28
-
29
- # We do cell.to_s.size as cell could be an integer and 8.size == 8,
30
- # which is not what we want.
31
- content.each do |row|
32
- row.each_with_index do |cell, index|
33
- if cell.to_s.size > cell_widths[index]
34
- cell_widths[index] = cell.to_s.size
35
- end
36
- end
37
- end
38
-
39
- lines = ''
40
-
41
- content.each_with_index do |row, row_index|
42
- if row_index == 1
43
- lines << '|'
44
- cell_widths.each do |c|
45
- # We use c + 2 to account for the spaces inside each cell
46
- lines << '-' * (c + 2)
47
- lines << '|'
48
- end
49
- lines << "\n"
50
- end
51
- lines << '|'
52
- row.each_with_index do |cell, cell_index|
53
- formatted = cell.to_s.ljust(cell_widths[cell_index])
54
-
55
- if row_index == 0
56
- lines << " #{@shell.set_color(formatted, @title_colour)} |"
57
- else
58
- lines << " #{formatted} |"
59
- end
60
- end
61
- lines << "\n"
62
- end
63
-
64
- return lines
19
+ def company(id)
20
+ get_json("/companies/#{id}")
65
21
  end
66
22
 
67
- # TODO: Make private once we are able to properly test methods which use
68
- # use this method.
69
- def get_json(uri)
70
- begin
71
- r = RestClient.get "#{@url}#{uri}", { accept: :json }
72
- rescue RestClient::ResourceNotFound
73
- puts "The resource #{uri} was not found"
74
- exit!
75
- rescue => e
76
- puts e
77
- exit!
78
- else
79
- JSON.parse(r)
80
- end
23
+ def companies(page=1)
24
+ uri = "/companies?page=#{page}"
25
+
26
+ get_json(uri)
81
27
  end
82
28
 
83
- def list_categories
84
- categories = get_json('/categories')
29
+ def display_categories
30
+ categories = self.categories
85
31
 
86
32
  if categories.size > 0
87
33
  content = []
@@ -91,17 +37,35 @@ class WfhLib
91
37
  content << [category['id'], category['name']]
92
38
  end
93
39
 
94
- puts generate_table(content)
40
+ generate_table(content)
95
41
  else
96
- puts 'No categories found'
42
+ 'No categories found'
97
43
  end
98
44
  end
99
45
 
100
- def list_companies(page=nil)
101
- uri = '/companies'
102
- uri = uri + "?page=#{page}" if page
46
+ def display_company(company_id)
47
+ output = ''
48
+ company = self.company(company_id)
103
49
 
104
- companies = get_json(uri)
50
+ output << generate_header_and_body('Name', company['name'])
51
+ output << generate_header_and_body('URL', company['url'])
52
+ unless company['country'].nil?
53
+ output << generate_header_and_body('Headquarters',
54
+ company['country']['name'])
55
+ end
56
+ unless company['twitter'].nil? || company['twitter'].empty?
57
+ output << generate_header_and_body('Twitter', company['twitter'])
58
+ end
59
+ unless company['showcase_url'].nil? || company['showcase_url'].empty?
60
+ output << generate_header_and_body('Showcase URL',
61
+ company['showcase_url'])
62
+ end
63
+
64
+ return output
65
+ end
66
+
67
+ def display_companies(page)
68
+ companies = self.companies(page)
105
69
 
106
70
  if companies.size > 0
107
71
  content = []
@@ -111,22 +75,43 @@ class WfhLib
111
75
  content << [company['id'], company['name']]
112
76
  end
113
77
 
114
- puts generate_table(content)
78
+ generate_table(content)
115
79
  else
116
- puts 'No companies found'
80
+ 'No companies found'
117
81
  end
118
82
  end
119
83
 
120
- def list_jobs(page=nil, category_id=nil)
121
- if category_id.nil?
122
- uri = '/jobs'
123
- uri = uri + "?page=#{page}" if page
84
+ def display_job(job_id)
85
+ output = ''
86
+ job = self.job(job_id)
87
+
88
+ if job['country'].nil? || job['country'].empty?
89
+ country = 'Anywhere'
124
90
  else
125
- uri = "/categories/#{category_id}/jobs"
126
- uri = uri + "?page=#{page}" if page
91
+ country = job['country']['name']
92
+ end
93
+
94
+ title = "#{job['title']} @ #{job['company']['name']} " \
95
+ "(#{job['company']['id']})"
96
+ category = "#{job['category']['name']} (#{job['category']['id']})"
97
+ posted = format_date(job['created_at'], true)
98
+
99
+ output << generate_header_and_body('Title', title)
100
+ output << generate_header_and_body('Category', category)
101
+ output << generate_header_and_body('Posted', posted)
102
+ output << generate_header_and_body('Description', job['description'])
103
+ output << generate_header_and_body('Application Info',
104
+ job['application_info'])
105
+ output << generate_header_and_body('Country', country)
106
+ unless job['location'].nil? || job['location'].empty?
107
+ output << generate_header_and_body('Location', job['location'])
127
108
  end
128
109
 
129
- jobs = get_json(uri)
110
+ return output
111
+ end
112
+
113
+ def display_jobs(page, category_id=nil)
114
+ jobs = self.jobs(page, category_id)
130
115
 
131
116
  if jobs.size > 0
132
117
  content = []
@@ -140,60 +125,96 @@ class WfhLib
140
125
  truncate(job['title'], 30)]
141
126
  end
142
127
 
143
- puts generate_table(content)
128
+ generate_table(content)
129
+ else
130
+ 'No jobs found'
131
+ end
132
+ end
133
+
134
+ def job(id)
135
+ get_json("/jobs/#{id}")
136
+ end
137
+
138
+ def jobs(page=1, category_id=nil)
139
+ if category_id.nil?
140
+ uri = "/jobs?page=#{page}"
144
141
  else
145
- puts 'No jobs found'
142
+ uri = "/categories/#{category_id}/jobs?page=#{page}"
146
143
  end
144
+
145
+ get_json(uri)
146
+ end
147
+
148
+ private
149
+
150
+ def format_date(str, inc_time=false)
151
+ format = '%Y-%m-%d'
152
+ format = format + ' %H:%M' if inc_time == true
153
+ d = DateTime.parse(str)
154
+ d.strftime(format)
147
155
  end
148
156
 
149
- # TODO: Make private once we are able to properly test methods which use
150
- # use this method.
151
157
  def generate_header_and_body(title, body)
152
- "#{@shell.set_color(title, @title_colour)}\n#{body}"
158
+ "#{@shell.set_color(title, @title_colour)}\n#{body}\n"
153
159
  end
154
160
 
155
- def show_company(company_id)
156
- company = get_json("/companies/#{company_id}")
161
+ def generate_table(content)
162
+ cell_widths = Array.new(content[0].size, 0)
157
163
 
158
- puts generate_header_and_body('Name', company['name'])
159
- puts generate_header_and_body('URL', company['url'])
160
- unless company['country'].nil?
161
- puts generate_header_and_body('Headquarters', company['country']['name'])
162
- end
163
- unless company['twitter'].nil? || company['twitter'].empty?
164
- puts generate_header_and_body('Twitter', company['twitter'])
165
- end
166
- unless company['showcase_url'].nil? || company['showcase_url'].empty?
167
- puts generate_header_and_body('Showcase URL', company['showcase_url'])
164
+ # We do cell.to_s.size as cell could be an integer and 8.size == 8,
165
+ # which is not what we want.
166
+ content.each do |row|
167
+ row.each_with_index do |cell, index|
168
+ if cell.to_s.size > cell_widths[index]
169
+ cell_widths[index] = cell.to_s.size
170
+ end
171
+ end
168
172
  end
169
- end
170
173
 
171
- def show_job(job_id)
172
- job = get_json("/jobs/#{job_id}")
173
- if job['country'].nil? || job['country'].empty?
174
- country = 'Anywhere'
175
- else
176
- country = job['country']['name']
174
+ lines = ''
175
+
176
+ content.each_with_index do |row, row_index|
177
+ if row_index == 1
178
+ lines << '|'
179
+ cell_widths.each do |c|
180
+ # We use c + 2 to account for the spaces inside each cell
181
+ lines << '-' * (c + 2)
182
+ lines << '|'
183
+ end
184
+ lines << "\n"
185
+ end
186
+ lines << '|'
187
+ row.each_with_index do |cell, cell_index|
188
+ formatted = cell.to_s.ljust(cell_widths[cell_index])
189
+
190
+ if row_index == 0
191
+ lines << " #{@shell.set_color(formatted, @title_colour)} |"
192
+ else
193
+ lines << " #{formatted} |"
194
+ end
195
+ end
196
+ lines << "\n"
177
197
  end
178
198
 
179
- title = "#{job['title']} @ #{job['company']['name']} " \
180
- "(#{job['company']['id']})"
181
- category = "#{job['category']['name']} (#{job['category']['id']})"
182
- posted = format_date(job['created_at'], true)
199
+ return lines
200
+ end
183
201
 
184
- puts generate_header_and_body('Title', title)
185
- puts generate_header_and_body('Category', category)
186
- puts generate_header_and_body('Posted', posted)
187
- puts generate_header_and_body('Description', job['description'])
188
- puts generate_header_and_body('Application Info', job['application_info'])
189
- puts generate_header_and_body('Country', country)
190
- unless job['location'].nil? || job['location'].empty?
191
- puts generate_header_and_body('Location', job['location'])
202
+ def get_json(uri)
203
+ begin
204
+ # TODO: add wfhcli version to user_agent string
205
+ r = RestClient.get("#{@url}#{uri}",
206
+ { accept: :json, user_agent: 'wfhcli' })
207
+ rescue RestClient::ResourceNotFound
208
+ puts "The resource #{uri} was not found"
209
+ exit!
210
+ rescue => e
211
+ puts e
212
+ exit!
213
+ else
214
+ JSON.parse(r)
192
215
  end
193
216
  end
194
217
 
195
- # TODO: Make private once we are able to properly test methods which use
196
- # use this method.
197
218
  def truncate(str, len)
198
219
  if str.size > len
199
220
  str[0..(len - 4)] + '...'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wfhcli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,41 +9,57 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-04 00:00:00.000000000 Z
12
+ date: 2015-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 1.7.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 1.7.2
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: thor
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 0.19.1
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
46
- description: CLI tool to query WFH.io's JSON API
45
+ version: 0.19.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.11.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.11.0
62
+ description: A CLI wrapper around the WFH.io (https://www.wfh.io) remote job board
47
63
  email: admin@wfh.io
48
64
  executables:
49
65
  - wfhcli
@@ -77,5 +93,5 @@ rubyforge_project:
77
93
  rubygems_version: 1.8.23
78
94
  signing_key:
79
95
  specification_version: 3
80
- summary: WFH.io CLI tool
96
+ summary: A CLI wrapper around the WFH.io (https://www.wfh.io) remote job board
81
97
  test_files: []