Class: Stars::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Client

Initializes a new Client.

Returns nothing.



15
16
17
18
19
20
21
22
23
# File 'lib/stars/client.rb', line 15

def initialize(cmd)
  Stars.config.prompt_for_username(cmd[1]) if cmd[0] == 'add'

  system "clear"
  puts "★  stars"

  display(cmd[0])
  star_loop
end

Instance Attribute Details

#postsObject

Returns the value of attribute posts.



9
10
11
# File 'lib/stars/client.rb', line 9

def posts
  @posts
end

Instance Method Details

#display(service = nil) ⇒ Object

Displays all of the star tables and information we have.

Returns nothing.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stars/client.rb', line 43

def display(service=nil)
  Stars.config.prompt_for_service if Stars.installed_services.empty?

  if service && service != 'add'
    if Stars.services.include? service
      posts = service.constantize.posts
    else
      puts "Service \"#{service}\" is unknown."
      exit
    end
  else
    posts = Stars.installed_services.collect{ |service| 
                              service.constantize.posts }.flatten
  end
  @posts = Post.filter(posts)
  puts print_posts(@posts)
end

#headingsObject

The headings used in the resulting printed table.

This returns an Array of headings.



95
96
97
98
99
100
101
102
# File 'lib/stars/client.rb', line 95

def headings
  [ 
    '',
    'Service',
    'Stars',
    {:value => 'The Hotness', :alignment => :center } 
  ]
end

This does the actual printing of posts.

posts - an Array of Post objects

It loops through the Array of posts and sends them to ‘terminal-table`.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stars/client.rb', line 78

def print_posts(posts)
  table do |t|
    t.headings = headings
    posts.each_with_index do |post,i|
      t << [
            { :value => i+1, :alignment => :right },
            post.service.capitalize,
            { :value => post.stars_count, :alignment => :center },
            post.short_name
           ]
    end
  end      
end

#show(id) ⇒ Object

Show more information about a particular post.

id - the Integer id entered by the user, which we map to a Post

Returns nothing (although does delegate to the Post to show #more).



66
67
68
69
70
71
# File 'lib/stars/client.rb', line 66

def show(id)
  post = @posts[id.to_i-1]
  return puts("\nMake a valid selection. Pretty please?\n") unless post
  puts post.more
  display
end

#star_loopObject

Run a loop FOREVER until we kill it or we make a selection.

Returns nothing.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stars/client.rb', line 28

def star_loop
  selection = ''
  while true
    puts "Type the number of the post that you want to learn about"
    print "  (or hit return to view all again, you ego-maniac)   >> "
    selection = $stdin.gets.chomp
    break if ['','q','quit','exit','fuckthis'].include?(selection.downcase)
    show(selection)
  end
  display if selection == ''
end