Class: App

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

Instance Method Summary collapse

Constructor Details

#initializeApp

creates a new CLI and Scrape object upon initialization


3
4
5
6
# File 'lib/askoverflow/app.rb', line 3

def initialize
    @ui = CLI.new
    @scraper = Scrape.new
end

Instance Method Details

#f_query(raw) ⇒ Object

formats the user’s search string into the stackoverflow url query format


9
10
11
12
# File 'lib/askoverflow/app.rb', line 9

def f_query(raw)
    fq = raw.gsub(" ", "+")
    return "q=#{fq}"
end

#make_url(query) ⇒ Object

take the user’s raw search string and return a url


14
15
16
17
18
19
# File 'lib/askoverflow/app.rb', line 14

def make_url(query)
    base = 'https://stackoverflow.com/search?'
    q = f_query(query)
    answeredOnly = "+hasaccepted%3Atrue"
    return "#{base}#{q}#{answeredOnly}"
end

#runObject

this is the main program control


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/askoverflow/app.rb', line 21

def run
    # greets the user on start
    @ui.greet
    # main loop
    running = true
    while running
        # preemptively clear Results for multiple searches
        # prompt the user for their search terms
        # exit if desired by user
        # otherwise scrape results from query
        Result.clear_results
        @ui.prompt_search
        query = gets.strip
        break if query == 'exit'
        url = make_url(query)
        @scraper.scrape_results(url)
        # "viewing loop"
        # this loop allows the user to return to the results
        # display or to try a different search
        # displays scraped results, prompts the user for a
        # specific result, to return to try another search
        # or exit the program
        viewing_results = true
        while viewing_results
            @ui.display_results
            @ui.prompt_result
            result_id = gets.strip
            if result_id == 'exit'
                running = false 
                break
            end
            break if result_id == 'back'
            # Scrapes the specific result, and adds data
            chosen_result = Result.find_by_id(result_id)
            @scraper.scrape_specific(chosen_result)
            # "Specific View Loop"
            # this loop allows us to view a specific result
            # and subsequently return to the results list
            viewing_result = true
            while viewing_result
                @ui.display_question(chosen_result)
                @ui.prompt_next
                @ui.display_answer(chosen_result)
                @ui.prompt_return
                break
            end
        end
    end
    @ui.goodbye
end