Class: MaxMind::Service

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

Direct Known Subclasses

CityIspOrgService, CityService, CountryService

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/max_mind/service.rb', line 10

def response
  @response
end

Class Method Details

.fetch_for_ip(ip) ⇒ Object

Raises:



12
13
14
15
16
17
# File 'lib/max_mind/service.rb', line 12

def self.fetch_for_ip(ip)
  raise RequestError.new("Cannot make a request without an IP address") if ip.nil?
  service = self.new
  service.make_request(ip)
  service.parsed_response
end

Instance Method Details

#build_path(params = {}) ⇒ Object

Raises:



46
47
48
49
# File 'lib/max_mind/service.rb', line 46

def build_path(params = {})
  raise RequestError.new("Cannot build a valid request path!") unless !base_path.nil? && params.is_a?(Hash)
  base_path + '?' + params.to_query_string
end

#make_request(ip) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/max_mind/service.rb', line 25

def make_request(ip)
  if license_key.nil?
    raise LicenseError.new("License Key is missing")
  end

  uri = URI.parse(base_url)

  begin
    self.response = Net::HTTP.get(uri.host, build_path(:l => license_key, :i => ip), uri.port)
  rescue EOFError => e
    raise ConnectionError, "The remote server dropped the connection"
  rescue Errno::ECONNRESET => e
    raise ConnectionError, "The remote server reset the connection"
  rescue Errno::ECONNREFUSED => e
    raise ConnectionError, "The remote server refused the connection"
  rescue Timeout::Error, Errno::ETIMEDOUT => e
    raise ConnectionError, "The connection to the remote server timed out"
  end

end

#parsed_responseObject

This method should be implemented in subclasses Here, we’re only returning the raw data returned by the server



21
22
23
# File 'lib/max_mind/service.rb', line 21

def parsed_response
  self.response
end

#valid_response?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/max_mind/service.rb', line 51

def valid_response?
  !self.response.blank?
end