Skip to content

Commit 51310da

Browse files
committed
Making some improvements
1 parent 721dc1e commit 51310da

16 files changed

+247
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ tmp
2020
*.o
2121
*.a
2222
mkmf.log
23+
gemfiles/*.lock

.rubocop.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Metrics/LineLength:
2+
Enabled: false
3+
4+
Style/EachWithObject:
5+
Enabled: false
6+
7+
Style/Encoding:
8+
EnforcedStyle: when_needed
9+
10+
Style/FormatString:
11+
Enabled: false
12+
13+
Style/HashSyntax:
14+
EnforcedStyle: hash_rockets
15+
16+
Style/SignalException:
17+
EnforcedStyle: only_raise
18+
19+
Style/StringLiterals:
20+
EnforcedStyle: double_quotes

.travis.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
matrix:
2+
include:
3+
- rvm: 1.8.7
4+
gemfile: gemfiles/Gemfile.1.8.7
5+
- rvm: 1.9.2
6+
gemfile: gemfiles/Gemfile.1.9.2+
7+
env: COVERAGE=true
8+
- rvm: 1.9.3
9+
gemfile: gemfiles/Gemfile.1.9.2+
10+
env: COVERAGE=true
11+
- rvm: 2.0.0
12+
gemfile: gemfiles/Gemfile.1.9.2+
13+
env: COVERAGE=true
14+
- rvm: 2.1.1
15+
gemfile: gemfiles/Gemfile.1.9.2+
16+
env: COVERAGE=true
17+
- rvm: ree
18+
gemfile: gemfiles/Gemfile.1.8.7
19+
- rvm: jruby
20+
gemfile: gemfiles/Gemfile.1.9.2+
21+
env: COVERAGE=true

CONTRIBUTING.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ New contributors are always welcome, when it doubt please ask questions. We stri
55
### Coding
66

77
* Pick a task:
8-
* Offer feedback on open [pull requests](https://github.com/zertico/fog-xml/pulls).
9-
* Review open [issues](https://github.com/zertico/fog-xml/issues) for things to help on.
10-
* [Create an issue](https://github.com/zertico/fog-xml/issues/new) to start a discussion on additions or features.
8+
* Offer feedback on open [pull requests](https://github.com/fog/fog-xml/pulls).
9+
* Review open [issues](https://github.com/fog/fog-xml/issues) for things to help on.
10+
* [Create an issue](https://github.com/fog/fog-xml/issues/new) to start a discussion on additions or features.
1111
* Fork the project, add your changes and tests to cover them in a topic branch.
12-
* Commit your changes and rebase against `zertico/fog-xml` to ensure everything is up to date.
13-
* [Submit a pull request](https://github.com/zertico/fog-xml/compare/)
12+
* Commit your changes and rebase against `fog/fog-xml` to ensure everything is up to date.
13+
* [Submit a pull request](https://github.com/fog/fog-xml/compare/)
1414

1515
### Non-Coding
1616

17-
* Offer feedback on open [issues](https://github.com/zertico/fog-xml/issues).
17+
* Offer feedback on open [issues](https://github.com/fog/fog-xml/issues).
1818
* Organize or volunteer at events.

CONTRIBUTORS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
* Paulo Henrique Lopes Ribeiro <[email protected]>
1+
* Michael Hale <[email protected]>
2+
* Paul Thornthwaite <[email protected]>
3+
* Paulo Henrique Lopes Ribeiro <[email protected]>
4+
* Wesley Beary <[email protected]>

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22

33
# Specify your gem's dependencies in fog-xml.gemspec
44
gemspec

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
require "bundler/gem_tasks"
2+
require "rake/testtask"
23

4+
Rake::TestTask.new do |t|
5+
t.libs.push %w(spec)
6+
t.test_files = FileList["spec/**/*_spec.rb"]
7+
t.verbose = true
8+
end
9+
10+
desc "Default Task"
11+
task :default => [:test]

fog-xml.gemspec

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
2+
lib = File.expand_path("../lib", __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'fog/xml/version'
4+
require "fog/xml/version"
55

66
Gem::Specification.new do |spec|
77
spec.name = "fog-xml"
88
spec.version = Fog::Xml::VERSION
9-
spec.authors = %q(Paulo Henrique Lopes Ribeiro)
10-
spec.email = %q(plribeiro3000@gmail.com)
11-
spec.summary = %q{XML parsing for fog providers}
12-
spec.description = %q{Extraction of the XML parsing tools shared between a
13-
number of providers in the 'fog' gem}
14-
spec.homepage = 'https://github.com/zertico/fog-xml'
15-
spec.license = 'MIT'
9+
spec.authors = ["Wesley Beary (geemus)", "Paul Thornthwaite (tokengeek)", "The fog team"]
10+
spec.email = ["geemus@gmail.com", "[email protected]"]
11+
spec.summary = "XML parsing for fog providers"
12+
spec.description = "Extraction of the XML parsing tools shared between a
13+
number of providers in the 'fog' gem"
14+
spec.homepage = "https://github.com/fog/fog-xml"
15+
spec.license = "MIT"
1616

1717
spec.files = `git ls-files -z`.split("\x0")
18-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
19+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
2020
spec.require_paths = %w(lib)
2121

22-
spec.add_dependency 'fog-core'
23-
spec.add_dependency 'nokogiri', '~> 1.5', '>= 1.5.11'
24-
spec.add_development_dependency 'rake'
25-
spec.add_development_dependency 'rspec'
22+
spec.add_dependency "fog-core"
23+
spec.add_dependency "nokogiri", "~> 1.5", ">= 1.5.11"
24+
spec.add_development_dependency "rake"
25+
spec.add_development_dependency "minitest"
26+
spec.add_development_dependency "turn"
27+
spec.add_development_dependency "pry"
28+
spec.add_development_dependency "rubocop"
29+
spec.add_development_dependency "coveralls" if RUBY_VERSION.to_f >= 1.9
2630
end

gemfiles/Gemfile.1.8.7

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source :rubygems
2+
3+
gem 'mime-types', '< 2.0'
4+
gem 'nokogiri', '< 1.6'
5+
gem 'rest-client', '~> 1.6.8'
6+
gem 'fog-core', :github => 'fog/fog-core'
7+
8+
gemspec :path => '../'

gemfiles/Gemfile.1.9.2+

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source :rubygems
2+
3+
gem 'activesupport', '>= 3.0', '< 4'
4+
gem 'mime-types', '< 2.0'
5+
gem 'fog-core', :github => 'fog/fog-core'
6+
7+
gemspec :path => '../'

lib/fog/to_hash_document.rb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module Fog
2+
class ToHashDocument < Nokogiri::XML::SAX::Document
3+
def initialize
4+
@stack = []
5+
end
6+
7+
def characters(string)
8+
@value ||= ""
9+
@value << string.strip
10+
end
11+
12+
def end_element(name)
13+
last = @stack.pop
14+
if last.empty? && @value.empty?
15+
@stack.last[name.to_sym] = ""
16+
elsif last == { :i_nil => "true" }
17+
@stack.last[name.to_sym] = nil
18+
elsif !@value.empty?
19+
@stack.last[name.to_sym] = @value
20+
end
21+
@value = ""
22+
end
23+
24+
def body
25+
@stack.first
26+
end
27+
28+
def response
29+
body
30+
end
31+
32+
def start_element(name, attributes = [])
33+
@value = ""
34+
parsed_attributes = {}
35+
until attributes.empty?
36+
if attributes.first.is_a?(Array)
37+
key, value = attributes.shift
38+
else
39+
key, value = attributes.shift, attributes.shift
40+
end
41+
parsed_attributes[key.gsub(":", "_").to_sym] = value
42+
end
43+
if @stack.last.is_a?(Array)
44+
@stack.last << { name.to_sym => parsed_attributes }
45+
else
46+
data = if @stack.empty?
47+
@stack.push(parsed_attributes)
48+
parsed_attributes
49+
elsif @stack.last[name.to_sym]
50+
unless @stack.last[name.to_sym].is_a?(Array)
51+
@stack.last[name.to_sym] = [@stack.last[name.to_sym]]
52+
end
53+
@stack.last[name.to_sym] << parsed_attributes
54+
@stack.last[name.to_sym].last
55+
else
56+
@stack.last[name.to_sym] = {}
57+
@stack.last[name.to_sym].merge!(parsed_attributes)
58+
@stack.last[name.to_sym]
59+
end
60+
@stack.push(data)
61+
end
62+
end
63+
end
64+
end

lib/fog/xml.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
require 'fog/xml/version'
2-
require 'nokogiri'
1+
require "fog/xml/version"
2+
require "nokogiri"
33

44
module Fog
5-
module Xml
6-
autoload :SAXParserConnection, 'fog/xml/sax_parser_connection'
7-
autoload :Connection, 'fog/xml/connection'
5+
autoload :ToHashDocument, "fog/to_hash_document"
6+
7+
module XML
8+
autoload :SAXParserConnection, "fog/xml/sax_parser_connection"
9+
autoload :Connection, "fog/xml/connection"
810
end
911
end

lib/fog/xml/connection.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Fog
2-
module Xml
2+
module XML
33
class Connection < SAXParserConnection
4-
def request(params, &block)
5-
if (parser = params.delete(:parser))
4+
def request(params, &_block)
5+
parser = params.delete(:parser)
6+
if parser
67
super(parser, params)
78
else
89
original_request(params)
910
end
1011
end
1112
end
1213
end
13-
end
14+
end

lib/fog/xml/sax_parser_connection.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
require 'fog/core/connection'
1+
require "fog/core/connection"
22

33
module Fog
4-
module Xml
4+
module XML
55
class SAXParserConnection < ::Fog::Core::Connection
66
# Makes a request using the connection using Excon
77
#
88
# @param [Hash] params
99
# @option params [String] :body text to be sent over a socket
1010
# @option params [Hash<Symbol, String>] :headers The default headers to supply in a request
11-
# @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
12-
# @option params [String] :path appears after 'scheme://host:port/'
11+
# @option params [String] :host The destination host"s reachable DNS name or IP, in the form of a String
12+
# @option params [String] :path appears after "scheme://host:port/"
1313
# @option params [Fixnum] :port The port on which to connect, to the destination host
14-
# @option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
15-
# @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
14+
# @option params [Hash] :query appended to the "scheme://host:port/path/" in the form of "?key=value"
15+
# @option params [String] :scheme The protocol; "https" causes OpenSSL to be used
1616
# @option params [Proc] :response_block
1717
# @option params [Nokogiri::XML::SAX::Document] :parser
1818
#
@@ -28,14 +28,14 @@ def request(parser, params)
2828
# Prepare the SAX parser
2929
data_stream = Nokogiri::XML::SAX::PushParser.new(parser)
3030
response_string = ""
31-
params[:response_block] = lambda do |chunk, remaining, total|
32-
response_string << chunk if ENV['DEBUG_RESPONSE']
31+
params[:response_block] = lambda do |chunk, _remaining, _total|
32+
response_string << chunk if ENV["DEBUG_RESPONSE"]
3333
data_stream << chunk
3434
end
3535

3636
# Make request which read chunks into parser
3737
response = @excon.request(params)
38-
Fog::Logger.debug "\n#{response_string}" if ENV['DEBUG_RESPONSE']
38+
Fog::Logger.debug "\n#{response_string}" if ENV["DEBUG_RESPONSE"]
3939

4040
# Cease parsing and override response.body with parsed data
4141
data_stream.finish
@@ -44,4 +44,4 @@ def request(parser, params)
4444
end
4545
end
4646
end
47-
end
47+
end

spec/fog/xml/connection_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "minitest_helper"
2+
3+
describe Fog::XML::Connection do
4+
before do
5+
@connection = Fog::XML::Connection.new("http://localhost")
6+
end
7+
8+
after do
9+
Excon.stubs.clear
10+
end
11+
12+
it "responds to #request" do
13+
assert_respond_to @connection, :request
14+
end
15+
16+
describe "when request is passed a parser" do
17+
it "returns the body after parsing" do
18+
@parser = Fog::ToHashDocument.new
19+
Excon.stub({}, { :status => 200, :body => "<xml></xml>" })
20+
response = @connection.request(:parser => @parser, :mock => true)
21+
assert_equal({ :xml => "" }, response.body)
22+
end
23+
end
24+
25+
describe "when request excludes a parser" do
26+
it "returns the response body without change" do
27+
Excon.stub({}, { :status => 200, :body => "<xml></xml>" })
28+
response = @connection.request(:mock => true)
29+
assert_equal("<xml></xml>", response.body)
30+
end
31+
end
32+
end

spec/minitest_helper.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "minitest/spec"
2+
require "minitest/autorun"
3+
require "turn"
4+
require "excon"
5+
require "fog/core"
6+
7+
Turn.config do |c|
8+
# use one of output formats:
9+
# :outline - turn's original case/test outline mode [default]
10+
# :progress - indicates progress with progress bar
11+
# :dotted - test/unit's traditional dot-progress mode
12+
# :pretty - new pretty reporter
13+
# :marshal - dump output as YAML (normal run mode only)
14+
# :cue - interactive testing
15+
# c.format = :outline
16+
# turn on invoke/execute tracing, enable full backtrace
17+
c.trace = 20
18+
# use humanized test names (works only with :outline format)
19+
c.natural = true
20+
end
21+
22+
if ENV["COVERAGE"]
23+
require "coveralls"
24+
require "simplecov"
25+
26+
SimpleCov.start do
27+
add_filter "/spec/"
28+
end
29+
end
30+
31+
require File.join(File.dirname(__FILE__), "../lib/fog/xml")
32+
33+
Coveralls.wear! if ENV["COVERAGE"]

0 commit comments

Comments
 (0)