emergency_number 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65a17bdc8be7f94d0b8acf4c33227cd54ec415b8
4
+ data.tar.gz: 87296a6d0b4e62048ca886e323d150ad8de1193d
5
+ SHA512:
6
+ metadata.gz: 33341051b2c0ac638a9821ff1721c79b42bd7c182095f712f0b14ff5b2affb46c64973cdab3fbbc30148be89ff08aa49e3bb7b3fde20f524fc6594a45fde1425
7
+ data.tar.gz: 489d7aff7f7bfddec6ec25350c8d413fc2e689cc46cda67e051b3187afe959628de1c1640aaad64006b91e1c409a67005f872667f08485be73f2483dc72c048a
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+ # gemspec
3
+
4
+ gem 'httparty'
5
+ gem 'json'
6
+
7
+ group :test, :development do
8
+ gem 'bundler'
9
+ gem 'rspec'
10
+ gem 'rake'
11
+ gem 'webmock'
12
+ gem 'coveralls', require: false
13
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Shayon Mukherjee. https://shayon.me
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ [![Build Status](https://travis-ci.org/shayonj/emergency_number.svg?branch=master)](https://travis-ci.org/shayonj/emergency_number)
2
+ [![Coverage Status](https://coveralls.io/repos/github/shayonj/emergency_number/badge.svg?branch=master)](https://coveralls.io/github/shayonj/emergency_number?branch=master)
3
+
4
+ # EmergencyNumber
5
+ ```EmergencyNumber``` is a ruby gem for the [Emergency Number API](http://emergencynumberapi.com/) application.
6
+
7
+ * Returns handy object to for any emergency numbers for any country in the world.
8
+ * Takes in one argument - ```country code```. Country can be an ISO3361-1_alpha-2 or ISO3361-1_numeric code.
9
+ * Authentication is not required. Rate limit is the same as it is mentioned in [Emergency Number API docs](http://emergencynumberapi.com/docs#rate-limiting).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'emergency_number'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```ruby
22
+ $ bundle
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```ruby
28
+ $ gem install emergency_number
29
+ ```
30
+
31
+ ## Usage
32
+ ```ruby
33
+ require "emergency_number"
34
+ ```
35
+ ### Examples
36
+ ```ruby
37
+ $ EmergencyNumber.get_country(:us) # or EmergencyNumber.get_country(840)
38
+ $ EmergencyNumber.get_country(:in) # or EmergencyNumber.get_country(356)
39
+ $ EmergencyNumber.get_country(:fr) # or EmergencyNumber.get_country(250)
40
+ $ EmergencyNumber.get_country(:au) # or EmergencyNumber.get_country(36)
41
+ ```
42
+ ```ruby
43
+ $ @result = EmergencyNumber.get_country(:us)
44
+ {
45
+ :disclaimer => "The data from this API is provided without any claims of accuracy, you should use this data as guidance, and do your own due dilligence.",
46
+ :error => "",
47
+ :data => {
48
+ :country => {
49
+ :name => "United States of America",
50
+ :ISOCode => "US",
51
+ :ISONumeric => "840"
52
+ },
53
+ :ambulance => {
54
+ :all => [""],
55
+ :gsm => nil,
56
+ :fixed => nil
57
+ },
58
+ :fire => {
59
+ :all => [""],
60
+ :gsm => nil,
61
+ :fixed => nil
62
+ },
63
+ :police => {
64
+ :all => [""],
65
+ :gsm => nil,
66
+ :fixed => nil
67
+ },
68
+ :dispatch => {
69
+ :all => ["911"],
70
+ :gsm => nil,
71
+ :fixed => nil
72
+ },
73
+ :member_112 => false,
74
+ :localOnly => false,
75
+ :nodata => false
76
+ }
77
+ }
78
+ ```
79
+
80
+ ### Retrieving Data
81
+
82
+ ```ruby
83
+ $ @result.data
84
+ {
85
+ :country => {
86
+ :name => "United States of America",
87
+ :ISOCode => "US",
88
+ :ISONumeric => "840"
89
+ },
90
+ :ambulance => {
91
+ :all => [""],
92
+ :gsm => nil,
93
+ :fixed => nil
94
+ },
95
+ :fire => {
96
+ :all => [""],
97
+ :gsm => nil,
98
+ :fixed => nil
99
+ },
100
+ :police => {
101
+ :all => [""],
102
+ :gsm => nil,
103
+ :fixed => nil
104
+ },
105
+ :dispatch => {
106
+ :all => ["911"],
107
+ :gsm => nil,
108
+ :fixed => nil
109
+ },
110
+ :member_112 => false,
111
+ :localOnly => false,
112
+ :nodata => false
113
+ }
114
+ ```
115
+
116
+ ### Retrieving country name
117
+ ```ruby
118
+ $ @result.data.country.name
119
+ => "United States of America"
120
+ ```
121
+
122
+ ## Development
123
+
124
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
125
+
126
+ 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).
127
+
128
+ ## Contributing
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shayonj/emergency_number. 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.
130
+
131
+ ## License
132
+
133
+ [MIT License](https://github.com/shayonj/emergency_number/blob/master/MIT-LICENSE)
@@ -0,0 +1,25 @@
1
+ require_relative 'emergency_number/version.rb'
2
+ require_relative 'normalize'
3
+ require 'httparty'
4
+
5
+ # Helps fetch JSON data from API call and returns easy to work with object.
6
+ module EmergencyNumber
7
+ module_function
8
+
9
+ include HTTParty
10
+ # Sets the base_uri for making GET calls to Emergency Number API.
11
+ base_uri 'http://emergencynumberapi.com/api/country'
12
+
13
+ # Fetches list of Emergency Numbers from the Emergency Number API service.
14
+ #
15
+ # @param code [Symbol] the language code.
16
+ # @return [Hash] returned as Hash object in normalized dot notation form.
17
+ #
18
+ # @example Get list of number for Australia
19
+ # EmergencyNumber.get_country(:au)
20
+ def get_country(code = :us)
21
+ response = get("/#{code}/")
22
+ body = JSON.parse(response.body, symbolize_names: true)
23
+ body.extend(Normalize)
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module EmergencyNumber
2
+ # Final versioning for the gem.
3
+ VERSION = '0.3.1'.freeze
4
+ end
data/lib/normalize.rb ADDED
@@ -0,0 +1,16 @@
1
+ # Namespace and Wrapper that fetch JSON objects from Emergency Number API
2
+ # @since 0.1.0
3
+
4
+ module Normalize
5
+ # Return values from hash object by looking up keys from {EmergencyNumber.get_country}.
6
+ # Called when using dot notation on a hash object.
7
+ #
8
+ # @overload method_missing(sym, value)
9
+ # @param [Symbol] sym key in the hash object.
10
+ # @param [Object] * multiple number of args.
11
+ # @return [Object] value the respective hash key points to.
12
+ def method_missing(sym, *)
13
+ r = fetch(sym) { fetch(sym.to_s) { super } }
14
+ r.is_a?(Hash) ? r.extend(Normalize) : r
15
+ end
16
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmergencyNumber do
4
+ before :each do
5
+ @us = EmergencyNumber.get_country(:us)
6
+ @aq = EmergencyNumber.get_country(:aq)
7
+ @invalid = EmergencyNumber.get_country(:foobar)
8
+ end
9
+ it 'takes nil just fine' do
10
+ expect { EmergencyNumber.get_country }.to_not raise_error
11
+ end
12
+
13
+ context 'tests for valid country codes' do
14
+ it 'has a version number' do
15
+ expect(EmergencyNumber::VERSION).not_to be nil
16
+ end
17
+
18
+ it 'returns valid json set for valid ISO3361-1_alpha-2 or ISO3361-1_numeric code' do
19
+ expect(EmergencyNumber).to respond_to :get_country
20
+ expect(@us).not_to be_empty
21
+ expect(@us.data).not_to be_empty
22
+ end
23
+
24
+ it 'returns valid json data set for US code' do
25
+ expect(@us.data.country.name).to eq('United States of America')
26
+ expect(@us.error).to eq('')
27
+ expect(@us.data.dispatch.all).to eq(['911'])
28
+ expect(@us.data.dispatch.gsm).to eq(nil)
29
+ expect(@us.data.dispatch.fixed).to eq(nil)
30
+ expect(@us.data.nodata).to eq(false)
31
+ end
32
+
33
+ it 'returns non-empty and valid json set for country case like AQ.' do
34
+ expect(EmergencyNumber).to respond_to :get_country
35
+ expect(@aq).not_to be_empty
36
+ end
37
+
38
+ it 'returns valid json data set with valid values for AQ code' do
39
+ expect(@aq.data.country.name).to eq('Antarctica')
40
+ expect(@aq.error).to eq('No Data for this Territory')
41
+ expect(@aq.data.dispatch.all).to eq([''])
42
+ expect(@aq.data.dispatch.gsm).to eq(nil)
43
+ expect(@aq.data.dispatch.fixed).to eq(nil)
44
+ expect(@aq.data.nodata).to eq(true)
45
+ end
46
+ end
47
+
48
+ context 'tests for invalid country code' do
49
+ it 'returns valid json set' do
50
+ expect(EmergencyNumber).to respond_to :get_country
51
+ expect(@invalid).not_to be_empty
52
+ expect(@invalid.error).to eq('No Country Found')
53
+ expect(@invalid.data).to be_nil
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ {
2
+ "disclaimer":"The data from this API is provided without any claims of accuracy, you should use this data as guidance, and do your own due dilligence.",
3
+ "error":"No Data for this Territory",
4
+ "data":{
5
+ "country":{
6
+ "name":"Antarctica",
7
+ "ISOCode":"AQ",
8
+ "ISONumeric":"10"
9
+ },
10
+ "ambulance":{
11
+ "all":[
12
+ ""
13
+ ],
14
+ "gsm":null,
15
+ "fixed":null
16
+ },
17
+ "fire":{
18
+ "all":[
19
+ ""
20
+ ],
21
+ "gsm":null,
22
+ "fixed":null
23
+ },
24
+ "police":{
25
+ "all":[
26
+ ""
27
+ ],
28
+ "gsm":null,
29
+ "fixed":null
30
+ },
31
+ "dispatch":{
32
+ "all":[
33
+ ""
34
+ ],
35
+ "gsm":null,
36
+ "fixed":null
37
+ },
38
+ "member_112":false,
39
+ "localOnly":false,
40
+ "nodata":true
41
+ }
42
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "disclaimer":"The data from this API is provided without any claims of accuracy, you should use this data as guidance, and do your own due dilligence.",
3
+ "error":"No Country Found",
4
+ "data":null
5
+ }
@@ -0,0 +1,42 @@
1
+ {
2
+ "disclaimer":"The data from this API is provided without any claims of accuracy, you should use this data as guidance, and do your own due dilligence.",
3
+ "error":"",
4
+ "data":{
5
+ "country":{
6
+ "name":"United States of America",
7
+ "ISOCode":"US",
8
+ "ISONumeric":"840"
9
+ },
10
+ "ambulance":{
11
+ "all":[
12
+ ""
13
+ ],
14
+ "gsm":null,
15
+ "fixed":null
16
+ },
17
+ "fire":{
18
+ "all":[
19
+ ""
20
+ ],
21
+ "gsm":null,
22
+ "fixed":null
23
+ },
24
+ "police":{
25
+ "all":[
26
+ ""
27
+ ],
28
+ "gsm":null,
29
+ "fixed":null
30
+ },
31
+ "dispatch":{
32
+ "all":[
33
+ "911"
34
+ ],
35
+ "gsm":null,
36
+ "fixed":null
37
+ },
38
+ "member_112":false,
39
+ "localOnly":false,
40
+ "nodata":false
41
+ }
42
+ }
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Normalize do
4
+ context 'normalizes hashes' do
5
+ before :each do
6
+ @hsh_normal = { a: 1, b: 2, c: 3 }
7
+ @hsh_nest = { a: 1, b: 2, c: 3, d: { e: 4 } }
8
+ @hsh_nest_n = { a: 1, b: 2, c: 3, d: { e: 4, f: { g: 5 } } }
9
+ end
10
+
11
+ it 'with 0 nest level' do
12
+ @hsh_normal.extend(Normalize)
13
+ expect(@hsh_normal.a).to eql(1)
14
+ expect(@hsh_normal.b).to eql(2)
15
+ expect(@hsh_normal.c).to eql(3)
16
+ end
17
+
18
+ it 'with 1 nest level' do
19
+ @hsh_nest.extend(Normalize)
20
+ expect(@hsh_nest.d.e).to eql(4)
21
+ end
22
+
23
+ it 'with n nest level' do
24
+ @hsh_nest_n.extend(Normalize)
25
+ expect(@hsh_nest_n.d.e).to eql(4)
26
+ expect(@hsh_nest_n.d.f).to eql(g: 5)
27
+ expect(@hsh_nest_n.d.f.g).to eql(5)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'emergency_number'
3
+ require 'normalize'
4
+ require 'webmock/rspec'
5
+ WebMock.disable_net_connect!(allow_localhost: true)
6
+
7
+ require 'coveralls'
8
+ Coveralls.wear!
9
+
10
+ RSpec.configure do |config|
11
+ config.before(:each) do
12
+ # US
13
+ stub_request(:get, %r|http://emergencynumberapi.com/api/country/us/|)
14
+ .to_return(
15
+ status: 200,
16
+ body: File.open('./spec/fixtures/us_response.json', &:read),
17
+ headers: {}
18
+ )
19
+
20
+ # AQ
21
+ stub_request(:get, %r|http://emergencynumberapi.com/api/country/aq/|)
22
+ .to_return(
23
+ status: 200,
24
+ body: File.open('./spec/fixtures/aq_response.json', &:read),
25
+ headers: {}
26
+ )
27
+
28
+ # Invalid
29
+ stub_request(:get, %r|http://emergencynumberapi.com/api/country/foobar/|)
30
+ .to_return(
31
+ status: 200,
32
+ body: File.open('./spec/fixtures/invalid_response.json', &:read),
33
+ headers: {}
34
+ )
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emergency_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Shayon Mukherjee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Based on Emergency Number API. This ruby gem allows you to query the
14
+ API and get any emergency numbers for any country in the world.
15
+ email:
16
+ - dev@shayon.me
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - MIT-LICENSE
23
+ - README.md
24
+ - lib/emergency_number.rb
25
+ - lib/emergency_number/version.rb
26
+ - lib/normalize.rb
27
+ - spec/emergency_number_spec.rb
28
+ - spec/fixtures/aq_response.json
29
+ - spec/fixtures/invalid_response.json
30
+ - spec/fixtures/us_response.json
31
+ - spec/normalize_spec.rb
32
+ - spec/spec_helper.rb
33
+ homepage: https://github.com/shayonj/emergency_number
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.5.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Get any emergency numbers for any country in the world.
57
+ test_files: []