Module: Mongo::Support
- Extended by:
- Support
- Includes:
- Conversions
- Included in:
- Support
- Defined in:
- lib/mongo/util/support.rb
Constant Summary collapse
- SECONDARY_OK_COMMANDS =
Commands that may be sent to replica-set secondaries, depending on read preference and tags. All other commands are always run on the primary.
[ 'group', 'aggregate', 'collstats', 'dbstats', 'count', 'distinct', 'geonear', 'geosearch', 'geowalk', 'mapreduce', 'replsetgetstatus', 'ismaster', ]
Constants included from Conversions
Conversions::ASCENDING_CONVERSION, Conversions::DESCENDING_CONVERSION
Instance Method Summary collapse
-
#auth_key(username, password, nonce) ⇒ String
Generate an MD5 for authentication.
- #format_order_clause(order) ⇒ Object
-
#hash_password(username, plaintext) ⇒ String
Return a hashed password for auth.
- #is_i?(value) ⇒ Boolean
- #normalize_seeds(seeds) ⇒ Object
-
#ok?(doc) ⇒ Boolean
Determine if a database command has succeeded by checking the document response.
- #secondary_ok?(selector) ⇒ Boolean
- #validate_db_name(db_name) ⇒ Object
Methods included from Conversions
#array_as_sort_parameters, #hash_as_sort_parameters, #sort_value, #string_as_sort_parameters
Instance Method Details
#auth_key(username, password, nonce) ⇒ String
Generate an MD5 for authentication.
47 48 49 |
# File 'lib/mongo/util/support.rb', line 47 def auth_key(username, password, nonce) Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}") end |
#format_order_clause(order) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mongo/util/support.rb', line 88 def format_order_clause(order) case order when Hash, BSON::OrderedHash then hash_as_sort_parameters(order) when String, Symbol then string_as_sort_parameters(order) when Array then array_as_sort_parameters(order) else raise InvalidSortValueError, "Illegal sort clause, '#{order.class.name}'; must be of the form " + "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]" end end |
#hash_password(username, plaintext) ⇒ String
Return a hashed password for auth.
57 58 59 |
# File 'lib/mongo/util/support.rb', line 57 def hash_password(username, plaintext) Digest::MD5.hexdigest("#{username}:mongo:#{plaintext}") end |
#is_i?(value) ⇒ Boolean
113 114 115 |
# File 'lib/mongo/util/support.rb', line 113 def is_i?(value) return !!(value =~ /^\d+$/) end |
#normalize_seeds(seeds) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mongo/util/support.rb', line 99 def normalize_seeds(seeds) pairs = Array(seeds) pairs = [ seeds ] if pairs.last.is_a?(Fixnum) pairs = pairs.collect do |hostport| if hostport.is_a?(String) host, port = hostport.split(':') [ host, port && port.to_i || MongoClient::DEFAULT_PORT ] else hostport end end pairs.length > 1 ? pairs : pairs.first end |
#ok?(doc) ⇒ Boolean
Determine if a database command has succeeded by checking the document response.
123 124 125 |
# File 'lib/mongo/util/support.rb', line 123 def ok?(doc) doc['ok'] == 1.0 || doc['ok'] == true end |
#secondary_ok?(selector) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mongo/util/support.rb', line 75 def secondary_ok?(selector) command = selector.keys.first.to_s.downcase if command == 'mapreduce' out = selector.select { |k, v| k.to_s.downcase == 'out' }.first.last # mongo looks at the first key in the out object, and doesn't # look at the value out.is_a?(Hash) && out.keys.first.to_s.downcase == 'inline' ? true : false else SECONDARY_OK_COMMANDS.member?(command) end end |
#validate_db_name(db_name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongo/util/support.rb', line 61 def validate_db_name(db_name) unless [String, Symbol].include?(db_name.class) raise TypeError, "db_name must be a string or symbol" end [" ", ".", "$", "/", "\\"].each do |invalid_char| if db_name.include? invalid_char raise Mongo::InvalidNSName, "database names cannot contain the character '#{invalid_char}'" end end raise Mongo::InvalidNSName, "database name cannot be the empty string" if db_name.empty? db_name end |