worldtimeengine 0.0.1 → 0.0.2

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.
@@ -19,11 +19,25 @@ module WorldTimeEngine
19
19
  request(:get, path, params, options)
20
20
  end
21
21
 
22
+ # Fetch information from WorldTimeEngine about IP address
23
+ #
24
+ # @param IP address [String]
25
+ # @raise [WorldTimeEngine::Error::Unauthorized] Error raised when supplied user credentials are not valid.
26
+ # @return [Hashi::Mash] IP info
22
27
  def api(ip)
23
28
  endpoint = WorldTimeEngine.instance_variable_get(:@endpoint)
24
29
  api_key = WorldTimeEngine.instance_variable_get(:@api_key)
25
30
 
26
- timezone_hash = HTTParty.get("#{endpoint}/api/ip/#{api_key}/#{ip}", :format => :xml).to_hash['timezone']
31
+ raise ArgumentError.new(":api_key wasn't set in configuration block") if api_key.nil?
32
+
33
+ hash = HTTParty.get("#{endpoint}/api/ip/#{api_key}/#{ip}", :format => :xml).to_hash
34
+
35
+ if hash.keys.include?('error')
36
+ raise "Invalid or Expired API Key used; Error code: 10001"
37
+ end
38
+
39
+ timezone_hash = hash['timezone']
40
+
27
41
  timezone_hash.delete('xmlns:xsi')
28
42
  timezone_hash.delete('xsi:noNamespaceSchemaLocation')
29
43
 
@@ -40,9 +54,11 @@ module WorldTimeEngine
40
54
  mash.time.zone.current.utc_offset = mash.time.zone.current.utcoffset
41
55
  mash.time.zone.current.effective_until = Time.parse "#{mash.time.zone.current.effectiveUntil} #{mash.time.zone.current.abbreviation} #{mash.time.zone.current.utcoffset}"
42
56
 
43
- mash.time.zone.next.is_dst = to_boolean mash.time.zone.next.isdst
44
- mash.time.zone.next.utc_offset = mash.time.zone.next.utcoffset
45
- mash.time.zone.next.effective_until = Time.parse "#{mash.time.zone.next.effectiveUntil} #{mash.time.zone.next.abbreviation} #{mash.time.zone.next.utcoffset}"
57
+ if mash.time.zone.has_dst
58
+ mash.time.zone.next.is_dst = to_boolean mash.time.zone.next.isdst
59
+ mash.time.zone.next.utc_offset = mash.time.zone.next.utcoffset
60
+ mash.time.zone.next.effective_until = Time.parse "#{mash.time.zone.next.effectiveUntil} #{mash.time.zone.next.abbreviation} #{mash.time.zone.next.utcoffset}"
61
+ end
46
62
  end
47
63
  end
48
64
 
@@ -0,0 +1,5 @@
1
+ module WorldTimeEngine
2
+ class Error
3
+ # Raised when Twitter returns the HTTP status code 401
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <error xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://worldtimeengine.com/error.xsd" type="exception">
3
+ <code>10001</code>
4
+ <message>Invalid or Expired API Key used</message>
5
+ </error>
@@ -12,7 +12,7 @@ describe WorldTimeEngine do
12
12
  end
13
13
 
14
14
  stub_request(:get, "http://worldtimeengine.com/api/ip/abc/193.174.32.100").
15
- to_return(:status => 200, :body => fixture('193.174.32.100.xml'), :headers => {})
15
+ to_return(:status => 200, :body => fixture('germany-193.174.32.100.xml'), :headers => {})
16
16
  end
17
17
 
18
18
  it "requests the correct resource" do
@@ -20,6 +20,62 @@ describe WorldTimeEngine do
20
20
 
21
21
  a_get("/api/ip/abc/193.174.32.100").should have_been_made
22
22
  end
23
+ end
24
+
25
+ describe 'request with invalid api_key' do
26
+ before do
27
+ WorldTimeEngine.configure do |config|
28
+ config.api_key = 'abc'
29
+ end
30
+
31
+ stub_request(:get, "http://worldtimeengine.com/api/ip/abc/212.154.168.243").
32
+ to_return(:status => 200, :body => fixture('wrong_key.xml'), :headers => {})
33
+ end
34
+
35
+ it "returns raises an exception" do
36
+ lambda {
37
+ WorldTimeEngine.api('212.154.168.243')
38
+ }.should raise_error(RuntimeError, 'Invalid or Expired API Key used; Error code: 10001')
39
+ end
40
+ end
41
+
42
+ context "region with DST" do
43
+ before do
44
+ WorldTimeEngine.configure do |config|
45
+ config.api_key = 'abc'
46
+ end
47
+
48
+ stub_request(:get, "http://worldtimeengine.com/api/ip/abc/212.154.168.243").
49
+ to_return(:status => 200, :body => fixture('kazakhstan-212.154.168.243.xml'), :headers => {})
50
+ end
51
+
52
+ it "returns needed values" do
53
+ response = WorldTimeEngine.api('212.154.168.243')
54
+
55
+ response.location.region.should == 'Kazakhstan'
56
+ response.location.latitude.should == 51.1811
57
+ response.location.longitude.should == 71.4278
58
+
59
+ response.time.zone.has_dst.should == false
60
+ response.time.zone.current.abbreviation.should == "ALMT"
61
+ end
62
+
63
+ it "skips next attribute" do
64
+ response = WorldTimeEngine.api('212.154.168.243')
65
+
66
+ response.time.zone.next.should be_nil
67
+ end
68
+ end
69
+
70
+ context "region without DST" do
71
+ before do
72
+ WorldTimeEngine.configure do |config|
73
+ config.api_key = 'abc'
74
+ end
75
+
76
+ stub_request(:get, "http://worldtimeengine.com/api/ip/abc/193.174.32.100").
77
+ to_return(:status => 200, :body => fixture('germany-193.174.32.100.xml'), :headers => {})
78
+ end
23
79
 
24
80
  it "returns needed values" do
25
81
  response = WorldTimeEngine.api('193.174.32.100')
@@ -19,6 +19,6 @@ Gem::Specification.new do |gem|
19
19
  gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
20
20
  gem.summary = %q{WorldTimeEngine API wrapper}
21
21
  gem.test_files = Dir.glob("spec/**/*")
22
- gem.version = '0.0.1'
22
+ gem.version = '0.0.2'
23
23
  end
24
24
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worldtimeengine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -120,10 +120,12 @@ files:
120
120
  - worldtimeengine.gemspec
121
121
  - lib/worldtimeengine/client.rb
122
122
  - lib/worldtimeengine/configurable.rb
123
+ - lib/worldtimeengine/error/unauthorized.rb
123
124
  - lib/worldtimeengine.rb
124
- - spec/fixtures/193.174.32.100.xml
125
- - spec/fixtures/another.xml
125
+ - spec/fixtures/germany-193.174.32.100.xml
126
+ - spec/fixtures/kazakhstan-212.154.168.243.xml
126
127
  - spec/fixtures/us.xml
128
+ - spec/fixtures/wrong_key.xml
127
129
  - spec/helper.rb
128
130
  - spec/worldtimeengine_spec.rb
129
131
  homepage: https://github.com/nfedyashev/worldtimeengine
@@ -140,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
142
  version: '0'
141
143
  segments:
142
144
  - 0
143
- hash: -4077128795819690830
145
+ hash: 488971206880791712
144
146
  required_rubygems_version: !ruby/object:Gem::Requirement
145
147
  none: false
146
148
  requirements:
@@ -154,8 +156,9 @@ signing_key:
154
156
  specification_version: 3
155
157
  summary: WorldTimeEngine API wrapper
156
158
  test_files:
157
- - spec/fixtures/193.174.32.100.xml
158
- - spec/fixtures/another.xml
159
+ - spec/fixtures/germany-193.174.32.100.xml
160
+ - spec/fixtures/kazakhstan-212.154.168.243.xml
159
161
  - spec/fixtures/us.xml
162
+ - spec/fixtures/wrong_key.xml
160
163
  - spec/helper.rb
161
164
  - spec/worldtimeengine_spec.rb