require 'nokogiri' require 'rest-client' require 'pry' class GithubGists attr_accessor :gist_base_url, :discover_url def initialize() @gist_base_url = 'https://gist.github.com' @discover_url = "#{gist_base_url}/discover" end def get_gists_from_discover # there are some url params we can use to fine tweak this. # - recently created # - last recently created # - recently updated # - last recently updated # we can add these params in instead of just one fetch of the regular discover_url response = RestClient::Request.execute( :method => :get, :url => @discover_url ) if response.code == 200 html = Nokogiri::HTML(response.body) else "Http Status: #{response.code}" end # this xpath should get us an array of urls of user gists: example: https://gist.github.com/joberding/3e408b8ec7ddb932ebef1b5b94a37255 html.xpath('//*[@class="link-overlay"]').map {|node| node.values[1]} end def get_raw_gist_from_url(url) # example input url: https://gist.github.com/joberding/3e408b8ec7ddb932ebef1b5b94a37255 # example output url: https://gist.githubusercontent.com/joberding/3e408b8ec7ddb932ebef1b5b94a37255/raw end def available_url_params { :recently_created => '?direction=desc&sort=created', :least_recently_created => '?direction=asc&sort=created', :recently_updated => '?direction=desc&sort=updated', :least_recently_updated => '?direction=asc&sort=updated' } end end