ip_info 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b26fd0a616f52de15a94cd82937c078808a022e6
4
- data.tar.gz: 8696df49bcce4071de7570d982b22e5d40dbcedc
3
+ metadata.gz: 5f526a57b67936ef68324a637a79510a5b908d8e
4
+ data.tar.gz: 97047dbf56a678255d482f53bb3488a06358f9d1
5
5
  SHA512:
6
- metadata.gz: 7eb5c34f631fa38021f3358f74fc7fd71696a2568546dbb000525a1058fd4555a4336bc367dedc825bdf4f9dfe6e4198cf9f79794ee7c6dec01985367c0c46dd
7
- data.tar.gz: 2c4fd318d77c59eb7b7db7ca068268736f9cafc8d0081347b84d8805ba68faf51eb7cdf017c5a4f2fc6ef504a3ff0f0167af576b8473afec696a644cd62cc190
6
+ metadata.gz: 8a9d9218022eef92084e724318c5780085955ff87bc4c34a0447bca8cb54c1ac38314e0447fe8dc04dcee71dc690b25f22e8d96e1b52894231bcf246b11c318e
7
+ data.tar.gz: 849569ac1a4b0bd33786b72ae70f84e50ec23252027e770335b8aff6d30848d9a3cfdbe2a63147b4384060544546e9ac9c2313d1b3114621c71e5178e1cb1211
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # IpInfo Gem
2
2
 
3
- [![Build Status](https://travis-ci.org/max-si-m/ipinfo.svg?branch=master)](https://travis-ci.org/max-si-m/ipinfo)
3
+ [![Build Status](https://travis-ci.org/max-si-m/ip_info.svg?branch=master)](https://travis-ci.org/max-si-m/ip_info)
4
4
 
5
5
  Find location from an IP address.
6
6
  This's simple gem for [http://ipinfodb.com/](http://ipinfodb.com/)
@@ -23,9 +23,8 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
  Use your api key for site, you can get it [here](http://ipinfodb.com/account.php)
26
- Api key also can be stored in environment variable `ENV['IP_INFO_KEY']`.
27
26
  ```ruby
28
- ip_info = IpInfo::API.new()
27
+ ip_info = IpInfo::API.new(api_key: "<YOUR_API_KEY>")
29
28
  ```
30
29
  And use `#lookup` for geting information about *ip*:
31
30
 
@@ -12,22 +12,23 @@ module IpInfo
12
12
  format :json
13
13
 
14
14
  attr_accessor :api_key
15
-
15
+
16
16
  # Using for adding adding api_key for requests
17
17
  # API_KEY you can get from: http://ipinfodb.com/account.php
18
18
  #
19
- # Api key also can be stored in environment variable ENV['IP_INFO_KEY'].
19
+ # Constructor yields a block to allow more complicated object configuration.
20
20
  #
21
- # It takes one arguments:
21
+ # It takes options with single optional key:
22
22
  # * +api_key+: string of api_key
23
23
  #
24
24
  # ==== Example:
25
25
  # ip_info = IpInfo::API.new()
26
- def initialize()
27
- self.api_key ||= ENV['IP_INFO_KEY']
28
- raise ApiKeyError.new("Error! Add your API key") if api_key.nil?
26
+ def initialize(options = {})
27
+ yield self if block_given?
28
+
29
+ self.api_key ||= options.fetch(:api_key) { fail ApiKeyError.new("API key is missing.") }
29
30
  end
30
-
31
+
31
32
  # Retreive the remote location of a given ip address.
32
33
  #
33
34
  # It takes two optional arguments:
@@ -5,17 +5,25 @@ module IpInfo
5
5
  module Parser
6
6
  class InvalidParamsError < ArgumentError; end
7
7
 
8
- def parse_response(response)
9
- result = convert_keys response
10
- raise InvalidParamsError.new(result[:status_message]) if result[:status_code] == "ERROR"
11
- result
8
+ def parse_response(response)
9
+ convert_keys(response).tap do |result|
10
+ raise InvalidParamsError.new(result[:status_message]) if result[:status_code] == "ERROR"
11
+ end
12
12
  end
13
13
 
14
14
  private
15
- def convert_keys(response)
16
- raise InvalidParamsError.new("Params must be presence") if response.nil?
17
- response.inject({}) {|hsh, (key,value)| hsh[key.to_s.gsub(/[A-Z]/){|s| '_' + s.downcase}.gsub(/^_/, '').to_sym] = value; hsh}
15
+
16
+ def convert_keys(response)
17
+ raise InvalidParamsError.new("Params must be present") if response.nil?
18
+
19
+ response.each_with_object({}) do |(key, value), hsh|
20
+ hsh[to_snake_case(key)] = value
18
21
  end
22
+ end
23
+
24
+ def to_snake_case(key)
25
+ key.to_s.gsub(/[A-Z]/) { |s| '_' + s.downcase }.gsub(/^_/, '').to_sym
26
+ end
19
27
  end
20
28
  end
21
29
  end
@@ -1,17 +1,25 @@
1
1
  module IpInfo
2
2
  class API
3
3
  module Request
4
- def query data, options = {}
4
+ REQUEST_TYPES = %w{city country}
5
+
6
+ def query(data, options = {})
5
7
  ip = data.sub(/^https?\:\/\//, '').sub(/^www./,'')
6
8
 
7
- type = (options[:type] == "city") ? "city" : "country"
8
- time_zone = (options[:time_zone] == true ) ? true : false
9
+ type = options.fetch(:type, 'country')
10
+
11
+ unless REQUEST_TYPES.include?(type)
12
+ raise ArgumentError.new("Wrong request type (available: #{REQUEST_TYPES.join(",")})")
13
+ end
14
+
15
+ time_zone = options.fetch(:time_zone, false)
9
16
 
10
- params = {}
11
- params[:key] = self.api_key
12
- params[:ip] = ip
13
- params[:timezone] = time_zone
14
- params[:format] = "json"
17
+ params = {
18
+ key: api_key,
19
+ ip: ip,
20
+ timezone: time_zone,
21
+ format: "json"
22
+ }
15
23
 
16
24
  response = self.class.get("#{type}/", query: params)
17
25
  parse_response(response.parsed_response)
@@ -1,3 +1,3 @@
1
1
  module IpInfo
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -2,17 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe IpInfo::API do
4
4
  let(:ip) { 'devbattles.com' }
5
- let(:ip_info) { IpInfo::API.new() }
5
+ let(:ip_info) { IpInfo::API.new(api_key: ENV["IP_INFO_KEY"]) }
6
6
 
7
7
  it "set api_key", :vcr do
8
8
  expect(ip_info.api_key).to eq(ENV["IP_INFO_KEY"])
9
9
  end
10
10
 
11
- it "raise errors with empty api_key", :vcr do
12
- expect{IpInfo::API.new(nil)}.to raise_error(ArgumentError)
11
+ it "raise errors with empty api_key", :vcr do
12
+ expect{IpInfo::API.new()}.to raise_error(ArgumentError)
13
13
  end
14
14
 
15
- it "check type of request", :vcr do
15
+ it "check type of request", :vcr do
16
16
  expect(ip_info.lookup(ip)).to be_kind_of(Hash)
17
17
  end
18
18
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  #using IpInfo::API instanse for access to IpInfo::Parser
4
4
  describe IpInfo::API::Parser do
5
- let(:ip_info) { IpInfo::API.new() }
5
+ let(:ip_info) { IpInfo::API.new(api_key: ENV['IP_INFO_KEY']) }
6
6
 
7
7
  let(:response) do
8
8
  {
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
  describe IpInfo::API::Request do
5
5
  let(:address_url) { 'devbattles.com' }
6
6
  let(:address_ip) { '64.233.191.255' }
7
- let(:ip_info) { IpInfo::API.new() }
7
+ let(:ip_info) { IpInfo::API.new(api_key: ENV['IP_INFO_KEY']) }
8
8
 
9
9
  context "check #query params" do
10
10
  it "must be one param" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Djuliy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-19 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty