Class: Pinteresting::Pins

Inherits:
Object
  • Object
show all
Defined in:
lib/pinteresting/pins.rb

Class Method Summary collapse

Class Method Details

.get_pins(page) ⇒ Object



22
23
24
# File 'lib/pinteresting/pins.rb', line 22

def self.get_pins(page)
  page.search('//div[@class="pin"]')
end

.get_search_term_page(search_term, a) ⇒ Object



18
19
20
# File 'lib/pinteresting/pins.rb', line 18

def self.get_search_term_page(search_term, a)
  a.get("http://pinterest.com/search/pins/?q=#{search_term}")
end

.parse(search_term, a) ⇒ Object



12
13
14
15
16
# File 'lib/pinteresting/pins.rb', line 12

def self.parse(search_term, a)
  page = get_search_term_page(search_term, a)
  pins = get_pins(page)
  parse_pins(pins)
end

.parse_pin(pin) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/pinteresting/pins.rb', line 32

def self.parse_pin(pin)
  {
    :image_url => parse_pin_image_url(pin), 
    :repins => parse_pin_repins(pin),
    :pinterest_id => parse_pin_id(pin),
    :url => parse_pin_url(pin),
    :pinner => parse_pin_pinner(pin),
    :likes => parse_pin_likes(pin)
  }
end

.parse_pin_id(pin) ⇒ Object



52
53
54
55
56
# File 'lib/pinteresting/pins.rb', line 52

def self.parse_pin_id(pin)
  # pin_id = pin.search(".//div[@class='PinHolder']/a/@href").text
  # pin_id = pin.attributes["data-id"].value
  pin.search("./@data-id").text
end

.parse_pin_image_url(pin) ⇒ Object



43
44
45
# File 'lib/pinteresting/pins.rb', line 43

def self.parse_pin_image_url(pin)
  pin.search(".//a[@class='PinImage ImgLink']/img/@src").text
end

.parse_pin_likes(pin) ⇒ Object



68
69
70
71
# File 'lib/pinteresting/pins.rb', line 68

def self.parse_pin_likes(pin)
  pin_likes = pin.search(".//p[@class='stats colorless']/span[@class='LikesCount']").text
  pin_likes[/(\d+)/,1]
end

.parse_pin_pinner(pin) ⇒ Object



64
65
66
# File 'lib/pinteresting/pins.rb', line 64

def self.parse_pin_pinner(pin)
  pin.search(".//div[@class='convo attribution clearfix']/a/@href").text
end

.parse_pin_repins(pin) ⇒ Object



47
48
49
50
# File 'lib/pinteresting/pins.rb', line 47

def self.parse_pin_repins(pin)
  pin_repins = pin.search(".//p[@class='stats colorless']/span[@class='RepinsCount']").text
  pin_repins[/(\d+)/,1]
end

.parse_pin_url(pin) ⇒ Object



58
59
60
61
62
# File 'lib/pinteresting/pins.rb', line 58

def self.parse_pin_url(pin)
  # TODO RYAN -- How can I DRY this up?
  pin_url = pin.search("./@data-id").text
  "http://pinterest.com/pin/#{pin_url}"
end

.parse_pins(pins) ⇒ Object



26
27
28
29
30
# File 'lib/pinteresting/pins.rb', line 26

def self.parse_pins(pins)
  pins.map do |pin|
    parse_pin(pin)
  end
end

.search(search_term) ⇒ Object



6
7
8
9
10
# File 'lib/pinteresting/pins.rb', line 6

def self.search(search_term)
  puts "Searching Pinterest for #{search_term}..."
  a = Mechanize.new
  parse(search_term, a)
end