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

Returns a new instance of 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
# File 'lib/john_stamos/pin_search.rb', line 35

def first_retrieval!
  page = @client.page_content(first_retrieval_url)
  embedded_script_json = JohnStamos::ExtractionHelper.embedded_page_json(page)
  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

Returns:

  • (Boolean)


60
61
62
# File 'lib/john_stamos/pin_search.rb', line 60

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

#more_results?Boolean

Returns:

  • (Boolean)

Raises:



55
56
57
58
# File 'lib/john_stamos/pin_search.rb', line 55

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

#pinsObject



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

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

#subsequent_retrieval!Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/john_stamos/pin_search.rb', line 44

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