Class: Maxmind::Response

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

Constant Summary collapse

ATTRIBUTE_MAP =
{
  'custPhoneInBillingLoc' => 'phone_in_billing_location',
  'maxmindID' => 'maxmind_id',
  'isTransProxy' => 'is_transparent_proxy',
  'err' => 'error',
  'carderEmail' => 'high_risk_email'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil, http_code = nil) ⇒ Response

Returns a new instance of Response.

Raises:

  • (ArgumentError)


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

def initialize(response = nil, http_code = nil)
  raise ArgumentError, 'Missing response string' unless response
  @body = response
  @http_code = http_code.to_i if http_code
  @attributes = {}
  parse(response)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/maxmind/response.rb', line 40

def method_missing(meth, *args)
  if meth.to_s[-1, 1] == '?'
    send(meth.to_s[0..-2])
  elsif attributes.has_key?(meth)
    attributes[meth]
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/maxmind/response.rb', line 3

def attributes
  @attributes
end

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/maxmind/response.rb', line 4

def body
  @body
end

#http_codeObject (readonly)

Returns the value of attribute http_code.



4
5
6
# File 'lib/maxmind/response.rb', line 4

def http_code
  @http_code
end

Instance Method Details

#attribute_namesObject

Returns an array of names for the attributes available on this object sorted alphabetically.



36
37
38
# File 'lib/maxmind/response.rb', line 36

def attribute_names
  attributes.keys.sort
end

#parse(response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/maxmind/response.rb', line 22

def parse(response)
  response.split(';').each do |parameter|
    k, v = parameter.split('=')

    if ATTRIBUTE_MAP.has_key?(k)
      set_attribute(ATTRIBUTE_MAP[k], v)
    else
      set_attribute(k.gsub(/([A-Z])/, '_\1').downcase, v)
    end
  end
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/maxmind/response.rb', line 50

def respond_to?(meth)
  if meth.to_s[-1, 1] == '?'
    respond_to? meth[0..-2]
  else
    super
  end 
end