Class: IpInfo::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Parser, Request
Defined in:
lib/ip_info/api.rb,
lib/ip_info/parser.rb,
lib/ip_info/request.rb

Defined Under Namespace

Modules: Parser, Request Classes: ApiKeyError

Constant Summary

Constants included from Request

Request::REQUEST_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_response

Methods included from Request

#query

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ API

Using for adding adding api_key for requests API_KEY you can get from: ipinfodb.com/account.php

Constructor yields a block to allow more complicated object configuration.

It takes options with single optional key:

  • api_key: string of api_key

Example:

ip_info = IpInfo::API.new()

Yields:

  • (_self)

Yield Parameters:

  • _self (IpInfo::API)

    the object that the method was called on



26
27
28
29
30
# File 'lib/ip_info/api.rb', line 26

def initialize(options = {})
  yield self if block_given?

  self.api_key ||= options.fetch(:api_key) { fail ApiKeyError.new("API key is missing.") }
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/ip_info/api.rb', line 14

def api_key
  @api_key
end

Instance Method Details

#lookup(ip, options = {}) ⇒ Object

Retreive the remote location of a given ip address.

It takes two optional arguments:

  • type: can either be :country (default) or :city

  • time_zone: can either be false (default) or true

Example:

ip_info.lookup('209.85.227.104', type: 'city', time_zone: true)


40
41
42
# File 'lib/ip_info/api.rb', line 40

def lookup(ip, options = {})
  query(ip, options)
end