Class: GScraper::SponsoredLinks

Inherits:
Array
  • Object
show all
Defined in:
lib/gscraper/sponsored_links.rb

Instance Method Summary collapse

Constructor Details

#initialize(ads = []) {|links| ... } ⇒ SponsoredLinks

Creates a new SponsoredLinks object.

Parameters:

  • ads (Array) (defaults to: [])

    The ads to populate the sponsored links object with.

Yields:

  • (links)

    If a block is given, it will be passed the new sponsored links object.

Yield Parameters:



39
40
41
42
43
# File 'lib/gscraper/sponsored_links.rb', line 39

def initialize(ads=[])
  super(ads)

  yield self if block_given?
end

Instance Method Details

#ads_with_direct_url(direct_url) {|ad| ... } ⇒ Array, Enumerator

Selects the ads with the matching direct URL.

Examples:

sponsored.ads_with_direct_url(/\.com/)
# => SponsoredLinks

Parameters:

  • direct_url (String, Regexp)

    The direct URL to search for.

Yields:

  • (ad)

    Each matching ad will be passed to the given block.

Yield Parameters:

  • ad (SponsoredAd)

    A sponsored ad with the matching direct URL.

Returns:

  • (Array, Enumerator)

    The sponsored ads with the matching URL. If no block is given, an Enumerator object will be returned.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# 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

#ads_with_title(title) {|ad| ... } ⇒ Array, Enumerator

Selects the ads with the matching title.

Examples:

sponsored.ads_with_title('be attractive')
# => SponsoredLinks
sponsored.ads_with_title(/buy me/) do |ad|
  puts ad.url
end

Parameters:

  • title (String, Regexp)

    The title to search for.

Yields:

  • (ad)

    Each matching ad will be passed to the given block.

Yield Parameters:

  • ad (SponsoredAd)

    A sponsored ad with the matching title.

Returns:

  • (Array, Enumerator)

    The sponsored ads with the matching title. If no block is given, an Enumerator object will be returned.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

#ads_with_url(url) {|ad| ... } ⇒ Array, Enumerator

Selects the ads with the matching URL.

Examples:

sponsored.ads_with_url(/\.com/)
# => SponsoredLinks

Parameters:

  • url (String, Regexp)

    The URL to search for.

Yields:

  • (ad)

    Each matching ad will be passed to the given block.

Yield Parameters:

  • ad (SponsoredAd)

    A sponsored ad with the matching URL.

Returns:

  • (Array, Enumerator)

    The sponsored ads with the matching URL. If no block is given, an Enumerator object will be returned.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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

#direct_urlsArray<URI::HTTP>

The direct URLs for the ads.

Examples:

sponsored.direct_urls # => [...]

Returns:

  • (Array<URI::HTTP>)

    The direct URLs for the ads.



324
325
326
# File 'lib/gscraper/sponsored_links.rb', line 324

def direct_urls
  each_direct_url.to_a
end

#direct_urls_of {|ad| ... } ⇒ Array<String>

The direct URLs of the selected ads.

Examples:

sponsored.urls_of { |ad| ad.title =~ /buy these pants/ }

Yields:

  • (ad)

    The given block will be passed each ad to be selected.

Yield Parameters:

Returns:

  • (Array<String>)

    The direct URLs of the selected ads.



381
382
383
# File 'lib/gscraper/sponsored_links.rb', line 381

def direct_urls_of(&block)
  ads_with(&block).direct_urls
end

#each_direct_url {|direct_url| ... } ⇒ Enumerator

Iterates over the direct URLs of each ad.

Examples:

each_direct_url { |url| puts url }

Yields:

  • (direct_url)

    The given block will be passed each direct URL.

Yield Parameters:

  • direct_url (URI::HTTP)

    A direct URL of an ad.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



281
282
283
284
285
286
287
# 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

#each_title {|title| ... } ⇒ Enumerator

Iterates over the titles of each ad.

Examples:

each_title { |title| puts title }

Yields:

  • (title)

    The given block will be passed each title.

Yield Parameters:

  • title (String)

    A title of an ad.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



235
236
237
238
239
240
241
# 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

#each_url {|url| ... } ⇒ Enumerator

Iterates over the URLs of each ad.

Examples:

each_url { |url| puts url }

Yields:

  • (url)

    The given block will be passed each URL.

Yield Parameters:

  • url (URI::HTTP)

    An URL of an ad.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



258
259
260
261
262
263
264
# 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

#map {|ad| ... } ⇒ Array, Enumerator

Maps the sponsored ads.

Examples:

sponsored.map
# => SponsoredLinks
sponsored.map { |ad| ad.url }
# => [...]

Yields:

  • (ad)

    The given block will be passed each ad.

Yield Parameters:

Returns:

  • (Array, Enumerator)

    The mapped result. If no block was given, an Enumerator object will be returned.



66
67
68
69
70
71
72
73
# 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

#select {|ad| ... } ⇒ Array, Enumerator Also known as: ads_with

Selects the ads within the sponsored links.

Examples:

sponsored.select { |ad| ad.title =~ /consume/i }

Yields:

  • (ad)

    The given block will determine which ads to select.

Yield Parameters:

Returns:

  • (Array, Enumerator)

    The selected ads. If no block is given, an Enumerator object will be returned.



91
92
93
94
95
96
97
# File 'lib/gscraper/sponsored_links.rb', line 91

def select(&block)
  unless block
    enum_for(:select)
  else
    SponsoredLinks.new(super(&block))
  end
end

#titlesArray<String>

The titles for the ads.

Examples:

sponsored.titles # => [...]

Returns:

  • (Array<String>)

    The titles for the ads.



298
299
300
# File 'lib/gscraper/sponsored_links.rb', line 298

def titles
  each_title.to_a
end

#titles_of {|ad| ... } ⇒ Array<String>

The titles of the selected ads.

Examples:

sponsored.titles_of { |ad| ad.url.include?('www') }

Yields:

  • (ad)

    The given block will be passed each ad to be selected.

Yield Parameters:

Returns:

  • (Array<String>)

    The titles of the selected ads.



343
344
345
# File 'lib/gscraper/sponsored_links.rb', line 343

def titles_of(&block)
  ads_with(&block).titles
end

#urlsArray<URI::HTTP>

The URLs for the ads.

Examples:

sponsored.urls # => [...]

Returns:

  • (Array<URI::HTTP>)

    The URLs for the ads.



311
312
313
# File 'lib/gscraper/sponsored_links.rb', line 311

def urls
  each_url.to_a
end

#urls_of {|ad| ... } ⇒ Array<String>

The URLs of the selected ads.

Examples:

sponsored.urls_of { |ad| ad.title =~ /buy these pants/ }

Yields:

  • (ad)

    The given block will be passed each ad to be selected.

Yield Parameters:

Returns:

  • (Array<String>)

    The URLs of the selected ads.



362
363
364
# File 'lib/gscraper/sponsored_links.rb', line 362

def urls_of(&block)
  ads_with(&block).urls
end