Skip to content

Commit

Permalink
Add support for different Ruby versions (including JRuby). Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Andritsch committed Dec 6, 2012
1 parent a7c5117 commit 095957e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ source 'http://rubygems.org'

gem 'rest-client'
gem 'rspec'

gem 'iconv' if RUBY_PLATFORM != "java" && RUBY_VERSION.match(/1\.8\.\d+/)

1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GEM
rspec-mocks (2.11.1)

PLATFORMS
java
ruby

DEPENDENCIES
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ URL for a card details page. Example:

MTGExtractor has been tested against the following Ruby versions:

- 1.9.3
- MRI 1.8.7+
- JRuby 1.6 with MRI 1.8.7
- JRuby 1.7 with MRI 1.9

**Note**: You will need Iconv for MRI 1.8.7

gem install iconv

### Rails

Expand Down
34 changes: 33 additions & 1 deletion lib/card_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ module MTGExtractor
class CardExtractor
require 'restclient'

if RUBY_PLATFORM == "java"
require 'java'
else
require 'iconv' if RUBY_VERSION.match(/1\.8\.\d+/)
end

attr_accessor :url

def initialize(url)
@url = url
end

def get_card_details
response = RestClient.get(@url).force_encoding("utf-8")
response = RestClient.get(@url)
response = convert_to_utf_8(response)

card_details = {}
card_details['gatherer_url'] = @url
card_details['multiverse_id'] = extract_multiverse_id(@url)
Expand Down Expand Up @@ -264,6 +272,30 @@ def extract_artist(html)
match ? match[1] : ""
end

private

# JRuby 1.7 (with MRI 1.9) or just MRI 1.9 can just use String#force_encoding
# JRuby 1.6 (with MRI 1.8.7) will use the Java method of conversion
# MRI (no JRuby) 1.8.7 needs Iconv
def convert_to_utf_8(response)
if RUBY_PLATFORM == "java"
if RUBY_VERSION.match(/1\.9\.\d+/)
response.force_encoding("utf-8")
else
bytes = response.to_java_bytes
converted_bytes = java.lang.String.new(bytes, "UTF-8").get_bytes("UTF-8")
String.from_java_bytes(converted_bytes)
end
else
if RUBY_VERSION.match(/1\.9\.\d+/)
response.force_encoding("utf-8")
else
::Iconv.conv('UTF-8//IGNORE', 'UTF-8', response + ' ')[0..-2]
end
end
end

end


end

0 comments on commit 095957e

Please sign in to comment.