Class: Google::Csene::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/google/csene/client.rb

Constant Summary collapse

TOKEN_CREDENTIAL_URI =
'https://accounts.google.com/o/oauth2/token'
AUDIENCE =
'https://accounts.google.com/o/oauth2/token'
SCOPE =
'https://www.googleapis.com/auth/cse'

Instance Method Summary collapse

Constructor Details

#initialize(application_name: 'google-csene', application_version: '1.0', api_key:, cse_id:) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/google/csene/client.rb', line 8

def initialize(application_name: 'google-csene',
               application_version: '1.0',
               api_key:,
               cse_id:)
  @application_name = application_name
  @application_version = application_version
  @api_key = api_key
  @cse_id = cse_id
end

Instance Method Details

seems working for the moment (maybe Google has stopped providing this?)



77
78
79
80
81
82
83
84
85
86
# File 'lib/google/csene/client.rb', line 77

def backlink_count(site)
  result = client.execute(
    service.cse.list,
    default_options.merge(
      'q' => "link:#{site}"
    )
  )

  result.data.search_information.total_results
end

#clientObject



18
19
20
21
22
23
24
# File 'lib/google/csene/client.rb', line 18

def client
  @client ||= Google::APIClient.new(
    application_name: @application_name,
    application_version: @application_version,
    authorization: nil
  )
end

#highest_search_rank_site_url(query) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/google/csene/client.rb', line 53

def highest_search_rank_site_url(query)
  result = client.execute(
    service.cse.list,
    default_options.merge(
      'start' => 1,
      'q' => query
    )
  )

  result.data.items.first.display_link
end

#index_count(site) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/google/csene/client.rb', line 65

def index_count(site)
  result = client.execute(
    service.cse.list,
    default_options.merge(
      'q' => "site:#{site}"
    )
  )

  result.data.search_information.total_results
end

#search_rank(query, link, page = 1) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google/csene/client.rb', line 26

def search_rank(query, link, page = 1)
  start = (page - 1) * 10 + 1
  result = client.execute(
    service.cse.list,
    default_options.merge(
      'start' => start,
      'q' => query
    )
  )
  queries = result.data.queries
  has_next_page = !queries.nil? && queries['nextPage'].count > 0
  has_result = result.data.items.count > 0

  return 0 unless has_result

  result.data.items.each_with_index do |item, i|
    # puts "start: #{start}, i: #{i} rank: #{start + i}, query: #{query}, link: #{item.link}"
    if item.link =~ link
      return start + i
    elsif item.link == link
      return start + i
    end
  end

  search_rank(query, link, page + 1) if has_next_page
end