Class: Cli

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



4
5
6
# File 'lib/cli.rb', line 4

def initialize
  @theData = TheData.new
end

Instance Attribute Details

#theDataObject

Returns the value of attribute theData.



2
3
4
# File 'lib/cli.rb', line 2

def theData
  @theData
end

Instance Method Details

#display_in_browserObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cli.rb', line 79

def display_in_browser
  link = @theData.post[:link]
  puts "Type b to display in browser or any key to quit".colorize(:color => :cyan)
  browser = STDIN.gets.chomp
  if browser.upcase == 'B'
    begin
       Launchy.open(link)
    rescue
      puts "Couldn't access browser. Link will be copied to clipboard instead.".colorize(:red)
      Clipboard.copy(link)
      puts "#{link.colorize(:light_blue)} copied to cliboard!"
    end
  end
end

#display_postObject



70
71
72
73
74
75
76
77
# File 'lib/cli.rb', line 70

def display_post
  puts "\n#{"Title:".colorize(:color => :light_white)} #{@theData.post[:title].colorize(:color => :light_green)}"
  puts "#{"Author:".colorize(:color => :light_white)} #{@theData.post[:author].colorize(:color => :green)}"
  puts "#{"Posted on:".colorize(:color => :light_white)} #{@theData.post[:time].colorize(:color => :yellow)}"
  puts "#{"Link:".colorize(:color => :light_white)} #{@theData.post[:link].colorize(:color => :light_blue)}"
  puts ""
  display_in_browser
end

#get_categoriesObject



28
29
30
# File 'lib/cli.rb', line 28

def get_categories
  @theData.get_categories
end

#get_selected_categoryObject



49
50
51
# File 'lib/cli.rb', line 49

def get_selected_category
  @theData.get_selected_category
end

#list(array) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/cli.rb', line 18

def list(array)
  array.each_with_index do |category, index|
    if index.even? 
      puts "#{index+1}. #{category[:name]}".colorize(:background => :red, :color => :white) 
    else 
      puts "#{index+1}. #{category[:name]}".colorize(:background => :light_black, :color => :white)
    end
  end 
end

#runObject



8
9
10
11
12
13
14
15
16
# File 'lib/cli.rb', line 8

def run
  get_categories
  select_categories

  get_selected_category
  select_specific_post

  display_post
end

#select_categoriesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cli.rb', line 32

def select_categories
  list(@theData.all_categories)
  selection = nil
  while !selection or selection == "" or !selection.to_i.between?(1, @theData.all_categories.count)
    puts "please select a category"
    selection = gets.chomp
    if selection.upcase == 'LIST'
      list(@theData.all_categories)
      selection = nil
      next
    elsif selection.upcase == 'Q'
      exit
    end
  end
  @theData.select_category(@theData.all_categories[selection.to_i-1])
end

#select_specific_postObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cli.rb', line 53

def select_specific_post
  list(@theData.category)
  selection = nil
  while !selection or selection == "" or !selection.to_i.between?(1, @theData.category.count)
    puts "please select a post"
    selection = gets.chomp
    if selection.upcase == 'LIST'
      list(@theData.category)
      selection = nil
      next
    elsif selection.upcase == 'Q'
      exit
    end
  end
  @theData.select_specific_post(@theData.category[selection.to_i-1])
end