gassy 1.0.0

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ before_script:
8
+ - gem install nokogiri
9
+ - gem install rspec
10
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gassy.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'nokogiri'
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+
2
+ = Gassy {<img src="https://secure.travis-ci.org/bballer320xu/Gassy.png?branch=master&rvm=1.9.3" alt="Build Status" />}[http://travis-ci.org/bballer320xu/Gassy]
3
+
4
+ * https://github.com/bballer320xu/Gassy
5
+
6
+ == DESCRIPTION:
7
+
8
+ Gassy is a simple gem that processes Gas price data publicly available on the U.S. Energy Information Administration website.
9
+
10
+ == FEATURES:
11
+
12
+ * Quick Gas Price lookup for Major Cities, States, and Regions
13
+ * Extracts live data from the U.S. Energy Information Administration website
14
+
15
+ == SYNOPSIS:
16
+
17
+ # First Initiate Gassy
18
+ require 'gassy'
19
+
20
+
21
+ # Then get lots of data & process it.
22
+ Gassy.get_all_price_data.each_pair do |entity, sub_entities|
23
+ puts "processing #{entity}"
24
+ sub_entities.each_pair do |sub_entity, price|
25
+ puts "The gas price in #{sub_entity} is #{price}."
26
+ end
27
+ end
28
+
29
+
30
+ #Or get a specific piece of data
31
+ price = Gassy.extract_one_price("Boston")
32
+ puts "The gas price in Boston is #{price}."
33
+
34
+
35
+ == REQUIREMENTS:
36
+
37
+ * ruby 1.9
38
+
39
+ == INSTALL:
40
+
41
+ * sudo gem install gassy
42
+
43
+ == LICENSE:
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2008 - 2012:
48
+
49
+ * {Matt Lucas}[https://github.com/bballer320xu]
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining
52
+ a copy of this software and associated documentation files (the
53
+ 'Software'), to deal in the Software without restriction, including
54
+ without limitation the rights to use, copy, modify, merge, publish,
55
+ distribute, sublicense, and/or sell copies of the Software, and to
56
+ permit persons to whom the Software is furnished to do so, subject to
57
+ the following conditions:
58
+
59
+ The above copyright notice and this permission notice shall be
60
+ included in all copies or substantial portions of the Software.
61
+
62
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
63
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
65
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
66
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
67
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
68
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new('spec')
data/gassy.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gassy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gassy"
7
+ s.version = Gassy::VERSION
8
+ s.authors = ["Matt Lucas"]
9
+ s.email = ["matt.k.lucas@gmail.com"]
10
+ s.summary = %q{Gas Price Retrieval Gem}
11
+ s.description = %q{This gem will gather weekly gas prices from the U.S. Energy Information Information site}
12
+ s.homepage = "https://github.com/bballer320xu/Gassy"
13
+
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency "nokogiri"
22
+
23
+ end
data/lib/.DS_Store ADDED
Binary file
data/lib/gassy.rb ADDED
@@ -0,0 +1,118 @@
1
+ require "open-uri"
2
+ require "gassy/gassy_utils"
3
+ require "gassy/gassy_constants"
4
+
5
+
6
+ # Gassy is a simple gem that processes Gas price data publicly available on the U.S. Energy Information Administration website.
7
+ #
8
+ # Features:
9
+ # * Quick Gas Price lookup for Major Cities, States, and Regions
10
+ # * Extracts live data from the U.S. Energy Information Administration website
11
+ #
12
+ module Gassy
13
+
14
+ @file = nil
15
+
16
+ class << self
17
+ attr_reader :file
18
+
19
+
20
+
21
+ # Get all Price Data and return it as a nested hash
22
+ #
23
+ # Example:
24
+ # >> price_hash = gassy.get_all_price_data
25
+ #
26
+ def get_all_price_data
27
+ @data = {:cities => {}, :states => {}, :regions => {}}
28
+ @data.each_key do |key|
29
+ GassyUtils.send("#{key}").each do |entity|
30
+ @data[key][entity] = extract_one_price(entity)
31
+ end
32
+ end
33
+ @data
34
+ end
35
+
36
+
37
+
38
+ # Get one Price for an entity and return it as a Float. Return nil if the entity is not found
39
+ #
40
+ # Example:
41
+ # >> boston_price = gassy.extract_one_price("Boston")
42
+ #
43
+ # * +entity+ - An entity to look up a price for. Can be a city, state, or region.
44
+ #
45
+ def extract_one_price(entity)
46
+ init_file
47
+ condensed = GassyUtils.condense_xml(@file)
48
+ remove_diesel = condensed.match(/.*On-Highway/).to_s
49
+ listing = remove_diesel.match(/\d*.\d*\D*#{entity.gsub(/\s+/, "")}(&lt;)/).to_s
50
+ price = listing.match(/\d*.\d*/).to_s.to_f
51
+ end
52
+
53
+
54
+
55
+ # This will force the cached datafile to be refreshed with new data from the website.
56
+ #
57
+ # Example:
58
+ # >> Gassy.refresh_datafile
59
+ #
60
+ def refresh_datafile
61
+ init_file(true)
62
+ end
63
+
64
+
65
+
66
+ # This will force the cached datafile to be refreshed with a locally provided file.
67
+ #
68
+ # Example:
69
+ # >> Gassy.load_datafile
70
+ #
71
+ # * +file_path+ - The path to the file to process
72
+ #
73
+ def load_datafile(file_path)
74
+ init_file(true, file_path)
75
+ end
76
+
77
+
78
+
79
+ private
80
+
81
+
82
+
83
+ #
84
+ # Method to initialize or retrieved the data file.
85
+ #
86
+ def init_file(refresh = false, file = nil)
87
+ if (@file.nil? || refresh == true)
88
+ unless !file.nil?
89
+ file = GassyConstants::SOURCE_FILE
90
+ end
91
+ @file = GassyUtils.rename(open(file).read, "West Coast less California", "North West Coast")
92
+ if @file.nil?
93
+ raise IOError, "A source file could not be read at #{file}"
94
+ end
95
+ else
96
+ @file
97
+ end
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
@@ -0,0 +1,39 @@
1
+ module Gassy
2
+ module GassyConstants
3
+
4
+ SOURCE_FILE = 'http://www.eia.gov/petroleum/gasdiesel/includes/gas_diesel_rss.xml'
5
+
6
+ ENTITIES = ["cities", "regions", "states"]
7
+
8
+ CITIES = ["Boston",
9
+ "Chicago",
10
+ "Cleveland",
11
+ "Denver",
12
+ "Houston",
13
+ "Los Angeles",
14
+ "Miami",
15
+ "New York City",
16
+ "San Francisco",
17
+ "Seattle"]
18
+
19
+ REGIONS = ["U.S.","East Coast",
20
+ "New England",
21
+ "Central Atlantic",
22
+ "Lower Atlantic",
23
+ "Midwest",
24
+ "Gulf Coast",
25
+ "Rocky Mountain",
26
+ "West Coast",
27
+ "North West Coast"]
28
+
29
+ STATES = ["California",
30
+ "Colorado",
31
+ "Florida",
32
+ "Massachusetts",
33
+ "Minnesota",
34
+ "New York",
35
+ "Ohio",
36
+ "Texas",
37
+ "Washington"]
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # Utility classes used by GassyUtils
3
+ #
4
+ module Gassy
5
+ module GassyUtils
6
+
7
+ # Condense the provided XML file by removing all spaces and new lines
8
+ #
9
+ # Example:
10
+ # >> condensed = GassyUtils.condense_xml(file)
11
+ #
12
+ def self.condense_xml(file)
13
+ file = file.gsub(/\r/,"")
14
+ file = file.gsub(/\n/,"")
15
+ file = file.gsub(/\t/,"")
16
+ file = file.gsub(/\s+/,"")
17
+ end
18
+
19
+ # Simple string substitution method alias
20
+ #
21
+ # Example:
22
+ # >> file_with_renamed_entry = GassyUtils.rename(file, "Joe", "Sam")
23
+ #
24
+ def self.rename(file, old_name, new_name)
25
+ file.sub(old_name, new_name)
26
+ end
27
+
28
+ # Automatically generate methods that returns data for the Entities listed in the Gassy::GassyConstants::ENTITIES array.
29
+ #
30
+ # Example:
31
+ # >> array_of_city_names = GassyUtils.cities
32
+ #
33
+ def self.method_missing(name, *args)
34
+ super unless GassyConstants::ENTITIES.include?(name.to_s)
35
+ eval("GassyConstants::#{name.upcase}")
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Gassy
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe "The Gassy main class" do
4
+
5
+ describe "should have a method 'get_all_price_data'" do
6
+ it "that returns a proper hash" do
7
+ Gassy.get_all_price_data.should be_an_instance_of(Hash)
8
+ end
9
+ end
10
+
11
+ describe "should have a method 'extract_one_price'" do
12
+ it "that returns a proper Float value for a correct entity" do
13
+ Gassy.extract_one_price("Boston").should be_an_instance_of(Float)
14
+ end
15
+
16
+ it "that returns nil for an incorrect entity" do
17
+ Gassy.extract_one_price("Ottawa").should be_an_instance_of(Float)
18
+ end
19
+ end
20
+ end
21
+
22
+
23
+ describe "The Gassy Utils class" do
24
+ describe "should have a method to return all vaild cities" do
25
+ it "as an array" do
26
+ Gassy::GassyUtils.cities.should be_an_instance_of(Array)
27
+ end
28
+ end
29
+ describe "should have a method to return all vaild states" do
30
+ it "as an array" do
31
+ Gassy::GassyUtils.states.should be_an_instance_of(Array)
32
+ end
33
+ end
34
+ describe "should have a method to return all vaild regions" do
35
+ it "as an array" do
36
+ Gassy::GassyUtils.regions.should be_an_instance_of(Array)
37
+ end
38
+ end
39
+ describe "should handle invalid method names" do
40
+ it "by raising an error" do
41
+ proc{Gassy.garbagemethod}.should raise_error
42
+ end
43
+ end
44
+ end
45
+
46
+
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe "The XML Gasoline Data File" do
4
+ before(:all) do
5
+ #Compress the XML file for simplified regex processing
6
+ @compressed_file = Gassy::GassyUtils.condense_xml(Gassy.file)
7
+ end
8
+
9
+ it "should return a vaild XML file for processing" do
10
+ parsed_doc = Nokogiri::XML(Gassy.file) do |config|
11
+ config.options = Nokogiri::XML::ParseOptions::STRICT
12
+ end
13
+ parsed_doc.errors.count.should eq(0)
14
+ end
15
+
16
+
17
+ describe "will contain cities" do
18
+ Gassy::GassyConstants::CITIES.each do |city|
19
+ it "and should contain the city of '#{city}'" do
20
+ @compressed_file.scan(/Cities.*#{city.gsub(/\s+/, "")}/).should_not be_empty
21
+ end
22
+
23
+ it "and should extract a gas price for the city of '#{city}' as a float" do
24
+ Gassy.extract_one_price(city).should be_an_instance_of(Float)
25
+ end
26
+
27
+ it "and should extract a gas price for the city of '#{city}' greater than 0" do
28
+ Gassy.extract_one_price(city).should be > 0
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "will contain states" do
34
+ Gassy::GassyConstants::STATES.each do |state|
35
+ it "and should contain the state of '#{state}'" do
36
+ @compressed_file.scan(/States.*#{state.gsub(/\s+/, "")}/).should_not be_empty
37
+ end
38
+
39
+ it "and should extract a gas price for the state of '#{state}' as a float" do
40
+ Gassy.extract_one_price(state).should be_an_instance_of(Float)
41
+ end
42
+
43
+ it "and should extract a gas price for the state of '#{state}' greater than 0" do
44
+ Gassy.extract_one_price(state).should be > 0
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "will contain regions" do
50
+ Gassy::GassyConstants::REGIONS.each do |region|
51
+ it "and should contain the region of '#{region}'" do
52
+ @compressed_file.scan(/RegularGasolineRetailPrice.*#{region.gsub(/\s+/, "")}/).should_not be_empty
53
+ end
54
+
55
+ it "and should extract a gas price for the region of '#{region}' as a float" do
56
+ Gassy.extract_one_price(region).should be_an_instance_of(Float)
57
+ end
58
+
59
+ it "and should extract a gas price for the region of '#{region}' greater than 0" do
60
+ Gassy.extract_one_price(region).should be > 0
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require "nokogiri"
3
+ require 'gassy'
4
+
5
+ RSpec.configure do |config|
6
+ config.color_enabled = true
7
+ config.formatter = 'documentation'
8
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gassy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Lucas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70197516102840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70197516102840
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ requirement: &70197516101880 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70197516101880
36
+ description: This gem will gather weekly gas prices from the U.S. Energy Information
37
+ Information site
38
+ email:
39
+ - matt.k.lucas@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .travis.yml
46
+ - Gemfile
47
+ - README.rdoc
48
+ - Rakefile
49
+ - gassy.gemspec
50
+ - lib/.DS_Store
51
+ - lib/gassy.rb
52
+ - lib/gassy/gassy_constants.rb
53
+ - lib/gassy/gassy_utils.rb
54
+ - lib/gassy/version.rb
55
+ - spec/gassy_data_spec.rb
56
+ - spec/gassy_xml_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/bballer320xu/Gassy
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.15
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Gas Price Retrieval Gem
82
+ test_files:
83
+ - spec/gassy_data_spec.rb
84
+ - spec/gassy_xml_spec.rb
85
+ - spec/spec_helper.rb