Class: IpGeoLookup::Result
- Inherits:
-
Object
- Object
- IpGeoLookup::Result
- Includes:
- Comparable
- Defined in:
- lib/ip_geo_lookup/result.rb
Constant Summary collapse
- FIELDS =
[ :country_code, :country_name, :region, :city, :continent_code, :continent_name, :latitude, :longitude, :time_zone, :postal_code ].freeze
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#continent_code ⇒ Object
readonly
Returns the value of attribute continent_code.
-
#continent_name ⇒ Object
readonly
Returns the value of attribute continent_name.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#country_name ⇒ Object
readonly
Returns the value of attribute country_name.
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#postal_code ⇒ Object
readonly
Returns the value of attribute postal_code.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#time_zone ⇒ Object
readonly
Returns the value of attribute time_zone.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
(also: #eql?)
Equality based on to_h; raw is intentionally excluded.
- #[](key) ⇒ Object
- #hash ⇒ Object
-
#initialize(country_code:, country_name:, region:, city:, continent_code: nil, continent_name: nil, latitude: nil, longitude: nil, time_zone: nil, postal_code: nil, raw: nil) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
- #members ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(country_code:, country_name:, region:, city:, continent_code: nil, continent_name: nil, latitude: nil, longitude: nil, time_zone: nil, postal_code: nil, raw: nil) ⇒ Result
Returns a new instance of Result.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ip_geo_lookup/result.rb', line 18 def initialize(country_code:, country_name:, region:, city:, continent_code: nil, continent_name: nil, latitude: nil, longitude: nil, time_zone: nil, postal_code: nil, raw: nil) @country_code = country_code @country_name = country_name @region = region @city = city @continent_code = continent_code @continent_name = continent_name @latitude = latitude @longitude = longitude @time_zone = time_zone @postal_code = postal_code @raw = raw end |
Instance Attribute Details
#city ⇒ Object (readonly)
Returns the value of attribute city.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def city @city end |
#continent_code ⇒ Object (readonly)
Returns the value of attribute continent_code.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def continent_code @continent_code end |
#continent_name ⇒ Object (readonly)
Returns the value of attribute continent_name.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def continent_name @continent_name end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def country_code @country_code end |
#country_name ⇒ Object (readonly)
Returns the value of attribute country_name.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def country_name @country_name end |
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def longitude @longitude end |
#postal_code ⇒ Object (readonly)
Returns the value of attribute postal_code.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def postal_code @postal_code end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def raw @raw end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def region @region end |
#time_zone ⇒ Object (readonly)
Returns the value of attribute time_zone.
13 14 15 |
# File 'lib/ip_geo_lookup/result.rb', line 13 def time_zone @time_zone end |
Instance Method Details
#<=>(other) ⇒ Object
90 91 92 93 |
# File 'lib/ip_geo_lookup/result.rb', line 90 def <=>(other) return nil unless other.is_a?(Result) [country_code.to_s, region.to_s, city.to_s] <=> [other.country_code.to_s, other.region.to_s, other.city.to_s] end |
#==(other) ⇒ Object Also known as: eql?
Equality based on to_h; raw is intentionally excluded.
96 97 98 99 |
# File 'lib/ip_geo_lookup/result.rb', line 96 def ==(other) return false unless other.is_a?(Result) to_h == other.to_h end |
#[](key) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ip_geo_lookup/result.rb', line 36 def [](key) case key.to_sym when :country_code then @country_code when :country_name then @country_name when :region then @region when :city then @city when :continent_code then @continent_code when :continent_name then @continent_name when :latitude then @latitude when :longitude then @longitude when :time_zone then @time_zone when :postal_code then @postal_code when :raw then @raw end end |
#hash ⇒ Object
103 104 105 |
# File 'lib/ip_geo_lookup/result.rb', line 103 def hash to_h.hash end |
#inspect ⇒ Object
86 87 88 |
# File 'lib/ip_geo_lookup/result.rb', line 86 def inspect "#<IpGeoLookup::Result #{self}>" end |
#members ⇒ Object
73 74 75 |
# File 'lib/ip_geo_lookup/result.rb', line 73 def members FIELDS.dup end |
#to_a ⇒ Object
67 68 69 70 71 |
# File 'lib/ip_geo_lookup/result.rb', line 67 def to_a [@country_code, @country_name, @region, @city, @continent_code, @continent_name, @latitude, @longitude, @time_zone, @postal_code] end |
#to_h ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ip_geo_lookup/result.rb', line 52 def to_h { country_code: @country_code, country_name: @country_name, region: @region, city: @city, continent_code: @continent_code, continent_name: @continent_name, latitude: @latitude, longitude: @longitude, time_zone: @time_zone, postal_code: @postal_code } end |
#to_s ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/ip_geo_lookup/result.rb', line 77 def to_s location = [@city, @region, @country_name].reject { |s| s.nil? || s.empty? }.join(", ") if @country_code && !@country_code.empty? location.empty? ? @country_code : "#{location} (#{@country_code})" else location end end |