Skip to content

Commit

Permalink
Dynamically load json library (add compatibility with appengine-jruby)
Browse files Browse the repository at this point in the history
  • Loading branch information
wynst committed Feb 21, 2010
1 parent 25d01a0 commit c18f024
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/sinbook.rb
Expand Up @@ -11,7 +11,6 @@ class FacebookError < StandardError

module Sinatra
require 'digest/md5'
require 'yajl'

class FacebookObject
def initialize app
Expand Down Expand Up @@ -200,10 +199,22 @@ def request method, opts = {}

"#{k}=" + case v
when Hash
Yajl::Encoder.encode(v)
if Object.const_defined?("Yajl") && Yajl.const_defined?("Encoder")
Yajl::Encoder.encode(v)
elsif Object.const_defined("JSON")
JSON.generate(v)
else
throw "you need to require either 'yajl' or 'json' for sinbook to work"
end
when Array
if k == :tags
Yajl::Encoder.encode(v)
if Object.const_defined?("Yajl") && Yajl.const_defined?("Encoder")
Yajl::Encoder.encode(v)
elsif Object.const_defined("JSON")
JSON.generate(v)
else
throw "you need to require either 'yajl' or 'json' for sinbook to work"
end
else
v.join(',')
end
Expand Down
3 changes: 1 addition & 2 deletions sinbook.gemspec
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'sinbook'
s.version = '0.1.9'
s.version = '0.1.9.aej'
s.date = '2010-02-04'
s.summary = 'simple sinatra facebook extension in 300 lines of ruby'
s.description = 'A full-featured facebook extension for the sinatra webapp framework'
Expand All @@ -10,7 +10,6 @@ spec = Gem::Specification.new do |s|
s.authors = ["Aman Gupta"]
s.email = "aman@tmm1.net"

s.add_dependency('yajl-ruby')
s.has_rdoc = false

# ruby -rpp -e' pp `git ls-files | grep -v examples`.split("\n") '
Expand Down

2 comments on commit c18f024

@romanbsd
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to do inside the class:
begin
require 'yajl'
def self.encode(v)
Yajl::Encoder.encode(v)}
end
rescue LoadError
require 'json'
def self.encode(v)
JSON.generate(v)
end
end

this way the code will be cleaner, IMHO.

@wynst
Copy link
Owner Author

@wynst wynst commented on c18f024 Feb 25, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a real quick hack as an excuse :)
much cleaner I'd say :)

Please sign in to comment.