weatherman-rb 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODQwYjY1MWJhN2M3NjI4Yjc0ZGZlMGQzYzEzZGUyMTRiODg0MWExMQ==
5
+ data.tar.gz: !binary |-
6
+ MjBhYjlhZGM4ZDk3MjI2NGVhZTcwZTIyMmM2OWZmMzM2ZGNhYzRmZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDIxOTFhOTkwYjFiMTM4Zjk1YmUxNzk1YmEyNTZlYWZhYmFlMDIxYjhjYzUx
10
+ YjFjMzNlNWVhZmRhNDFmYWJmZGNlMzIyOTBlMGQwZmJkM2FiYTk5NzkxNTZk
11
+ ZDJhNGUxZjAwOGU0MTA4N2VhMDc4Njk1ZjIwN2RmZmY3NjU1ZTE=
12
+ data.tar.gz: !binary |-
13
+ ZjFkM2Y4ZDBmMzZjNjRiNTNjYTg4NGI2MGRhNzM1ZWQ5MjEyNzE1YjA5NDdi
14
+ NTNkNzMxN2U5YjRhM2U3ZTU4MDIwYzA4M2ZhMGFkNjA4OTdhZTE5NzE0OWZl
15
+ YjEyYWU1MDIxZDQ3NzhkNmI0MzU4ZGZjNmYxM2E1YmM5NDczMmE=
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weatherman.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rocky Jaiswal
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Weatherman
2
+
3
+ A simple little wrapper over - [http://www.webservicex.net/WS/WSDetails.aspx?WSID=56&CATID=12](http://www.webservicex.net/WS/WSDetails.aspx?WSID=56&CATID=12) Weather SOAP Service
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'weatherman'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install weatherman
18
+
19
+ ## Usage
20
+
21
+ ###To get the list of available cities, you can do -
22
+
23
+ Weatherman.get_cities_by_country("Poland")
24
+
25
+ This will return an array of Cities. Each "City" has two fields -
26
+
27
+ - city_name
28
+ - country_name
29
+
30
+
31
+ ###To get the weather, you can do -
32
+
33
+ Weatherman.get_weather("Jodhpur", "India")
34
+
35
+ This will retrun a "Weather" object, which has a few fields -
36
+
37
+ - location
38
+ - time
39
+ - wind
40
+ - visibility
41
+ - temperature
42
+ - pressure
43
+ - status (as received from the web service)
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :test do
4
+ system "bundle exec rspec spec"
5
+ end
6
+
7
+ task :default => :test
data/lib/weatherman.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'weatherman/version'
2
+ require 'weatherman/soap_client'
3
+ require 'weatherman/parser'
4
+ require 'weatherman/soap_wrapper'
5
+
6
+ module Weatherman
7
+
8
+ def self.get_cities_by_country(country)
9
+ soap_wrapper = init()
10
+ soap_wrapper.get_cities_by_country(country)
11
+ end
12
+
13
+ def self.get_weather(city, country)
14
+ soap_wrapper = init()
15
+ soap_wrapper.get_weather(city, country)
16
+ end
17
+
18
+ private
19
+ def self.init
20
+ soap_client = Weatherman::SOAPClient.new
21
+ parser = Weatherman::Parser.new
22
+ Weatherman::SOAPWrapper.new(soap_client, parser)
23
+ end
24
+
25
+ end
@@ -0,0 +1,12 @@
1
+ module Weatherman
2
+
3
+ class City
4
+ attr_reader :city_name, :country_name
5
+
6
+ def initialize(city_name, country_name)
7
+ @city_name = city_name
8
+ @country_name = country_name
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,3 @@
1
+ module Weatherman
2
+ WSDL_ENDPOINT = "http://www.webservicex.net/globalweather.asmx?WSDL"
3
+ end
@@ -0,0 +1 @@
1
+ class WeathermanException < StandardError; end
@@ -0,0 +1,46 @@
1
+ require 'weatherman/city'
2
+ require 'weatherman/weather'
3
+
4
+ module Weatherman
5
+ class Parser
6
+
7
+ def parse_cities_response(response)
8
+ xml = Nokogiri::XML(response)
9
+ build_cities(xml)
10
+ end
11
+
12
+ def parse_weather_response(response)
13
+ xml = Nokogiri::XML(response)
14
+ link = xml.xpath("//CurrentWeather").first
15
+ return nil if link.nil?
16
+ return build_weather(link) unless link.nil?
17
+ end
18
+
19
+ private
20
+
21
+ def build_cities(xml)
22
+ cities = []
23
+ xml.xpath("//NewDataSet").each do |tab|
24
+ tab.xpath("//Table/Country").children.each_with_index do |country, index|
25
+ country_name = country.content.strip
26
+ city_name = tab.xpath("//Table/City").children[index].content.strip
27
+ city = Weatherman::City.new(city_name, country_name)
28
+ cities << city
29
+ end
30
+ end
31
+ cities
32
+ end
33
+
34
+ def build_weather(link)
35
+ location = link.xpath("//Location").first.content.strip
36
+ time = link.xpath("//Time").first.content.strip
37
+ wind = link.xpath("//Wind").first.content.strip
38
+ visibility = link.xpath("//Visibility").first.content.strip
39
+ temperature = link.xpath("//Temperature").first.content.strip
40
+ pressure = link.xpath("//Pressure").first.content.strip
41
+ status = link.xpath("//Status").first.content.strip
42
+ Weatherman::Weather.new(location, time, wind, visibility, temperature, pressure, status)
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ require 'weatherman/exception'
2
+ require 'weatherman/constants'
3
+ require 'savon'
4
+
5
+ module Weatherman
6
+ class SOAPClient
7
+
8
+ def initialize
9
+ begin
10
+ @client = Savon.client(wsdl: Weatherman::WSDL_ENDPOINT)
11
+ rescue Exception => ex
12
+ raise WeathermanException, ex.message
13
+ end
14
+ end
15
+
16
+ def get_cities_by_country(country)
17
+ @client.call(:get_cities_by_country, message: { "CountryName" => country }).body
18
+ end
19
+
20
+ def get_weather(city, country)
21
+ @client.call(:get_weather, message: { "CityName" => city, "CountryName" => country }).body
22
+ end
23
+
24
+ #Not really used
25
+ def get_operations
26
+ @client.operations
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ require 'weatherman/exception'
2
+ require 'weatherman/soap_client'
3
+ require 'weatherman/parser'
4
+
5
+ module Weatherman
6
+ class SOAPWrapper
7
+
8
+ def initialize(soap_client, parser)
9
+ @soap_client = soap_client
10
+ @parser = parser
11
+ end
12
+
13
+ def get_cities_by_country(country)
14
+ begin
15
+ response = @soap_client.get_cities_by_country(country)
16
+ xml = response[:get_cities_by_country_response][:get_cities_by_country_result]
17
+ @parser.parse_cities_response(xml)
18
+ rescue Exception => ex
19
+ raise WeathermanException, ex.message
20
+ end
21
+ end
22
+
23
+ def get_weather(city, country)
24
+ begin
25
+ response = @soap_client.get_weather(city, country)
26
+ xml = response[:get_weather_response][:get_weather_result]
27
+ @parser.parse_weather_response(xml)
28
+ rescue Exception => ex
29
+ raise WeathermanException, ex.message
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Weatherman
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,19 @@
1
+ module Weatherman
2
+
3
+ class Weather
4
+
5
+ attr_reader :location, :time, :wind, :visibility, :temperature, :pressure, :status
6
+
7
+ def initialize(location, time, wind, visibility, temperature, pressure, status)
8
+ @location = location
9
+ @time = time
10
+ @wind = wind
11
+ @visibility = visibility
12
+ @temperature = temperature
13
+ @pressure = pressure
14
+ @status = status
15
+ end
16
+
17
+ end
18
+
19
+ end
data/spec/.DS_Store ADDED
Binary file
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'weatherman')
2
+ require File.join(File.dirname(__FILE__), '..', 'lib/weatherman', 'soap_client')
3
+ require File.join(File.dirname(__FILE__), '..', 'lib/weatherman', 'soap_wrapper')
4
+ require File.join(File.dirname(__FILE__), '..', 'lib/weatherman', 'parser')
@@ -0,0 +1,31 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Weatherman do
4
+ describe "Parser" do
5
+
6
+ before(:each) do
7
+ @parser = Weatherman::Parser.new
8
+ end
9
+
10
+ it "should parse cities response XML properly" do
11
+ response = '<NewDataSet><Table><Country>British Indian Ocean Territory</Country><City>Diego Garcia</City></Table><Table><Country>India</Country><City>Ahmadabad</City></NewDataSet>'
12
+ cities = @parser.parse_cities_response(response)
13
+ cities.size.should == 2
14
+ cities.select{|c| c.city_name == "Ahmadabad"}.size.should == 1
15
+ end
16
+
17
+ it "should parse weather response XML properly" do
18
+ response = '<CurrentWeather><Location>Jodhpur, India (VIJO) 26-18N 073-01E 224M</Location><Time>Jan 25, 2010 - 02:30 AM EST / 2010.01.25 0730 UTC</Time><Wind> from the NE (050 degrees) at 5 MPH (4 KT):0</Wind><Visibility> 3 mile(s):0</Visibility><Temperature> 75 F (24 C)</Temperature><Pressure> 30.06 in. Hg (1018 hPa)</Pressure><Status>Success</Status></CurrentWeather>'
19
+ weather = @parser.parse_weather_response(response)
20
+ weather.status.should == "Success"
21
+ weather.temperature.should == "75 F (24 C)"
22
+ end
23
+
24
+ it "should parse not found city properly" do
25
+ response = "Data Not Found"
26
+ weather = @parser.parse_weather_response(response)
27
+ weather.should be_nil
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Weatherman do
4
+ describe "SOAPClient" do
5
+
6
+ before(:each) do
7
+ end
8
+
9
+ it "should initialize the client properly" do
10
+ soap_client = Weatherman::SOAPClient.new
11
+ soap_client.should_not be_nil
12
+ end
13
+
14
+ it "should get the SOAP operations" do
15
+ soap_client = Weatherman::SOAPClient.new
16
+ soap_client.get_operations.size.should == 2
17
+ end
18
+
19
+ it "should handle exception properly" do
20
+ Savon.stub(:client).and_raise(Exception)
21
+ lambda { Weatherman::SOAPClient.new }.should raise_error(WeathermanException)
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,50 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Weatherman do
4
+ describe "SOAPWrapper" do
5
+
6
+ before(:each) do
7
+ soap_client = Weatherman::SOAPClient.new
8
+ parser = Weatherman::Parser.new
9
+ @soap_wrapper = Weatherman::SOAPWrapper.new(soap_client, parser)
10
+ end
11
+
12
+ it "should initialize the wrapper properly" do
13
+ @soap_wrapper.should_not be_nil
14
+ end
15
+
16
+ it "should get some cities in India" do
17
+ cities = @soap_wrapper.get_cities_by_country("India")
18
+ cities.size.should_not == 0
19
+ end
20
+
21
+ it "should return blank array if country is wrong" do
22
+ cities = @soap_wrapper.get_cities_by_country("Noida")
23
+ cities.size.should == 0
24
+ end
25
+
26
+ #it "should get some cities in Germany" do
27
+ # @soap_wrapper.get_cities_by_country("Germany").size.should_not == 0
28
+ #end
29
+
30
+ it "should get weather for Jodhpur India" do
31
+ weather = @soap_wrapper.get_weather("Jodhpur", "India")
32
+ weather.status.should == "Success"
33
+ end
34
+
35
+ it "should not crash for bad data" do
36
+ weather = @soap_wrapper.get_weather("Jakarta", "India")
37
+ weather.should be_nil
38
+ end
39
+
40
+ it "should handle exception properly" do
41
+ soap_client = double("SOAPClient")
42
+ soap_client.stub(:get_cities_by_country).and_raise(Exception)
43
+ parser = Weatherman::Parser.new
44
+ soap_wrapper = Weatherman::SOAPWrapper.new(soap_client, parser)
45
+ lambda { soap_wrapper.get_cities_by_country("") }.should raise_error(WeathermanException)
46
+ end
47
+
48
+ end
49
+ end
50
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'weatherman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "weatherman-rb"
8
+ spec.version = Weatherman::VERSION
9
+ spec.authors = ["Rocky Jaiswal"]
10
+ spec.email = ["rocky.jaiswal@gmail.com"]
11
+ spec.description = %q{A wrapper over GlobalWeather SOAP services}
12
+ spec.summary = %q{Get the current weather for major cities of the world}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "savon", "~> 2.3.0"
22
+ spec.add_dependency "nokogiri"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weatherman-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rocky Jaiswal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A wrapper over GlobalWeather SOAP services
84
+ email:
85
+ - rocky.jaiswal@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .DS_Store
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/weatherman.rb
97
+ - lib/weatherman/city.rb
98
+ - lib/weatherman/constants.rb
99
+ - lib/weatherman/exception.rb
100
+ - lib/weatherman/parser.rb
101
+ - lib/weatherman/soap_client.rb
102
+ - lib/weatherman/soap_wrapper.rb
103
+ - lib/weatherman/version.rb
104
+ - lib/weatherman/weather.rb
105
+ - spec/.DS_Store
106
+ - spec/spec_helper.rb
107
+ - spec/weatherman/parser_spec.rb
108
+ - spec/weatherman/soap_client_spec.rb
109
+ - spec/weatherman/soap_wrapper_spec.rb
110
+ - weatherman.gemspec
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Get the current weather for major cities of the world
135
+ test_files:
136
+ - spec/.DS_Store
137
+ - spec/spec_helper.rb
138
+ - spec/weatherman/parser_spec.rb
139
+ - spec/weatherman/soap_client_spec.rb
140
+ - spec/weatherman/soap_wrapper_spec.rb