ipstack 0.0.1 → 0.1.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
  SHA256:
3
- metadata.gz: 79566f77f9a1814cbfa641c27cc579e9029c317e7c66a6ed9bd765a4657cb1c4
4
- data.tar.gz: 58372672c88fcd8f5aaeb1a016be04e94fa350697684e232fe22b33b2b3c29cb
3
+ metadata.gz: 412b55aa9741be14e2f8f8df76e93dae4856354c917118cb45084903fff0f833
4
+ data.tar.gz: ba32b6b878a746814cc64d8230316d0634984735c03b50320ce21559ef3d39c4
5
5
  SHA512:
6
- metadata.gz: a6d7a438521b642b1b18ebb3f0d0971786db0e66d349539a73c33e423c529669f456d4a9d6d6d9b1b7e6566692c93540cb1b51e6190e533a83f9e5d81dfca573
7
- data.tar.gz: f6a658795fd33d063497752bc8c2c9e6fd8370cfc2b62c023a5b20431b451ac2525a0efc3ac45105e24e6dbfc6ae102d48ea09d94d725cf47fac32e804c9b74c
6
+ metadata.gz: 2f7975f01d53b72a4e2e6ff9901eed1b52aa93902535cca7a435e41ae13df5a00fc905bfdd11a0ec95fc368eba1a6d50b1f99caf176432125dc362fdeeb5f134
7
+ data.tar.gz: 1a5e2c1244283b62a7c67ededce73e7f7b75636f4b912ad689597d21ada181ad3125eb9efbfd87c3c1bde53db71c052b5c3a083557b5426d843fe131852c04eb
data/Gemfile CHANGED
@@ -2,7 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
- gem 'pry'
6
-
7
5
  # Specify your gem's dependencies in ipstack.gemspec
8
6
  gemspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # (Unofficial) Ipstack
1
+ # (Unofficial) Ipstack Gem for API requests
2
2
 
3
3
  ## Installation
4
4
 
@@ -18,17 +18,44 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ * All endpoints listed in https://ipstack.com/documentation can be found under Ipstack::API (.check, .standard, .bulk)
22
+ * Returned data is automatically converted into ruby usable forms
23
+ * **JSON** will be passed through JSON.parse to return a Ruby data structure (a Hash)
24
+ * **XML** through Nokogiri::XML
25
+ * To disable this, you can set the _return_raw_ to true under optionals.
26
+ * Only basic checks are performed to be sure your parameters for the method are there or != nil/empty. You'll want to still rely on errors returned from the api itself. See documentation.
22
27
 
23
- ## Development
28
+ Here are examples:
24
29
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+ ```ruby
31
+
32
+ irb(main):008:0> Ipstack::API.check
33
+ => {"ip"=>"73.152.49.135", "type"=>"ipv4", "continent_code"=>"NA", "continent_name"=>"North America", "country_code"=>"US", "country_name"=>"United States", "region_code"=>"VA", "region_name"=>"Virginia", "city"=>"Ashburn", "zip"=>"20147", "latitude"=>39.018, "longitude"=>-77.539, "location"=>{"geoname_id"=>4744870, "capital"=>"Washington D.C.", "languages"=>[{"code"=>"en", "name"=>"English", "native"=>"English"}], "country_flag"=>"http://assets.ipstack.com/flags/us.svg", "country_flag_emoji"=>"🇺🇸", "country_flagmoji_unicode"=>"U+1F1FA U+1F1F8", "calling_code"=>"1", "is_eu"=>false}}
34
+
35
+ irb(main):007:0> Ipstack::API.standard('134.201.250.155',{fields: 'city,region_name,zip'})
36
+ => {"city"=>"Huntington Beach", "region_name"=>"California", "zip"=>"92648"}
37
+
38
+ irb(main):010:0> Ipstack::API.bulk('134.201.250.155,73.152.49.135',{fields: 'city,region_name,zip'})
39
+ => {"success"=>false, "error"=>{"code"=>303, "type"=>"batch_not_supported_on_plan", "info"=>"Bulk requests are not supported on your plan. Please upgrade your subscription."}}
26
40
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+ # Returning XML
42
+
43
+ irb(main):001:0> Ipstack::API.check({output: 'xml'})
44
+ => #<Nokogiri::XML::Document:0x3fe9c31604f8 name="document" children=[#<Nokogiri::XML::Element:0x3fe9c31601b0 name="result" children=[#<Nokogiri::XML::Element:0x3fe9c315de9c name="ip" children=[#<Nokogiri::XML::Text:0x3fe9c315db68 "73.152.49.135">]>, #<Nokogiri::XML::Element:0x3fe9c315d30c . . .
45
+
46
+ # Returning raw json or xml
47
+
48
+ irb(main):002:0> Ipstack::API.check({return_raw: true})
49
+ => "{\"ip\":\"73.152.49.135\",\"type\":\"ipv4\",\"continent_code\":\"NA\",\"continent_name\":\"North America\",\"country_code\":\"US\",\"country_name\":\"United States\",\"region_code\":\"VA\",\"region_name\":\"Virginia\",\"city\":\"Ashburn\",\"zip\":\"20147\",\"latitude\":39.018,\"longitude\":-77.539,\"location\":{\"geoname_id\":4744870,\"capital\":\"Washington D.C.\",\"languages\":[{\"code\":\"en\",\"name\":\"English\",\"native\":\"English\"}],\"country_flag\":\"http:\\/\\/assets.ipstack.com\\/flags\\/us.svg\",\"country_flag_emoji\":\"\\ud83c\\uddfa\\ud83c\\uddf8\",\"country_flag_emoji_unicode\":\"U+1F1FA U+1F1F8\",\"calling_code\":\"1\",\"is_eu\":false}}"
50
+
51
+ irb(main):003:0> Ipstack::API.standard('73.152.49.135',{output: 'xml', return_raw: true})
52
+ => "<?xml version=\"1.0\"?>\n<result><ip>73.152.49.135</ip><type>ipv4</type><continent_code>NA</continent_code><continent_name>North America</continent_name><country_code>US</country_code><country_name>United States</country_name><region_code>VA</region_code><region_name>Virginia</region_name><city>Ashburn</city><zip>20147</zip><latitude>39.018</latitude><longitude>-77.539</longitude><location><geoname_id>4744870</geoname_id><capital>Washington D.C.</capital><languages><code>en</code><name>English</name><native>English</native></languages><country_flag>http://assets.ipstack.com/flags/us.svg</country_flag><country_flag_emoji>&#x1F1FA;&#x1F1F8;</country_flag_emoji><country_flag_emoji_unicode>U+1F1FA U+1F1F8</country_flag_emoji_unicode><calling_code>1</calling_code><is_eu></is_eu></location></result>\n"
53
+
54
+ ```
28
55
 
29
56
  ## Contributing
30
57
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ipstack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
32
59
 
33
60
  ## License
34
61
 
@@ -36,4 +63,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
63
 
37
64
  ## Code of Conduct
38
65
 
39
- Everyone interacting in the Ipstack project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ipstack/blob/master/CODE_OF_CONDUCT.md).
66
+ Everyone interacting in the Ipstack project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/NorseGaud/ipstack/blob/master/CODE_OF_CONDUCT.md).
data/lib/ipstack/api.rb CHANGED
@@ -1,16 +1,20 @@
1
1
  require 'net/http'
2
2
  require 'openssl'
3
3
  require 'json'
4
+ require 'nokogiri'
4
5
 
5
6
  module Ipstack
6
7
  class API
7
8
  # Make sure we can access parameters of the object
8
- attr_accessor :api_url, :content_type, :access_key, :optionals, :params_uri
9
+ attr_accessor :api_url, :content_type, :access_key, :optionals, :params_uri, :return_raw
9
10
  def initialize(optionals = {}, access_key = ENV['IPSTACK_ACCESS_KEY'])
10
11
 
11
12
  raise ArgumentError, 'Requires a hash of optional values found on https://ipstack.com/documentation' unless optionals.is_a?(Hash)
12
13
  raise ArgumentError, '\'access_key\' (api key) cannot be nil. Obtain your key from https://ipstack.com/quickstart and set it as ENV[\'IPSTACK_ACCESS_KEY\']' if access_key.nil? || access_key == ''
13
14
 
15
+ @return_raw = optionals[:return_raw]
16
+ optionals.delete(:return_raw) # remove it from optionals since we each optionals for our url params
17
+
14
18
  @api_url = 'http://api.ipstack.com/'
15
19
  @access_key = access_key
16
20
  @params_uri = "?access_key=#{access_key}"
@@ -24,31 +28,35 @@ module Ipstack
24
28
  # TODO: HTTP/HTTPS support
25
29
  #http.use_ssl = false
26
30
  # https.verify_mode = OpenSSL::SSL::VERIFY_NONE # SSL not signed? Don't care!
27
- request = Net::HTTP::Get.new(query_ips.nil? || query_ips == '' ? "/check#{object.params_uri}" : "/#{query_ips}#{object.params_uri}")
31
+ request = Net::HTTP::Get.new(query_ips.nil? || query_ips.empty? ? "/check#{object.params_uri}" : "/#{query_ips}#{object.params_uri}")
28
32
  response = http.request(request)
29
33
  case response
30
34
  when Net::HTTPSuccess
31
- object.optionals[:output] == 'xml' ? Nokogiri::XML(response.body) : JSON.parse(response.body)
35
+ if object.return_raw
36
+ response.body
37
+ else
38
+ object.optionals[:output] == 'xml' ? Nokogiri::XML(response.body) : JSON.parse(response.body)
39
+ end
32
40
  else
33
41
  return response
34
42
  #"HTTP Error: #{response.code} #{response.message} : #{response.body}"
35
43
  end
36
44
  end
37
45
 
38
- # https://ipstack.com/documentation
39
46
  def self.standard(ip, optionals = {})
40
47
  api_request_object = Ipstack::API.new(optionals)
41
- raise('Requires a single IP') if ip.include?(',')
48
+ raise ArgumentError, 'Requires a single IP as first parameter' if ip.nil? || ip.empty? || ip.is_a?(Hash)
42
49
  Ipstack::API.make_request(api_request_object,ip)
43
50
  end
44
51
 
45
52
  def self.bulk(ips, optionals = {})
46
53
  api_request_object = Ipstack::API.new(optionals)
47
- raise('Requires a list of IPs comma separated') unless ips.include?(',') && ips.match(/((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)(,\n|,?$))/)
54
+ raise ArgumentError, 'Requires a list of IPs comma separated as first parameter' if ips.nil? || ips.empty? || ip.is_a?(Hash) || !( ips.include?(',') && ips.match(/((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)(,\n|,?$))/) )
48
55
  Ipstack::API.make_request(api_request_object,ips)
49
56
  end
50
57
 
51
58
  def self.check(optionals = {})
59
+ raise ArgumentError,'Only optionals hash allowed as first parameter' unless optionals.is_a?(Hash) || optionals.empty?
52
60
  api_request_object = Ipstack::API.new(optionals)
53
61
  Ipstack::API.make_request(api_request_object)
54
62
  end
@@ -1,3 +1,3 @@
1
1
  module Ipstack
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipstack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Pierce
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-08 00:00:00.000000000 Z
11
+ date: 2018-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler