lombard 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +54 -0
  3. data/bin/lombard +49 -0
  4. data/lib/lombard.rb +121 -0
  5. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7bd1a397e6c097d76a7b77e2898c2b5797a4d74
4
+ data.tar.gz: 770c30edfdee60f3ff0226fdea3b82a5ab950b8d
5
+ SHA512:
6
+ metadata.gz: e436e64ed4f2b0b4fccdcaeff0e497f54ebcb3583fd4ce4882b784431d7de32d160c7f150c53295fbb5bb806c8d769646db95c9016e3e07c2ae3aca9f8b5010e
7
+ data.tar.gz: 8cebf9474ef773fa9074d26c897cdb54107175efdac2afb2ceeae20f97016a802ae2d8503488e5c6987e696d17c38f2e320f9e3c0cdfdba24f8b97e19d13ce68
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Lombard
2
+ Quickly overview weather forecast in your preferred cities, using [OpenWeatherMap API](http://openweathermap.org/).
3
+
4
+ ## Get started
5
+
6
+ To install Lombard, run this command:
7
+
8
+ ```
9
+ gem install lombard
10
+ ```
11
+
12
+ It is already done. That was fast!
13
+
14
+ ## Commands
15
+
16
+ For fetching forecast, just run lombard with any city name:
17
+
18
+ ```
19
+ $ lombard London
20
+ # London (GB)
21
+ Clear - Sky is Clear
22
+ 1°C / 34°F
23
+
24
+ $ lombard San Francisco
25
+ # San Francisco (US)
26
+ Clear - Sky is Clear
27
+ 20°C / 69°F
28
+ ```
29
+
30
+ ### Options
31
+
32
+ #### `--favorites`
33
+
34
+ Displays forecast for any favorite. Firstly, run `lombard --init` for creating a default config file (`~/.lombard.yml`). Then, list your favorite cities in this YAML file:
35
+
36
+ ```yaml
37
+ - San Francisco
38
+ - Bordeaux
39
+ - Périgueux
40
+ ```
41
+
42
+ Running `lombard` without any argument is an alias for `lombard --favorites`.
43
+
44
+ #### `--help`
45
+
46
+ Displays help message.
47
+
48
+ #### `--init`
49
+
50
+ Generates empty configuration file.
51
+
52
+ #### `--version`
53
+
54
+ Gets current version.
data/bin/lombard ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+ require 'lombard'
3
+
4
+ def version
5
+ puts "Lombard #{Lombard.version} - Adrien Cadet"
6
+ end
7
+
8
+ def help
9
+ puts "Lombard fetches weather to your terminal.
10
+
11
+ Usage:
12
+ lombard \"<city>\"
13
+ Where
14
+ city: Any city on Earth. Mars will be soon supported.
15
+
16
+ Options:
17
+ --favorites Fetches forecast for each favorite
18
+ --help Displays this message
19
+ --init Creates default config file
20
+ --version Gets current version
21
+ "
22
+ end
23
+
24
+ def init
25
+ Lombard.init_config
26
+ end
27
+
28
+ def favorites
29
+ Lombard.new().favorites()
30
+ end
31
+
32
+ if ARGV.size < 1
33
+ favorites
34
+ else
35
+ arg = ARGV[0]
36
+
37
+ if arg == '--version'
38
+ version
39
+ elsif arg == '--help'
40
+ help
41
+ elsif arg == '--init'
42
+ init
43
+ elsif arg == '--favorites'
44
+ favorites
45
+ else
46
+ Lombard.new().run(ARGV.join(" "))
47
+ end
48
+ end
49
+
data/lib/lombard.rb ADDED
@@ -0,0 +1,121 @@
1
+ require 'httparty'
2
+ #require 'pry'
3
+ require 'uri'
4
+ require 'yaml'
5
+
6
+ class Lombard
7
+ @@api_url = "http://api.openweathermap.org/data/2.5/weather"
8
+ @@favorite_file = File.expand_path "~/.lombard.yml"
9
+
10
+ def initialize
11
+ @no_connection = false
12
+ end
13
+
14
+ def self.version
15
+ "1.0.0"
16
+ end
17
+
18
+ ###
19
+ # Fetches forecast from remote API
20
+ ##
21
+ def fetch(location)
22
+ begin
23
+ response = HTTParty.get "#{@@api_url}?q=#{URI.escape(location)}"
24
+ body = JSON.parse response.body
25
+
26
+ # Check if city exists
27
+ (body['cod'] == 200) ? body : nil
28
+ rescue
29
+ puts "Whoops! I failed to reach server. You should check your connection!"
30
+ @no_connection = true
31
+ nil
32
+ end
33
+ end
34
+
35
+ ###
36
+ # Formats pulled data
37
+ ##
38
+ def format(o)
39
+ if o == nil
40
+ # No data
41
+ return nil
42
+ end
43
+
44
+ outcome = "# #{o['name']}"
45
+
46
+ country = o['sys']['country']
47
+ if country != nil
48
+ outcome += " (#{country})"
49
+ end
50
+
51
+ outcome += "\n #{o['weather'].first['main']}"
52
+
53
+ description = o['weather'].first['description']
54
+ if description != nil
55
+ outcome += " - #{description}"
56
+ end
57
+
58
+ temp = o['main']['temp']
59
+ celsius = temp.to_f - 273.15
60
+ fahrenheit = celsius * 1.8 + 32
61
+ outcome += "\n #{celsius.to_i}°C / #{fahrenheit.to_i}°F"
62
+
63
+ return outcome
64
+ end
65
+
66
+ ###
67
+ # Fetches forecast for each favorite
68
+ ##
69
+ def favorites()
70
+ if File.exists? @@favorite_file
71
+ locations = YAML.load_file @@favorite_file
72
+
73
+ if locations
74
+ locations.each_with_index do |e, i|
75
+ if i != 0
76
+ puts '----------------------------'
77
+ end
78
+ run(e)
79
+ end
80
+ else
81
+ puts "No favorite in your config file. Visit https://github.com/acadet/lombard for more details."
82
+ end
83
+ else
84
+ # No config file
85
+ puts "No favorite file found. Run \"lombard --init\" to generate it."
86
+ end
87
+ end
88
+
89
+ ###
90
+ # Generates empty config file
91
+ ##
92
+ def self.init_config
93
+ if File.exists? @@favorite_file
94
+ # Do not override existing file
95
+ puts "Config file is already existing (#{@@favorite_file})."
96
+ else
97
+ f = File.new @@favorite_file, "w"
98
+ f.puts '###############################################'
99
+ f.puts '### Lombard config file ###'
100
+ f.puts '### ###'
101
+ f.puts '### Visit https://github.com/acadet/lombard ###'
102
+ f.puts '### to know more about this file ###'
103
+ f.puts '###############################################'
104
+ puts "Successfully created #{@@favorite_file}!"
105
+ end
106
+ end
107
+
108
+ ###
109
+ # Here comes the magic trick
110
+ ##
111
+ def run(location)
112
+ result = format(fetch(location))
113
+
114
+ if result != nil
115
+ puts result
116
+ elsif !@no_connection
117
+ # No data and user is connected to the fabulous Internet, full of unicorns and rainbows
118
+ puts "#{location} does not exist on Earth. You may be an alien!"
119
+ end
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lombard
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adrien Cadet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri
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: yaml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Quickly overview weather forecast in your preferred cities.
56
+ email: acadet@live.fr
57
+ executables:
58
+ - lombard
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - bin/lombard
64
+ - lib/lombard.rb
65
+ homepage: https://github.com/acadet/lombard
66
+ licenses: []
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Fetches weather to your terminal
88
+ test_files: []