class FontPair::Fetcher

Fetches a copy of the pairs available on FontPair.co locally.

Constants

SOURCE_URL

Public Class Methods

new(filename) click to toggle source
# File lib/fontpair/fetcher.rb, line 7
def initialize(filename)
  @filename = filename.freeze
end

Public Instance Methods

fetch() click to toggle source
# File lib/fontpair/fetcher.rb, line 11
def fetch
  return initialize_pairs unless File::exists?(@filename)
  update_pairs
end

Private Instance Methods

fetch_pairs() click to toggle source
# File lib/fontpair/fetcher.rb, line 32
def fetch_pairs
  request = Net::HTTP.get(URI(SOURCE_URL))
  page = Nokogiri::HTML(request)

  page.css('.fontName').map { |pair| pair.content }
end
initialize_pairs() click to toggle source
# File lib/fontpair/fetcher.rb, line 18
def initialize_pairs
  pairs = fetch_pairs
  File::open(@filename, 'w') { |file| file.puts pairs.join("\n") }
end
update_pairs() click to toggle source
# File lib/fontpair/fetcher.rb, line 23
def update_pairs
  pairs = fetch_pairs

  File::open(@filename, 'w+') do |file|
    puts file.readlines
    file.puts pairs.join("\n") unless file.read == pairs
  end
end