Class: Cli

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

Class Method Details

.audio_searchObject

Gets search terms and creates Content objects based on the provided results



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/services/cli.rb', line 109

def self.audio_search
  Content.all.clear
  puts "\n\nPlease enter your audio search terms.\n\n"
  terms = gets.chomp!
  terms = terms.split(" ").join("%20")

  search_results = CliNasaAPI.media_search("audio", terms)
  search_results = search_results["collection"]["items"]

  search_results.each do |result|
    attributes = result["data"][0]
    links = result["links"][0] if result["links"] != nil

    title = attributes["title"]
    description = attributes["description"]
    keywords = attributes["keywords"]
    link = links["href"] if links != nil

    Content.add_content(title, description, keywords, link)
  end
end

.display_content(results) ⇒ Object

Method for displaying a piece of content, navigating thorugh its data, and prompting the user for their next valid command



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/services/cli.rb', line 156

def self.display_content(results)
  input = ""
  page = 1

  while input != "s"

    current = results[page-1]
    puts "\n\nPage #{page}: #{current.title}"
    if current.link == nil
      puts "Link: link not available"
    else
      puts "Link: #{current.link}"
    end
    puts "\n\nHere are your available commands:"
    puts ">> k - Displays the content's keywords"
    puts ">> d - Displays the content's description."
    puts ">> n - Shows the next result"
    puts ">> p - Shows the previous result"
    puts ">> s - Start a new search."

    input = self.get_input()
    case input
    when "k"
      puts "\n\nKeywords : #{current.keywords}\n"
    when "d"
      puts "\n\nDescription : #{current.description}\n"
    when "n"
      if page < results.length
        page += 1
      else
        puts "\n\nThere are no further results.\n"
      end
    when "p"
      if page > 1
         page -= 1
      else
         puts "\n\nYou're at the top of your results.\n"
       end
    when "s"
      self.main_menu
    else
      puts "\n\nCommand not recognized, please try again.\n"
    end
  end
end

.get_inputObject

Prompts the user for input



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

def self.get_input
  puts "\n\nPlease make a selection.\n\n"
  input = gets.chomp!
  input
end

.greetingObject

Welcomes users and provides the list of available commands



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

def self.greeting
  puts "\n\nWelcome to the NASA-CLI, here are your available commands: "
  puts ">> s - a keyword search of the Nasa Image and Video Library"
  puts ">> i - a search of the library focused for only images"
  puts ">> a - a search of the library focused for only audio clips"
  puts ">> v - a search of the library focused for only videos"
  puts ">> exit - exit NASA-CLI\n"
end

.image_searchObject

Gets search terms and creates Content objects based on the provided results



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/services/cli.rb', line 86

def self.image_search
  Content.all.clear
  puts "\n\nPlease enter your image search terms.\n\n"
  terms = gets.chomp!
  terms = terms.split(" ").join("%20")

  search_results = CliNasaAPI.media_search("image", terms)
  search_results = search_results["collection"]["items"]

  search_results.each do |result|
    attributes = result["data"][0]
    links = result["links"][0]

    title = attributes["title"]
    description = attributes["description"]
    keywords = attributes["keywords"]
    link = links["href"]

    Content.add_content(title, description, keywords, link)
  end
end

.keyword_searchObject

Gets search terms and creates Content objects based on the provided results



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/services/cli.rb', line 63

def self.keyword_search
  Content.all.clear
  puts "\n\nPlease enter your search terms.\n\n"
  terms = gets.chomp!
  terms = terms.split(" ").join("%20")

  search_results = CliNasaAPI.basic_search(terms)
  search_results = search_results["collection"]["items"]

  search_results.each do |result|
    attributes = result["data"][0]
    links = result["links"][0] if result["links"] != nil

    title = attributes["title"]
    description = attributes["description"]
    keywords = attributes["keywords"]
    link = links["href"] if links != nil

    Content.add_content(title, description, keywords, link)
  end
end

Brings up .greeting, begins the search process, and then displays the provided results



11
12
13
14
15
# File 'lib/services/cli.rb', line 11

def self.main_menu
  self.greeting
  results = self.search_menu_command()
  self.display_content(results)
end

.runObject

Method called at startup that brings up main menu



6
7
8
# File 'lib/services/cli.rb', line 6

def self.run
  self.main_menu
end

.search_menu_commandObject

Runs the specific search requested by the user and returns results as an array of Content objects, asks the user to submit a valid command, or exits the program



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/services/cli.rb', line 37

def self.search_menu_command
    command = self.get_input()

    case command
    when "s"
      self.keyword_search()
      Content.all
    when "i"
      self.image_search()
      Content.all
    when "a"
      self.audio_search()
      Content.all
    when "v"
      self.video_search()
      Content.all
    when "exit"
      puts "\n\nThank you, please come again!"
      exit
    else
      puts "\n\nCommand not recognized, please try again."
      self.main_menu
    end
end

.video_searchObject

Gets search terms and creates Content objects based on the provided results



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/services/cli.rb', line 132

def self.video_search
  Content.all.clear
  puts "\n\nPlease enter your video search terms.\n\n"
  terms = gets.chomp!
  terms = terms.split(" ").join("%20")

  search_results = CliNasaAPI.media_search("video", terms)
  search_results = search_results["collection"]["items"]

  search_results.each do |result|
    attributes = result["data"][0]
    links = result["links"][0] if result["links"] != nil

    title = attributes["title"]
    description = attributes["description"]
    keywords = attributes["keywords"]
    link = links["href"] if links != nil

    Content.add_content(title, description, keywords, link)
  end
end