class UnsplashDownloader::Unsplash

Attributes

number_of_pages[RW]
photos_urls[RW]

Public Class Methods

new(verbose) click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 7
def initialize(verbose)
  @mechanize = Mechanize.new
  @all_elements = []
  @source = get_source
  @number_of_pages = count_pages
  @verbose = verbose
end

Public Instance Methods

count_pages() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 19
def count_pages
  @pagination = @source.search('div.pagination')
  @pagination.to_s.match(/(\d+)<\/a> <a class="next_page"/i).captures
end
get_single_page_elements(number) click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 24
def get_single_page_elements(number)
  @single_page_elements = @mechanize.get("https://unsplash.com/?page=#{number}").search('div.photo')
end
get_source() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 15
def get_source
  @mechanize.get('https://unsplash.com/')
end
merge_all_elements() click to toggle source
# File lib/unsplash_downloader/unsplash.rb, line 28
def merge_all_elements
  for index in 1..@number_of_pages[0].to_i + 1 do
    print "Merging pages: #{index - 1} of #{@number_of_pages[0]}" if @verbose
    get_single_page_elements index
    @single_page_elements.each do |element|
      @all_elements.unshift(element)
    end
    print "\r" if @verbose
  end
  puts "Merging pages: #{@number_of_pages[0]} of #{@number_of_pages[0]}. Done!" if @verbose
  return @all_elements
end