Module: IpInfo::API::Request

Included in:
IpInfo::API
Defined in:
lib/ip_info/request.rb

Constant Summary collapse

REQUEST_TYPES =
%w{city country}

Instance Method Summary collapse

Instance Method Details

#query(data, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ip_info/request.rb', line 6

def query(data, options = {})
  ip = data.sub(/^https?\:\/\//, '').sub(/^www./,'')  

  type = options.fetch(:type, 'country')

  unless REQUEST_TYPES.include?(type)
    raise ArgumentError.new("Wrong request type (available: #{REQUEST_TYPES.join(",")})")
  end

  time_zone = options.fetch(:time_zone, false)

  params = {
    key:      api_key,
    ip:       ip,
    timezone: time_zone,
    format:   "json"
  }

  response = self.class.get("#{type}/", query: params)
  parse_response(response.parsed_response)
end