wfhcli 0.3.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 +45 -0
  2. data/lib/wfhcli.rb +195 -0
  3. metadata +80 -0
data/bin/wfhcli ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'wfhcli'
5
+
6
+ class WfhCli < Thor
7
+ def initialize(*args)
8
+ @wfh = WfhLib.new
9
+ super
10
+ end
11
+
12
+ desc "categories", "List categories"
13
+ def categories
14
+ @wfh.list_categories
15
+ end
16
+
17
+ desc "companies", "List companies"
18
+ option :page
19
+ def companies
20
+ @wfh.list_companies(options[:page])
21
+ end
22
+
23
+ desc "company ID", "Show company with ID"
24
+ def company(id)
25
+ @wfh.show_company(id)
26
+ end
27
+
28
+ desc "jobs", "List jobs"
29
+ option :category
30
+ option :page
31
+ def jobs
32
+ if options[:category]
33
+ @wfh.list_jobs(options[:page], options[:category])
34
+ else
35
+ @wfh.list_jobs(options[:page])
36
+ end
37
+ end
38
+
39
+ desc "job ID", "Show job with ID"
40
+ def job(id)
41
+ @wfh.show_job(id)
42
+ end
43
+ end
44
+
45
+ WfhCli.start(ARGV)
data/lib/wfhcli.rb ADDED
@@ -0,0 +1,195 @@
1
+ require 'date'
2
+ require 'json'
3
+ require 'rest-client'
4
+ require 'thor'
5
+
6
+ class WfhLib
7
+ TITLE_COLOUR = :magenta
8
+ URL = 'https://www.wfh.io/api'
9
+
10
+ def initialize
11
+ @shell = Thor::Shell::Color.new
12
+ end
13
+
14
+ # TODO: Make private once we are able to properly test methods which use
15
+ # use this method.
16
+ def format_date(str, inc_time=false)
17
+ format = '%Y-%m-%d'
18
+ format = format + ' %H:%M' if inc_time == true
19
+ d = DateTime.parse(str)
20
+ d.strftime(format)
21
+ end
22
+
23
+ # TODO: Make private once we are able to properly test methods which use
24
+ # use this method.
25
+ def generate_table(content)
26
+ cell_widths = Array.new(content[0].size, 0)
27
+
28
+ # We do cell.to_s.size as cell could be an integer and 8.size == 8, which is
29
+ # not what we want.
30
+ content.each do |row|
31
+ row.each_with_index do |cell, index|
32
+ if cell.to_s.size > cell_widths[index]
33
+ cell_widths[index] = cell.to_s.size
34
+ end
35
+ end
36
+ end
37
+
38
+ lines = ""
39
+
40
+ content.each_with_index do |row, row_index|
41
+ if row_index == 1
42
+ lines << "|"
43
+ cell_widths.each do |c|
44
+ # We use c + 2 to account for the spaces inside each cell
45
+ lines << "-" * (c + 2)
46
+ lines << "|"
47
+ end
48
+ lines << "\n"
49
+ end
50
+ lines << "|"
51
+ row.each_with_index do |cell, cell_index|
52
+ formatted = cell.to_s.ljust(cell_widths[cell_index])
53
+
54
+ if row_index == 0
55
+ lines << " #{@shell.set_color(formatted, TITLE_COLOUR)} |"
56
+ else
57
+ lines << " #{formatted} |"
58
+ end
59
+ end
60
+ lines << "\n"
61
+ end
62
+
63
+ return lines
64
+ end
65
+
66
+ # TODO: Make private once we are able to properly test methods which use
67
+ # use this method.
68
+ def get_json(uri)
69
+ begin
70
+ r = RestClient.get "#{URL}#{uri}", {:accept => :json}
71
+ rescue => e
72
+ puts e
73
+ exit!
74
+ else
75
+ JSON.parse(r)
76
+ end
77
+ end
78
+
79
+ def list_categories
80
+ categories = get_json('/categories')
81
+
82
+ if categories.size > 0
83
+ content = []
84
+ content[0] = ['ID', 'Name']
85
+
86
+ categories.each do |category|
87
+ content << [category['id'], category['name']]
88
+ end
89
+
90
+ puts generate_table(content)
91
+ else
92
+ puts 'No categories found'
93
+ end
94
+ end
95
+
96
+ def list_companies(page=nil)
97
+ uri = '/companies'
98
+ uri = uri + "?page=#{page}" if page
99
+
100
+ companies = get_json(uri)
101
+
102
+ if companies.size > 0
103
+ content = []
104
+ content[0] = ['ID', 'Name']
105
+
106
+ companies.each do |company|
107
+ content << [company['id'], company['name']]
108
+ end
109
+
110
+ puts generate_table(content)
111
+ else
112
+ puts 'No companies found'
113
+ end
114
+ end
115
+
116
+ def list_jobs(page=nil, category_id=nil)
117
+ if category_id == nil
118
+ uri = '/jobs'
119
+ uri = uri + "?page=#{page}" if page
120
+ else
121
+ uri = "/categories/#{category_id}/jobs"
122
+ uri = uri + "?page=#{page}" if page
123
+ end
124
+
125
+ jobs = get_json(uri)
126
+
127
+ if jobs.size > 0
128
+ content = []
129
+ content[0] = ['ID', 'Posted', 'Category', 'Company', 'Title']
130
+
131
+ jobs.each do |job|
132
+ content << [job['id'],
133
+ format_date(job['created_at']),
134
+ "#{job['category']['name']} (#{job['category']['id']})",
135
+ "#{job['company']['name']} (#{job['company']['id']})",
136
+ truncate(job['title'], 30)]
137
+ end
138
+
139
+ puts generate_table(content)
140
+ else
141
+ puts 'No jobs found'
142
+ end
143
+ end
144
+
145
+ # TODO: Make private once we are able to properly test methods which use
146
+ # use this method.
147
+ def generate_header_and_body(title, body)
148
+ "#{@shell.set_color(title, TITLE_COLOUR)}\n#{body}"
149
+ end
150
+
151
+ def show_company(company_id)
152
+ company = get_json("/companies/#{company_id}")
153
+
154
+ puts generate_header_and_body('Name', company['name'])
155
+ puts generate_header_and_body('URL', company['url'])
156
+ unless company['country'].nil?
157
+ puts generate_header_and_body('Headquarters', company['country']['name'])
158
+ end
159
+ unless company['twitter'].nil? or company['twitter'].empty?
160
+ puts generate_header_and_body('Twitter', company['twitter'])
161
+ end
162
+ unless company['showcase_url'].nil? or company['showcase_url'].empty?
163
+ puts generate_header_and_body('Showcase URL', company['showcase_url'])
164
+ end
165
+ end
166
+
167
+ def show_job(job_id)
168
+ job = get_json("/jobs/#{job_id}")
169
+ if job['country'].nil? or job['country'].empty?
170
+ country = 'Anywhere'
171
+ else
172
+ country = job['country']['name']
173
+ end
174
+
175
+ puts generate_header_and_body('Title', "#{job['title']} @ #{job['company']['name']}")
176
+ puts generate_header_and_body('Category', "#{job['category']['name']} (#{job['category']['id']})")
177
+ puts generate_header_and_body('Posted', format_date(job['created_at'], inc_time=true))
178
+ puts generate_header_and_body('Description', job['description'])
179
+ puts generate_header_and_body('Application Info', job['application_info'])
180
+ puts generate_header_and_body('Country', country)
181
+ unless job['location'].nil? or job['location'].empty?
182
+ puts generate_header_and_body('Location', job['location'])
183
+ end
184
+ end
185
+
186
+ # TODO: Make private once we are able to properly test methods which use
187
+ # use this method.
188
+ def truncate(str, len)
189
+ if str.size > len
190
+ str[0..(len-4)] + "..."
191
+ else
192
+ str
193
+ end
194
+ end
195
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wfhcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Thompson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: CLI tool to query WFH.io's JSON API
47
+ email: admin@wfh.io
48
+ executables:
49
+ - wfhcli
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/wfhcli.rb
54
+ - bin/wfhcli
55
+ homepage: https://github.com/wfhio/wfhcli
56
+ licenses:
57
+ - MIT
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.23
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: WFH.io CLI tool
80
+ test_files: []