Skip to content

Commit

Permalink
Replaces libxml with Nokogiri (#375)
Browse files Browse the repository at this point in the history
* Replaces libxml with Nokogiri

Nokogiri has much wider adoption with Ruby users than libxml.
Some developers were having trouble with libxml installation.
libxml doesn't work on JRuby, but Nokogiri will.
The API doesn't change.

Bonus
The unused `indent` argument to `TwiML` has been removed.

* Fixes up support for JRuby.

Adds jruby to Travis CI config.

* Specify jruby that exists on Travis

* Whitespace removal when code was generated
  • Loading branch information
philnash authored and jmctwilio committed Nov 17, 2017
1 parent ffebd86 commit 4144755
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rvm:
- 2.2.0
- 2.1
- 2.0.0
- jruby-9.1.9.0

matrix:
fast_finish: true
Expand Down
2 changes: 1 addition & 1 deletion lib/twilio-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'net/http'
require 'net/https'
require 'libxml'
require 'nokogiri'
require 'cgi'
require 'openssl'
require 'base64'
Expand Down
20 changes: 10 additions & 10 deletions lib/twilio-ruby/twiml/twiml.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'libxml'
require 'nokogiri'

##
# This code was generated by
Expand All @@ -11,11 +11,10 @@ module TwiML
class TwiMLError < StandardError; end

class TwiML
attr_accessor :name, :indent
attr_accessor :name

def initialize(indent: false, **keyword_args)
def initialize(**keyword_args)
@name = self.class.name.split('::').last
@indent = indent
@value = nil
@verbs = []
@attrs = {}
Expand All @@ -35,17 +34,18 @@ def self.to_lower_camel_case(symbol)
end

def to_s(xml_declaration = true)
xml = self.xml.to_s(indent: indent)

opts = { encoding: 'UTF-8', indent: 0 }
document = Nokogiri::XML::Document.new
xml = self.xml(document).to_xml(opts).gsub(/\n/, '')
return ('<?xml version="1.0" encoding="UTF-8"?>' + xml) if xml_declaration
xml
end

def xml
def xml(document)
# create XML element
value = (@value.is_a?(String) or @value == nil) ? @value : JSON.generate(@value)
elem = LibXML::XML::Node.new(@name, value)

elem = Nokogiri::XML::Node.new(@name, document)
elem.content = value
# set element attributes
keys = @attrs.keys.sort
keys.each do |key|
Expand All @@ -56,7 +56,7 @@ def xml
end

@verbs.each do |verb|
elem << verb.xml
elem << verb.xml(document)
end

elem
Expand Down
4 changes: 2 additions & 2 deletions twilio-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Gem::Specification.new do |spec|
spec.extra_rdoc_files = ['README.md', 'LICENSE.md']
spec.rdoc_options = ['--line-numbers', '--inline-source', '--title', 'twilio-ruby', '--main', 'README.md']

spec.add_dependency('libxml-ruby', '>= 2.0', '< 4.0')
spec.add_dependency('nokogiri', '>= 1.6', '< 2.0')
spec.add_dependency('jwt', '~> 1.5')
spec.add_dependency('faraday', '~>0.9')
spec.add_dependency('jruby-openssl') if RUBY_PLATFORM == 'java'
spec.add_dependency('jruby-openssl', '>= 0.9.6') if RUBY_PLATFORM == 'java'
# Workaround for RBX <= 2.2.1, should be fixed in next version
spec.add_dependency('rubysl') if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'

Expand Down

0 comments on commit 4144755

Please sign in to comment.