Class: PintrestApi::Core

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/pintrest_api/core.rb

Overview

This class is the core of the pintrest api and handles all url visiting and such

Direct Known Subclasses

Board, Pin

Constant Summary collapse

PINTREST_URL =
'http://www.pintrest.com/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/pintrest_api/core.rb', line 9

def session
  @session
end

Class Method Details

.click(css) ⇒ Object



29
30
31
# File 'lib/pintrest_api/core.rb', line 29

def click(css)
  @session.find(css).click
end

.get_with_ajax_scroll(css_selector) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pintrest_api/core.rb', line 64

def get_with_ajax_scroll(css_selector)
  old_items_count = 0
  items = []

  until (items.count === old_items_count) && items.count > 0
    old_items_count = items.count
    scroll_page if old_items_count > 0
    newItems = Nokogiri::HTML.parse(html).css css_selector
    items = newItems if old_items_count === 0 || newItems.count > old_items_count
    puts "New Count: #{items.count}\nOld Count: #{old_items_count}"
  end

  items
end

.htmlObject

Returns the current session’s page



60
61
62
# File 'lib/pintrest_api/core.rb', line 60

def html
  @session.body
end

.new_sessionObject

Create a new PhantomJS session in Capybara



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pintrest_api/core.rb', line 34

def new_session
  # Register PhantomJS (aka poltergeist) as the driver to use
  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app)
  end

  # Use XPath as the default selector for the find method
  Capybara.default_selector = :css

  # Start up a new thread
  @session = Capybara::Session.new(:poltergeist)

  # Report using a particular user agent
  @session.driver.headers = { 'User-Agent' =>
    "Mozilla/5.0 (Macintosh; Intel Mac OS X)" }

  # Return the driver's session
  @session
end

.scroll_pageObject



54
55
56
57
# File 'lib/pintrest_api/core.rb', line 54

def scroll_page
  @session.execute_script 'window.scrollTo(0,100000)'
  sleep 5
end

.visit(url) ⇒ Object



22
23
24
25
26
27
# File 'lib/pintrest_api/core.rb', line 22

def visit(url)
  new_session
  @session.visit url
  sleep 5
  @session
end

.visit_page(user_name) ⇒ Object



15
16
17
18
19
20
# File 'lib/pintrest_api/core.rb', line 15

def visit_page(user_name)
  new_session
  @session.visit PINTREST_URL + user_name
  sleep 5
  @session
end