Skip to content

Instantly share code, notes, and snippets.

@HopperMCS
Created July 20, 2020 21:31
Show Gist options
  • Save HopperMCS/8b59f35408afd3bd433a8dc873fa3a8b to your computer and use it in GitHub Desktop.
Save HopperMCS/8b59f35408afd3bd433a8dc873fa3a8b to your computer and use it in GitHub Desktop.
def self.api_public_key
begin
@@public = File.open(File.expand_path("~/.newsfeed_key")).read.strip
rescue
puts "No public key found. Please follow the instructions at https://newsapi.org to get your keys."
# create public key
puts "Please enter your public key:"
@@public = gets.strip
return if @@public == "exit"
File.open(File.expand_path("~/.newsfeed_key"), "w") do |file|
file.print @@public
end
end
@@public
end
@sanazaidichi
Copy link

module NatGeo
class API

    URL = "https://newsapi.org/v2/top-headlines?sources=national-geographic&apiKey="

    def self.get_stories 
        uri = URI.parse(URL+api_key)
        response = Net::HTTP.get_response(uri) #string 
        JSON.parse(response.body)["articles"] 
    end    
        
    
    def self.api_key
        begin 
            @@key = File.open(File.expand_path("~/.news-api-key")).read.strip 
        rescue 
            puts "Whoops! Looks like you have not added your API key"
            puts "Post it here"
            
            @@key = gets.strip   
            return if @@key == "exit"
            File.open(File.expand_path("~/.news-api-key"), "w") do |file|
                file.print @@key   
            end
        end
        @@key  
    end
end 

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment