Back to Parent

def determine_message x
    
    client = SODA::Client.new({:domain => 'explore.data.gov', :app_token => '1ItWOJ1oGYyvN9Ajo5UvHCSaa'})
    output = client.get("https://data.sfgov.org/resource/wwmu-gmzc.json", {"$where" => "title like '#{x}%'"})
    
    
    # ---------- if the response does not match with any film title, ask to try again ----------
    if output == []
        "I don't recall and film or TV series that match what you said. Maybe try again? (Note: case and space sensitive)"
   
        
    # ---------- if the response match with any film, move forward ----------
    else
        titles = []
        release_year = output.first["release_year"]
        locations = []
        fun_facts = []
        director = output.first["director"]
        writer = output.first["writer"]
        actors_1 = output.first["actor_1"].to_s + output.first["actor_2"].to_s + output.first["actor_3"].to_s


        output.each do |row|
            if row["title"]
                titles.push(row["title"])
            end  
        end

        titles = titles.uniq

        # ---------- if the response matches with more then 2 film titles ----------
        if titles.length >=2

            "There are actually quite a few. It'd really help if you can be more specific! For example: 'Star Trek IV' or 'Change Seasion 1'."

        # ---------- if the response matches with 2 film titles, ask ----------    
        elsif titles.length == 2
            "Do you mean '" + titles[0].to_s + "' or '" + titles[1].to_s + "'?"

        # ---------- if the response matches with 1 film title, produce answer ----------    
        elsif titles.length == 1

            output.each do |row|
                if row["locations"]
                    locations.push(row["locations"])
                end
                if row["fun_facts"].to_s != ""
                    fun_facts.push(row["fun_facts"])                    
                end    
            end

            # ---------- edit message about location ----------

            locations = locations.uniq
            
            if locations.length >=3
                location_list = locations.sample(2)
                location_1 = location_list[0].to_s
                location_2 = location_list[1].to_s
                message_location = output.length.to_s + " locations, including " + location_1 + ", " + location_2 + ", and more places in SF."
            elsif locations.length == 2
                location_list = locations.sample(2)
                location_1 = location_list[0].to_s
                location_2 = location_list[1].to_s
                message_location = output.length.to_s + " locations, including " + location_1 + " and " + location_2 + " in SF."
            elsif locations.length == 1
                location_1 = locations[0].to_s
                message_location = "one location – " + location_1 + " in SF."
            elsif locations.nil?
                message_location = "one location in SF." 
            end
            
            # ---------- edit message about fun fact ----------
            
            #fun_facts = fun_facts.uniq
            
            if fun_facts == []
                message_fun_facts = ""
            else
                message_fun_facts = " Also fun fact: " + fun_facts.sample.to_s
            end
            
            
            # ---------- print answer about the movie ----------
            titles[0].to_s + " was released in " + release_year + " and featured " + message_location + message_fun_facts
            
            
        end

    end
    
    
end
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0