Class: JohnStamos::PinSearch
- Inherits:
-
Object
- Object
- JohnStamos::PinSearch
- Defined in:
- lib/john_stamos/pin_search.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#next_bookmark ⇒ Object
Returns the value of attribute next_bookmark.
-
#pin_ids ⇒ Object
Returns the value of attribute pin_ids.
-
#search_text ⇒ Object
Returns the value of attribute search_text.
Instance Method Summary collapse
- #execute! ⇒ Object
- #first_retrieval! ⇒ Object
- #first_retrieval_url ⇒ Object
-
#initialize(client, search_text = nil, options = {}) ⇒ PinSearch
constructor
A new instance of PinSearch.
- #limit_reached? ⇒ Boolean
- #more_results? ⇒ Boolean
- #pins ⇒ Object
- #subsequent_retrieval! ⇒ Object
- #subsequent_retrieval_url ⇒ Object
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, ={}) = { limit: 50 } = .merge() @limit = .fetch(:limit) @client = client @search_text = search_text @pins, @pin_ids = [], [] @next_bookmark = nil end |
Instance Attribute Details
#limit ⇒ Object
Returns the value of attribute limit.
2 3 4 |
# File 'lib/john_stamos/pin_search.rb', line 2 def limit @limit end |
#next_bookmark ⇒ Object
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_ids ⇒ Object
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_text ⇒ Object
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) = JohnStamos::ExtractionHelper.(page) = pin_ids_from_first_retrieval() pin_ids_up_to_limit() @next_bookmark = next_bookmark_from_first_retrieval() end |
#first_retrieval_url ⇒ Object
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
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
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 |
#pins ⇒ Object
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_url ⇒ Object
31 32 33 |
# File 'lib/john_stamos/pin_search.rb', line 31 def subsequent_retrieval_url '/resource/SearchResource/get/' end |