Class: DribbbleBucketApi::ShotIndexParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dribbble_bucket_api/shot_index_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, options = {}) ⇒ ShotIndexParser

Returns a new instance of ShotIndexParser.



8
9
10
11
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 8

def initialize(body, options = {})
  @body = body
  @options = options
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 6

def body
  @body
end

Instance Method Details

#current_pageObject



24
25
26
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 24

def current_page
  @options[:page] || 1
end

#next_pageObject



28
29
30
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 28

def next_page
  current_page + 1 if current_page < total_pages
end

#previous_pageObject



32
33
34
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 32

def previous_page
  current_page - 1 if current_page > 1
end

#shotsObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 13

def shots
  @shots ||= document.css(".dribbbles > li").map do |shot|
    # parse shot data from HTML
    id = shot["id"] =~ /^screenshot\-(\d+)$/ && $1.to_i
    img_src = shot.css(".dribbble-img img").first["src"]
    url = "http://dribbble.com" + shot.css("a.dribbble-link").first["href"]
    # pass data into shot object
    Shot.new(id: id, image_teaser_url: img_src, url: url)
  end
end

#total_entriesObject



36
37
38
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 36

def total_entries
  @total_entries ||= (document.css("#main h2.section").text =~ /^(\d+).*$/ && $1.to_i) || 0
end

#total_pagesObject



40
41
42
# File 'lib/dribbble_bucket_api/shot_index_parser.rb', line 40

def total_pages
  total_entries > 0 ? (total_entries.to_f / 15).ceil : 0
end