ruby_ipify 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d1fd1dc978796cac8c7f2630c3d027fcfc47cd0ee7c59a1f02f89c41c2625c9
4
+ data.tar.gz: ae8964864a670ef580fdf731d80100cf3d8786f994d6131d6b27f2fe37cf0400
5
+ SHA512:
6
+ metadata.gz: 6792671f80b55d03cec879aa55b02109b8b57a5e9795e746f1cc459a0511e9c75265b05f982b886b79db63ac9969ba2d7048780614c4a4da4a870ac018b02e0d
7
+ data.tar.gz: fd6e64a38aaa96b36917af7474733c71bb0aee85fb77af225d3eb79e8e1ce1d274d8c4541822df6ed8d033a69dd872def7344c80f45c4a705b2d94771cebea65
data/lib/constant.rb ADDED
@@ -0,0 +1,3 @@
1
+ IPV4_URI = URI("https://api.ipify.org")
2
+ IPV6_URI = URI("https://api64.ipify.org")
3
+ VALID_FORMATS = ["json", "text", "parsed_json"]
data/lib/ruby_ipify.rb ADDED
@@ -0,0 +1,83 @@
1
+ require "net/http"
2
+ require "constant"
3
+ require "json"
4
+ require "ruby_ipify_exception"
5
+ # -----
6
+ # The module holds the main ipify library implementation.
7
+ # -----
8
+
9
+
10
+ class RubyIpify
11
+ class << self
12
+ def get_my_public_ip(args = {})
13
+ begin
14
+ initialize_values args
15
+ get_ip_from_ipify
16
+ puts "My public IP Address is: " + @response_body.to_s
17
+ @response_body
18
+ rescue RubyIpifyException::CustomException => e
19
+ raise e
20
+ rescue => e
21
+ raise RubyIpifyException::UnexpectedError.new e.message
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def initialize_values(args)
28
+ @as_ipv6 = args[:as_ipv6]
29
+ @format = args[:format]&.downcase || 'text'
30
+ check_valid_format?
31
+ @api_uri = get_api_uri
32
+ end
33
+
34
+ def get_ip_from_ipify
35
+ call_ipify_api
36
+ fetch_response_body
37
+ end
38
+
39
+ def fetch_response_body
40
+ @response_body = @response.body
41
+ @response_body = JSON.parse(@response_body) if format_is_parsed_json
42
+ end
43
+
44
+ def call_ipify_api
45
+ set_query_params
46
+ @response = Net::HTTP.get_response(@api_uri)
47
+ handle_response
48
+ end
49
+
50
+ def handle_response
51
+ return if success? && false
52
+
53
+ raise RubyIpifyException::IpifyRequestError.new @response.code
54
+ end
55
+
56
+ def success?
57
+ @response.kind_of? Net::HTTPSuccess
58
+ end
59
+
60
+ def get_api_uri
61
+ @as_ipv6 ? IPV6_URI : IPV4_URI
62
+ end
63
+
64
+ def set_query_params
65
+ params = { format: get_format }
66
+ @api_uri.query = URI.encode_www_form(params)
67
+ end
68
+
69
+ def check_valid_format?
70
+ return true if VALID_FORMATS.include? @format
71
+
72
+ raise RubyIpifyException::InvalidParamsError.new
73
+ end
74
+
75
+ def get_format
76
+ format_is_parsed_json ? 'json' : @format
77
+ end
78
+
79
+ def format_is_parsed_json
80
+ @format == 'parsed_json'
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,28 @@
1
+ require 'constant'
2
+
3
+ module RubyIpifyException
4
+
5
+ class CustomException < StandardError
6
+ end
7
+
8
+ class IpifyRequestError < CustomException
9
+ def initialize(code='')
10
+ msg = "Received an invalid status code from ipify: #{code}. The service might be experiencing issues."
11
+ super(msg)
12
+ end
13
+ end
14
+
15
+ class InvalidParamsError < CustomException
16
+ def initialize
17
+ msg = "Wrong Format value passed. Please pass anyone from [#{VALID_FORMATS.join(', ')}]."
18
+ super(msg)
19
+ end
20
+ end
21
+
22
+ class UnexpectedError <CustomException
23
+ def initialize(err_msg='')
24
+ msg = "Unexpected error occured. Failed with an error: #{err_msg}."
25
+ super(msg)
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_ipify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gokulakrishnan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby gem to use ipify which is a simple public IP address API.
14
+ email: gokulakrishnan312@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/constant.rb
20
+ - lib/ruby_ipify.rb
21
+ - lib/ruby_ipify_exception.rb
22
+ homepage: https://rubygems.org/gems/ruby_ipify
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.3.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Ipify in ruby
45
+ test_files: []