current_temp 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/current_temp.rb +39 -0
  3. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8c72cc7100910a343cdbb70b7a6d41549b2f98d
4
+ data.tar.gz: 64fff046b179bcb789670842d2aa55041a791a49
5
+ SHA512:
6
+ metadata.gz: 7558bb89883ee92843f026f9d9872f9f7b0f0ab887a5b0981f90b49e854cc6f6c63984136a695ee6a8c6c6d53781025711bea684904cf8f14b0a7133e261625b
7
+ data.tar.gz: d24dd36f5d26fd346bb5a1b5641cb11af04eb38fde739d142c92c7529ea4c53647afa69d4d99b98ab2508fa10f06a97deaca0a28ce1c257f2f6abca99bf77c68
@@ -0,0 +1,39 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+
4
+ class CurrentTemperature
5
+ class MissingAPIKey < StandardError; end
6
+ class APIError < RuntimeError; end
7
+ class InvalidLocation < ArgumentError
8
+ def initialize(message)
9
+ super(message)
10
+ end
11
+ end
12
+
13
+ API_KEY = ENV['WUNDERGROUND_API_KEY']
14
+
15
+ def self.at_location(location)
16
+ raise MissingAPIKey if API_KEY.nil?
17
+ location = location.to_s
18
+
19
+ if location.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/)
20
+ location_query = location
21
+ elsif location.match(/[a-z,A-Z]/)
22
+ city = location.split(/,/).first.gsub(' ','_')
23
+ state = location.split(/,/).last.strip
24
+ location_query = "#{state}/#{city}"
25
+ else
26
+ raise InvalidLocation.new("You location #{location}, was invalid")
27
+ end
28
+
29
+ base_url = "http://api.wunderground.com/api/#{API_KEY}/geolookup/conditions/q/#{location_query}.json"
30
+
31
+ open(base_url) do |f|
32
+ json_string = f.read
33
+ parsed_json = JSON.parse(json_string)
34
+ location = parsed_json['location']['city']
35
+ temperature = parsed_json['current_observation']['temp_f']
36
+ return temperature
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: current_temp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Bunting
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Pass a valid US ZIP code, or a city and state and the current temperate
14
+ in degrees fahrenheit is returned. This requies a valid United States ZIP code as
15
+ a Fixnum such as 94110 or a city in a String like "San Francisco, Ca"
16
+ email: christophersbunting@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/current_temp.rb
22
+ homepage: http://rubygems.org/gems/current_temp
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Returns the current temperature in °F for the provided location.
46
+ test_files: []
47
+ has_rdoc: