Class: Zoomeye::ZoomEye

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

Constant Summary collapse

@@home_url =
"http://api.zoomeye.org"
@@login_sub_url =
"/user/login"
@@resource_sub_url =
"/resources-info"
@@host_sub_url =
"/host/search"
@@web_sub_url =
"/web/search"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass) ⇒ ZoomEye

Returns a new instance of ZoomEye.



18
19
20
21
22
# File 'lib/zoomeye.rb', line 18

def initialize(user, pass)
  @user = user
  @pass = pass
  @token = nil
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



17
18
19
# File 'lib/zoomeye.rb', line 17

def user
  @user
end

Instance Method Details

#get(uri, params) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/zoomeye.rb', line 43

def get(uri, params)
  full_path = path_with_params(uri, params)
  uri = URI.parse(full_path)
  http = Net::HTTP.new(uri.host, uri.port)
  auth = "JWT " + @token
  return http.get(full_path, {'Authorization' => auth})
end

#host_search(query, page = nil, facets = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zoomeye.rb', line 71

def host_search(query, page=nil, facets=nil)
  params = {}
  params["query"] = query if query
  params["page"] = page if page
  params["facets"] = facets if facets
  res = get(@@home_url + @@host_sub_url, params)
  body = JSON.parse res.body
  if '200' == res.code
    return body
  else
    raise ZoomEyeError.new(res.code, body["message"])
  end
end

#loginObject



51
52
53
54
55
56
57
58
59
# File 'lib/zoomeye.rb', line 51

def 
  res = post(@@home_url + @@login_sub_url, {"username" => @user, "password" => @pass})
  body = JSON.parse res.body
  if '200' == res.code
    @token = body['access_token']
  else
    raise ZoomEyeError.new(res.code, body["message"])
  end
end

#path_with_params(uri, params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/zoomeye.rb', line 32

def path_with_params(uri, params)
  if params.length > 0
    param_list = []
    params.each do |key, val|
      param_list.push("#{key}=#{val}")
    end
    return [uri, param_list.join("&")].join("?")
  end
  return uri
end

#post(url, params) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/zoomeye.rb', line 24

def post(url, params)
  json_headers = {"Content-Type" => 'application/json',
                  "Accpet" => 'application/json'}
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  return http.post(uri.path, params.to_json, json_headers)
end

#resources_infoObject



61
62
63
64
65
66
67
68
69
# File 'lib/zoomeye.rb', line 61

def resources_info
  res = get(@@home_url + @@resource_sub_url, {})
  body = JSON.parse res.body
  if '200' == res.code
    return body
  else
    raise ZoomEyeError.new(res.code, body["message"])
  end
end

#web_search(query, page = 1, facets = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zoomeye.rb', line 85

def web_search(query, page=1, facets=nil)
  params = {}
  params["query"] = query if query
  params["page"] = page if page
  params["facets"] = facets if facets
  res = get(@@home_url + @@web_sub_url, params)
  body = JSON.parse res.body
  if '200' == res.code
    return body
  else
    raise ZoomEyeError.new(res.code, body["message"])
  end
end