Class: PwnalyticsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pwnalytics_client/site.rb,
lib/pwnalytics_client/event.rb,
lib/pwnalytics_client/client.rb

Overview

Information about connecting to a Pwnalytics server.

Defined Under Namespace

Classes: Event, Site

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PwnalyticsClient

New client connection to a Pwnalytics server.

The options argument supports the following keys:

:host:: the server hostname (e.g. "ticks.pwnb.us")
:port:: the server port (defaults to 80 for http, 443 for https)
:user:: the account username (e.g. "config")
:password:: the account passowrd (e.g. "vars")
:ssl:: if true, https will be used; make sure to populate the relevant
       net_http options (at the very least, you need ca_file or ca_path)
:net_http:: properties to be set on the Net::HTTP connection object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pwnalytics_client/client.rb', line 19

def initialize(options)
  unless @host = options[:host]
    raise InvalidArgumentError, 'Missing server hostname'
  end
  unless @user = options[:user]
    raise InvalidArgumentError, 'Missing account username'
  end
  unless @password = options[:password]
    raise InvalidArgumentError, 'Missing account password'
  end
  @ssl = options[:ssl] ? true : false
  @port = options[:port] || (@ssl ? 443 : 80)
  @net_http = options[:net_http] || {}
end

Instance Attribute Details

#hostObject (readonly)

Pwnalytics server hostname.



35
36
37
# File 'lib/pwnalytics_client/client.rb', line 35

def host
  @host
end

#portObject (readonly)

Pwnalytics server port.



38
39
40
# File 'lib/pwnalytics_client/client.rb', line 38

def port
  @port
end

#sslObject (readonly)

True if the connection to the Pwnalytics server is secured with SSL.



41
42
43
# File 'lib/pwnalytics_client/client.rb', line 41

def ssl
  @ssl
end

Instance Method Details

#request(request) ⇒ Object

Issues a low-level request to the Pwnalytics server, parses the JSON.

Args:

request:: the low-level request, e.g. '/web_properties.json'

Returns the parsed JSON, as a Hash or Array.

Raises IOError if the server’s response indicates HTTP error.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pwnalytics_client/client.rb', line 51

def request(request)
  http_request = Net::HTTP::Get.new request
  http_request.initialize_http_header 'User-Agent' => 'Pwnalytics Client Gem'
  http_request.basic_auth @user, @password

  http = Net::HTTP.new @host, @port
  if @ssl
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end
  @net_http.each { |key, value| http.send :"#{key}=", value }
  response = http.request http_request
  unless response.kind_of? Net::HTTPSuccess
    raise IOError, "Unhappy HTTP response: #{response}\n"
  end
  
  JSON.parse response.body
end

#site(uid) ⇒ Object

Returns the Site with the given UID.

This method does a server request, to get additional property data.



80
81
82
83
# File 'lib/pwnalytics_client/client.rb', line 80

def site(uid)
  site_data = request("/web_properties/#{uid}.json")
  Site.new self, site_data['uid'], site_data['name']
end

#sitesObject

Returns an array of all Sites on this server.



71
72
73
74
75
# File 'lib/pwnalytics_client/client.rb', line 71

def sites
  request('/web_properties.json').map do |site_data|
    Site.new self, site_data['uid'], site_data['name']
  end
end