Class: JohnStamos::PinSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/john_stamos/pin_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, search_text = nil, options = {}) ⇒ PinSearch



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/john_stamos/pin_search.rb', line 4

def initialize(client, search_text=nil, options={})
  default_options = { limit: 50 }
  options = default_options.merge(options)
  @limit = options.fetch(:limit)

  @client = client
  @search_text = search_text

  @pins, @pin_ids = [], []
  @next_bookmark = nil
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



2
3
4
# File 'lib/john_stamos/pin_search.rb', line 2

def limit
  @limit
end

#next_bookmarkObject

Returns the value of attribute next_bookmark.



2
3
4
# File 'lib/john_stamos/pin_search.rb', line 2

def next_bookmark
  @next_bookmark
end

#pin_idsObject

Returns the value of attribute pin_ids.



2
3
4
# File 'lib/john_stamos/pin_search.rb', line 2

def pin_ids
  @pin_ids
end

#search_textObject

Returns the value of attribute search_text.



2
3
4
# File 'lib/john_stamos/pin_search.rb', line 2

def search_text
  @search_text
end

Instance Method Details

#execute!Object



16
17
18
19
20
21
22
23
24
# File 'lib/john_stamos/pin_search.rb', line 16

def execute!
  raise JohnStamos::MissingSearchText if @search_text.nil?

  first_retrieval!

  until limit_reached? do
    subsequent_retrieval!
  end
end

#first_retrieval!Object



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

def first_retrieval!
  page = @client.page_content(first_retrieval_url)

  embedded_script = page.search('script').select do |script|
    script['src'].nil? && script.content.include?('Pc.startArgs')
  end

  embedded_script_content = embedded_script.first.content
  # This regex used in the range snatches the parameter Pinterest uses to
  # start their app... This parameter happens to be a JSON representation of
  # the page.
  raw_json = embedded_script_content[/Pc.startArgs = (.*);/, 1]
  embedded_script_json = JSON.parse(raw_json)

  pin_ids_from_embedded_script_json = pin_ids_from_first_retrieval(embedded_script_json)

  pin_ids_up_to_limit(pin_ids_from_embedded_script_json)
  @next_bookmark = next_bookmark_from_first_retrieval(embedded_script_json)
end

#first_retrieval_urlObject



26
27
28
29
# File 'lib/john_stamos/pin_search.rb', line 26

def first_retrieval_url
  raise JohnStamos::MissingSearchText if @search_text.nil?
  "/search/pins/?q=#{URI::encode(@search_text)}"
end

#limit_reached?Boolean



71
72
73
# File 'lib/john_stamos/pin_search.rb', line 71

def limit_reached?
  @pin_ids.length == @limit.to_i
end

#more_results?Boolean



66
67
68
69
# File 'lib/john_stamos/pin_search.rb', line 66

def more_results?
  raise JohnStamos::MissingNextBookmark if @next_bookmark.nil?
  @next_bookmark != "-end-"
end

#pinsObject



75
76
77
78
79
# File 'lib/john_stamos/pin_search.rb', line 75

def pins
  @pin_ids.map do |pinterest_pin_id|
    JohnStamos::Pin.new(@client, pinterest_pin_id)
  end
end

#subsequent_retrieval!Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/john_stamos/pin_search.rb', line 55

def subsequent_retrieval!
  raise JohnStamos::MissingNextBookmark if @next_bookmark.nil?
  raise JohnStamos::MissingSearchText if @search_text.nil?

  pins_json = @client.json_content(subsequent_retrieval_url, build_url_params)
  pin_ids_from_json = pin_ids_from_subsequent_retrieval(pins_json)
  pin_ids_up_to_limit(pin_ids_from_json)

  @next_bookmark = next_bookmark_from_subsequent_retrieval(pins_json)
end

#subsequent_retrieval_urlObject



31
32
33
# File 'lib/john_stamos/pin_search.rb', line 31

def subsequent_retrieval_url
  '/resource/SearchResource/get/'
end