Class: Mihari::Clients::ZoomEye

Inherits:
Base
  • Object
show all
Defined in:
lib/mihari/clients/zoomeye.rb

Overview

ZoomEye API client

Constant Summary collapse

PAGE_SIZE =
10

Instance Attribute Summary collapse

Attributes inherited from Base

#base_url, #headers, #pagination_interval, #timeout

Instance Method Summary collapse

Constructor Details

#initialize(base_url = "https://api.zoomeye.org", api_key:, headers: {}, pagination_interval: Mihari.config.pagination_interval, timeout: nil) ⇒ ZoomEye

Returns a new instance of ZoomEye.

Parameters:

  • base_url (String) (defaults to: "https://api.zoomeye.org")
  • api_key (String, nil)
  • headers (Hash) (defaults to: {})
  • pagination_interval (Integer) (defaults to: Mihari.config.pagination_interval)
  • timeout (Integer, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mihari/clients/zoomeye.rb', line 20

def initialize(
  base_url = "https://api.zoomeye.org",
  api_key:,
  headers: {},
  pagination_interval: Mihari.config.pagination_interval,
  timeout: nil
)
  raise(ArgumentError, "api_key is required") unless api_key

  headers["api-key"] = api_key
  super(base_url, headers:, pagination_interval:, timeout:)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/mihari/clients/zoomeye.rb', line 11

def api_key
  @api_key
end

Instance Method Details

#host_search(query, page: nil, facets: nil) ⇒ Hash

Search the Host devices

Parameters:

  • query (String)

    Query string

  • page (Integer, nil) (defaults to: nil)

    The page number to paging(default:1)

  • facets (String, nil) (defaults to: nil)

    A comma-separated list of properties to get summary information on query

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
# File 'lib/mihari/clients/zoomeye.rb', line 49

def host_search(query, page: nil, facets: nil)
  params = {
    query:,
    page:,
    facets:
  }.compact
  get_json "/host/search", params:
end

#host_search_with_pagination(query, facets: nil, pagination_limit: Mihari.config.pagination_limit) ⇒ Enumerable<Hash>

Parameters:

  • query (String)
  • facets (String, nil) (defaults to: nil)
  • pagination_limit (Integer) (defaults to: Mihari.config.pagination_limit)

Returns:

  • (Enumerable<Hash>)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mihari/clients/zoomeye.rb', line 65

def host_search_with_pagination(query, facets: nil, pagination_limit: Mihari.config.pagination_limit)
  Enumerator.new do |y|
    (1..pagination_limit).each do |page|
      res = host_search(query, facets:, page:)

      break if res.nil?

      y.yield res

      total = res["total"].to_i
      break if total <= page * PAGE_SIZE

      sleep_pagination_interval
    end
  end
end

#http::HTTP::Client

Returns:

  • (::HTTP::Client)


36
37
38
# File 'lib/mihari/clients/zoomeye.rb', line 36

def http
  @http ||= HTTP::Factory.build(headers:, timeout:, raise_exception: false)
end

#web_search(query, page: nil, facets: nil) ⇒ Hash

Search the Web technologies

Parameters:

  • query (String)

    Query string

  • page (Integer, nil) (defaults to: nil)

    The page number to paging(default:1)

  • facets (String, nil) (defaults to: nil)

    A comma-separated list of properties to get summary information on query

Returns:

  • (Hash)


91
92
93
94
95
96
97
98
# File 'lib/mihari/clients/zoomeye.rb', line 91

def web_search(query, page: nil, facets: nil)
  params = {
    query:,
    page:,
    facets:
  }.compact
  get_json "/web/search", params:
end

#web_search_with_pagination(query, facets: nil, pagination_limit: Mihari.config.pagination_limit) ⇒ Enumerable<Hash>

Parameters:

  • query (String)
  • facets (String, nil) (defaults to: nil)
  • pagination_limit (Integer) (defaults to: Mihari.config.pagination_limit)

Returns:

  • (Enumerable<Hash>)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mihari/clients/zoomeye.rb', line 107

def web_search_with_pagination(query, facets: nil, pagination_limit: Mihari.config.pagination_limit)
  Enumerator.new do |y|
    (1..pagination_limit).each do |page|
      res = web_search(query, facets:, page:)

      break if res.nil?

      y.yield res

      total = res["total"].to_i
      break if total <= page * PAGE_SIZE

      sleep_pagination_interval
    end
  end
end