Class: CIF::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cif/client.rb,
lib/cif/client/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, apikey, severity = nil, restriction = nil, nolog = false) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
# File 'lib/cif/client.rb', line 14

def initialize(host,apikey,severity=nil,restriction=nil,nolog=false)
	@host = host
	@apikey = apikey
	@severity = severity
	@nolog = nolog
	@restriction = restriction
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



12
13
14
# File 'lib/cif/client.rb', line 12

def fields
  @fields
end

#restriction=(value) ⇒ Object (writeonly)

Sets the attribute restriction

Parameters:

  • value

    the value to set the attribute restriction to.



13
14
15
# File 'lib/cif/client.rb', line 13

def restriction=(value)
  @restriction = value
end

#severity=(value) ⇒ Object (writeonly)

Sets the attribute severity

Parameters:

  • value

    the value to set the attribute severity to.



13
14
15
# File 'lib/cif/client.rb', line 13

def severity=(value)
  @severity = value
end

Instance Method Details

#query(q, severity = nil, restriction = nil, nolog = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cif/client.rb', line 22

def query(q,severity=nil,restriction=nil,nolog=false)
	params = {'apikey' => @apikey}
	params['restriction'] = restriction || @restriction if restriction || @restriction
	params['severity'] = severity || @severity if severity || @severity
	params['nolog'] = 1 if nolog || @nolog
	s = "#{@host}/#{q}?"+params.map{|k,v| "#{k}=#{v}"}.join("&")
	url = URI.parse s
	http = Net::HTTP.new(url.host, url.port)
	http.use_ssl = (url.scheme == 'https')
	http.verify_mode = OpenSSL::SSL::VERIFY_NONE
	http.verify_depth = 5
	request = Net::HTTP::Get.new(url.path+"?"+url.query)
	request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} cif-client v#{CIF::Client::VERSION}")
	response = http.request(request)
	doc = response.body
	data = JSON.parse(doc)
	@response_code = data['status']
	if data['data'] and data['data']
		feed = data['data']['feed']
	end
	feed
end