Module: Humongous::Helpers::SinatraHelpers
- Defined in:
- lib/humongous/helpers.rb
Instance Method Summary collapse
- #autanticate! ⇒ Object
- #connection(params) ⇒ Object
- #default_opts ⇒ Object
- #doc_to_bson(doc, converter) ⇒ Object
- #from_bson(options) ⇒ Object
- #get_uri(params = {}) ⇒ Object
- #json_converter(params_json) ⇒ Object
- #opts_to_connect(params = {}) ⇒ Object
- #to_bson(options) ⇒ Object
Instance Method Details
#autanticate! ⇒ Object
11 12 13 14 |
# File 'lib/humongous/helpers.rb', line 11 def autanticate! return if params[:auth].blank? || params[:auth][:db].blank? @connection.with( database: params[:auth][:db], user: params[:auth][:username], password: params[:auth][:password]) end |
#connection(params) ⇒ Object
6 7 8 9 |
# File 'lib/humongous/helpers.rb', line 6 def connection(params) opts = opts_to_connect(params) session[:connection] = Mongo::Client.new(get_uri(opts)) end |
#default_opts ⇒ Object
43 44 45 |
# File 'lib/humongous/helpers.rb', line 43 def default_opts { :skip => 0, :limit => 10 } end |
#doc_to_bson(doc, converter) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/humongous/helpers.rb', line 62 def doc_to_bson( doc, converter ) doc = send(converter, doc) doc.each do |k,v| case v when Hash send(converter, v ) end end doc end |
#from_bson(options) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/humongous/helpers.rb', line 73 def from_bson( ) ids = .select{ |k,v| v.is_a? BSON::ObjectId } ids.each do | k, v | [k] = v.to_s end end |
#get_uri(params = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/humongous/helpers.rb', line 28 def get_uri(params = {}) @options = { :url => "127.0.0.1", :port => "27017", :username => "", :password => "" } @options = @options.merge(params) if @options[:username].present? && @options[:password].present? "mongodb://#{@options[:username]}:#{@options[:password]}@#{@options[:url]}:#{@options[:port]}" else "mongodb://#{@options[:url]}:#{@options[:port]}" end end |
#json_converter(params_json) ⇒ Object
81 82 83 |
# File 'lib/humongous/helpers.rb', line 81 def json_converter( params_json ) params_json.gsub(/(\w+):/, '"\1":') end |
#opts_to_connect(params = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/humongous/helpers.rb', line 16 def opts_to_connect(params = {}) return @options if @options && @options[:freeze] @options = { :url => "127.0.0.1", :port => "27017", :username => "", :password => "" } return @options if params.blank? @options.merge!(params) end |
#to_bson(options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/humongous/helpers.rb', line 47 def to_bson( ) ids = .keys.grep /_id$/ ids.each do |id| begin [id] = BSON::ObjectId.from_string([id]) rescue BSON::InvalidObjectId puts "found illegal ObjectId, skipping..." next rescue e puts e. end end end |