Class: MaxMind::Enquiry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Enquiry

Returns a new instance of Enquiry.

[View source]

6
7
8
# File 'lib/max_mind/enquiry.rb', line 6

def initialize(attributes = {})
  self.attributes = attributes if attributes.is_a?(Hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value = nil) ⇒ Object

 Set or get an attribute from the attributes hash or raise no method error if it doesn’t exist in our mapping.

[View source]

54
55
56
57
58
# File 'lib/max_mind/enquiry.rb', line 54

def method_missing(name, value = nil)
  attribute_name = name.to_s.gsub(/\=\z/, '').to_sym
  return super unless mapping.keys.include?(attribute_name)
  value ? (attributes[attribute_name] = value) : attributes[attribute_name]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.


4
5
6
# File 'lib/max_mind/enquiry.rb', line 4

def attributes
  @attributes
end

#emailObject

Various methods to make setting fields in the attributes hash  a little easier.


62
63
64
# File 'lib/max_mind/enquiry.rb', line 62

def email
  @email
end

#passwordObject

Various methods to make setting fields in the attributes hash  a little easier.


62
63
64
# File 'lib/max_mind/enquiry.rb', line 62

def password
  @password
end

#usernameObject

Various methods to make setting fields in the attributes hash  a little easier.


62
63
64
# File 'lib/max_mind/enquiry.rb', line 62

def username
  @username
end

Instance Method Details

#lookupObject

 Run the check and return a hash of responses from the server.

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/max_mind/enquiry.rb', line 21

def lookup
  MaxMind.logger.debug "Beginning lookups"
  MaxMind.logger.debug "('#{sendable_attributes.inspect}')"
  for endpoint in MaxMind.endpoints
    MaxMind.logger.debug "Attempting lookup on #{endpoint}"
    uri = URI.parse(endpoint)
    req = Net::HTTP::Post.new(uri.path)
    req.set_form_data(sendable_attributes)
    res = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == 'https'
      res.use_ssl = true
      res.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    
    begin
      case res = res.request(req)
      when Net::HTTPSuccess
        return Lookup.new(res.body)
      else
        MaxMind.logger.debug "Error on #{endpoint} (#{res.class.to_s})"
        next
      end
    rescue Timeout::Error
      MaxMind.logger.debug "Timed out connecting to #{endpoint}"
      next
    end
  end
  
  false
end

#sendable_attributesObject

 The attributes here will be sent to max mind

[View source]

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

def sendable_attributes
  hash = Hash.new
  for field, data in attributes
    hash[mapping[field]] = data
  end
  hash['license_key'] = MaxMind.licence_key
  hash
end