Class: WfhLib

Inherits:
Object
  • Object
show all
Defined in:
lib/wfhcli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWfhLib

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_colourObject

Returns the value of attribute title_colour.



7
8
9
# File 'lib/wfhcli.rb', line 7

def title_colour
  @title_colour
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/wfhcli.rb', line 7

def url
  @url
end

Instance Method Details

#categoriesObject



15
16
17
# File 'lib/wfhcli.rb', line 15

def categories
  get_json('/categories')
end

#companies(page = 1) ⇒ Object



23
24
25
26
27
# File 'lib/wfhcli.rb', line 23

def companies(page=1)
  uri = "/companies?page=#{page}"

  get_json(uri)
end

#company(id) ⇒ Object



19
20
21
# File 'lib/wfhcli.rb', line 19

def company(id)
  get_json("/companies/#{id}")
end

#display_categoriesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wfhcli.rb', line 29

def display_categories
  categories = self.categories

  if categories.size > 0
    content = []
    content[0] = %w{ID Name}

    categories.each do |category|
      content << [category['id'], category['name']]
    end

    generate_table(content)
  else
    'No categories found'
  end
end

#display_companies(page) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wfhcli.rb', line 67

def display_companies(page)
  companies = self.companies(page)

  if companies.size > 0
    content = []
    content[0] = %w{ID Name}

    companies.each do |company|
      content << [company['id'], company['name']]
    end

    generate_table(content)
  else
    'No companies found'
  end
end

#display_company(company_id) ⇒ Object



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 46

def display_company(company_id)
  output = ''
  company = self.company(company_id)

  output << generate_header_and_body('Name', company['name'])
  output << generate_header_and_body('URL', company['url'])
  unless company['country'].nil?
    output << generate_header_and_body('Headquarters',
                                       company['country']['name'])
  end
  unless company['twitter'].nil? || company['twitter'].empty?
    output << generate_header_and_body('Twitter', company['twitter'])
  end
  unless company['showcase_url'].nil? || company['showcase_url'].empty?
    output << generate_header_and_body('Showcase URL',
                                       company['showcase_url'])
  end

  return output
end

#display_job(job_id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wfhcli.rb', line 84

def display_job(job_id)
  output = ''
  job = self.job(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)

  output << generate_header_and_body('Title', title)
  output << generate_header_and_body('Category', category)
  output << generate_header_and_body('Posted', posted)
  output << generate_header_and_body('Description', job['description'])
  output << generate_header_and_body('Application Info',
                                     job['application_info'])
  output << generate_header_and_body('Country', country)
  unless job['location'].nil? || job['location'].empty?
    output << generate_header_and_body('Location', job['location'])
  end

  return output
end

#display_jobs(page, category_id = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wfhcli.rb', line 113

def display_jobs(page, category_id=nil)
  jobs = self.jobs(page, category_id)

  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

    generate_table(content)
  else
    'No jobs found'
  end
end

#job(id) ⇒ Object



134
135
136
# File 'lib/wfhcli.rb', line 134

def job(id)
  get_json("/jobs/#{id}")
end

#jobs(page = 1, category_id = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/wfhcli.rb', line 138

def jobs(page=1, category_id=nil)
  if category_id.nil?
    uri = "/jobs?page=#{page}"
  else
    uri = "/categories/#{category_id}/jobs?page=#{page}"
  end

  get_json(uri)
end