class GScraper::SponsoredLinks
Public Class Methods
Creates a new SponsoredLinks
object.
@param [Array] ads
The ads to populate the sponsored links object with.
@yield [links]
If a block is given, it will be passed the new sponsored links object.
@yieldparam [SponsoredLinks] links
The new sponsored links object.
# File lib/gscraper/sponsored_links.rb, line 39 def initialize(ads=[]) super(ads) yield self if block_given? end
Public Instance Methods
Selects the ads with the matching direct URL.
@param [String, Regexp] direct_url
The direct URL to search for.
@yield [ad]
Each matching ad will be passed to the given block.
@yieldparam [SponsoredAd] ad
A sponsored ad with the matching direct URL.
@return [Array, Enumerator]
The sponsored ads with the matching URL. If no block is given, an Enumerator object will be returned.
@example
sponsored.ads_with_direct_url(/\.com/) # => SponsoredLinks
# File lib/gscraper/sponsored_links.rb, line 202 def ads_with_direct_url(direct_url) return enum_for(:ads_with_direct_url,direct_url) unless block_given? comparitor = if direct_url.kind_of?(Regexp) lambda { |ad| ad.direct_url =~ direct_url } else lambda { |ad| ad.direct_url == direct_url } end return ads_with do |ad| if comparitor.call(ad) yield ad true end end end
Selects the ads with the matching title.
@param [String, Regexp] title
The title to search for.
@yield [ad]
Each matching ad will be passed to the given block.
@yieldparam [SponsoredAd] ad
A sponsored ad with the matching title.
@return [Array, Enumerator]
The sponsored ads with the matching title. If no block is given, an Enumerator object will be returned.
@example
sponsored.ads_with_title('be attractive') # => SponsoredLinks
@example
sponsored.ads_with_title(/buy me/) do |ad| puts ad.url end
# File lib/gscraper/sponsored_links.rb, line 126 def ads_with_title(title) return enum_for(:ads_with_title,title) unless block_given? comparitor = if title.kind_of?(Regexp) lambda { |ad| ad.title =~ title } else lambda { |ad| ad.title == title } end return ads_with do |ad| if comparitor.call(ad) yield ad true end end end
Selects the ads with the matching URL.
@param [String, Regexp] url
The URL to search for.
@yield [ad]
Each matching ad will be passed to the given block.
@yieldparam [SponsoredAd] ad
A sponsored ad with the matching URL.
@return [Array, Enumerator]
The sponsored ads with the matching URL. If no block is given, an Enumerator object will be returned.
@example
sponsored.ads_with_url(/\.com/) # => SponsoredLinks
# File lib/gscraper/sponsored_links.rb, line 164 def ads_with_url(url) return enum_for(:ads_with_url,url) unless block_given? comparitor = if url.kind_of?(Regexp) lambda { |ad| ad.url =~ url } else lambda { |ad| ad.url == url } end return ads_with do |ad| if comparitor.call(ad) yield ad true end end end
The direct URLs for the ads.
@return [Array<URI::HTTP>]
The direct URLs for the ads.
@example
sponsored.direct_urls # => [...]
# File lib/gscraper/sponsored_links.rb, line 324 def direct_urls each_direct_url.to_a end
The direct URLs of the selected ads.
@yield [ad]
The given block will be passed each ad to be selected.
@yieldparam [SponsoredAd] ad
An ad to be selected.
@return [Array<String>]
The direct URLs of the selected ads.
@example
sponsored.urls_of { |ad| ad.title =~ /buy these pants/ }
# File lib/gscraper/sponsored_links.rb, line 381 def direct_urls_of(&block) ads_with(&block).direct_urls end
Iterates over the direct URLs of each ad.
@yield [direct_url]
The given block will be passed each direct URL.
@yieldparam [URI::HTTP] direct_url
A direct URL of an ad.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
@example
each_direct_url { |url| puts url }
# File lib/gscraper/sponsored_links.rb, line 281 def each_direct_url unless block_given? enum_for(:each_direct_url) else each { |ad| yield ad.direct_url } end end
Iterates over the titles of each ad.
@yield [title]
The given block will be passed each title.
@yieldparam [String] title
A title of an ad.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
@example
each_title { |title| puts title }
# File lib/gscraper/sponsored_links.rb, line 235 def each_title unless block_given? enum_for(:each_title) else each { |ad| yield ad.title } end end
Iterates over the URLs of each ad.
@yield [url]
The given block will be passed each URL.
@yieldparam [URI::HTTP] url
An URL of an ad.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
@example
each_url { |url| puts url }
# File lib/gscraper/sponsored_links.rb, line 258 def each_url unless block_given? enum_for(:each_url) else each { |ad| yield ad.url } end end
Maps the sponsored ads.
@yield [ad]
The given block will be passed each ad.
@yieldparam [SponsoredAd] ad
The sponsored ad.
@return [Array, Enumerator]
The mapped result. If no block was given, an Enumerator object will be returned.
@example
sponsored.map # => SponsoredLinks
@example
sponsored.map { |ad| ad.url } # => [...]
# File lib/gscraper/sponsored_links.rb, line 66 def map return enum_for(:map) unless block_given? mapped = [] each { |ad| mapped << yield(ad) } return mapped end
Selects the ads within the sponsored links.
@yield [ad]
The given block will determine which ads to select.
@yieldparam [SponsoredAd] ad
A sponsored ad.
@return [Array, Enumerator]
The selected ads. If no block is given, an Enumerator object will be returned.
@example
sponsored.select { |ad| ad.title =~ /consume/i }
# File lib/gscraper/sponsored_links.rb, line 91 def select(&block) unless block enum_for(:select) else SponsoredLinks.new(super(&block)) end end
The titles for the ads.
@return [Array<String>]
The titles for the ads.
@example
sponsored.titles # => [...]
# File lib/gscraper/sponsored_links.rb, line 298 def titles each_title.to_a end
The titles of the selected ads.
@yield [ad]
The given block will be passed each ad to be selected.
@yieldparam [SponsoredAd] ad
An ad to be selected.
@return [Array<String>]
The titles of the selected ads.
@example
sponsored.titles_of { |ad| ad.url.include?('www') }
# File lib/gscraper/sponsored_links.rb, line 343 def titles_of(&block) ads_with(&block).titles end
The URLs for the ads.
@return [Array<URI::HTTP>]
The URLs for the ads.
@example
sponsored.urls # => [...]
# File lib/gscraper/sponsored_links.rb, line 311 def urls each_url.to_a end
The URLs of the selected ads.
@yield [ad]
The given block will be passed each ad to be selected.
@yieldparam [SponsoredAd] ad
An ad to be selected.
@return [Array<String>]
The URLs of the selected ads.
@example
sponsored.urls_of { |ad| ad.title =~ /buy these pants/ }
# File lib/gscraper/sponsored_links.rb, line 362 def urls_of(&block) ads_with(&block).urls end