Class: Climgur::ImgurGet
- Inherits:
-
Object
- Object
- Climgur::ImgurGet
- Defined in:
- lib/climgur/imgurget.rb
Instance Attribute Summary collapse
-
#images ⇒ Object
Returns the value of attribute images.
Instance Method Summary collapse
- #display_large_image(index) ⇒ Object
- #display_top_images ⇒ Object
-
#initialize ⇒ ImgurGet
constructor
A new instance of ImgurGet.
- #large_image(url) ⇒ Object
- #scrape_main_page ⇒ Object
- #small_image(url) ⇒ Object
Constructor Details
#initialize ⇒ ImgurGet
Returns a new instance of ImgurGet.
4 5 6 |
# File 'lib/climgur/imgurget.rb', line 4 def initialize @images = [] end |
Instance Attribute Details
#images ⇒ Object
Returns the value of attribute images.
2 3 4 |
# File 'lib/climgur/imgurget.rb', line 2 def images @images end |
Instance Method Details
#display_large_image(index) ⇒ Object
32 33 34 35 |
# File 'lib/climgur/imgurget.rb', line 32 def display_large_image(index) puts self.large_image(self.images[index.to_i-1][:full_url]) puts "(#{index}) "+self.images[index.to_i-1][:description] end |
#display_top_images ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/climgur/imgurget.rb', line 22 def display_top_images puts "the most popular images on imgur right now: \n --------" self.images.each_with_index do |x, index| puts self.small_image(x[:preview_url]) puts "(#{index+1}) "+x[:description] puts "\n" end self end |
#large_image(url) ⇒ Object
42 43 44 |
# File 'lib/climgur/imgurget.rb', line 42 def large_image(url) AsciiArt.new("http:"+url).to_ascii_art(color: true, width: 122) end |
#scrape_main_page ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/climgur/imgurget.rb', line 8 def scrape_main_page mainpage = Nokogiri::HTML(open("http://www.imgur.com")) mainpage.css("div.post").each do |preview| hash = {} hash[:description] = preview.css("div.hover p").text hash[:preview_url] = preview.css(".image-list-link img").first["src"] hash[:full_url] = preview.css(".image-list-link img").first["src"].gsub('b.jpg', '.jpg') #the only difference in URL between a full image and a preview is the letter 'b' on the end hash[:type] = preview.css("div.hover div.post-info").text #because I want to filter out galleries and animated gifs @images << hash end @images = @images.select {|x| x[:type].include?('image')} #filtering out only the images self end |
#small_image(url) ⇒ Object
38 39 40 |
# File 'lib/climgur/imgurget.rb', line 38 def small_image(url) AsciiArt.new("http:"+url).to_ascii_art(color: true, width: 44) end |