Permalink
Browse files

some refactorings

  • Loading branch information...
1 parent b245392 commit 337bbc2d7d442ca8b6f9fe37b0cc23697b1d8bb6 @phifty committed Oct 10, 2011
Showing with 26 additions and 26 deletions.
  1. +2 −2 lib/allegro_graph/repository.rb
  2. +24 −24 lib/allegro_graph/session.rb
View
4 lib/allegro_graph/repository.rb
@@ -65,11 +65,11 @@ def size
response.to_i
end
- def transaction(options={}, &block)
+ def transaction(options = { }, &block)
self.class.transaction self, options, &block
end
- def self.transaction(repository, options={}, &block)
+ def self.transaction(repository, options = { }, &block)
session = Session.create repository, options
begin
session.instance_eval &block
View
48 lib/allegro_graph/session.rb
@@ -42,30 +42,16 @@ def size
response.to_i
end
- class << self
-
- def create(repository_or_server, options={})
- response = repository_or_server.request_http :post, repository_or_server.path + "/session", { :expected_status_code => 200, :parameters => parse_session_options(options, repository_or_server.path.empty?) }
- url = response.sub(/^"/, "").sub(/"$/, "")
- server = repository_or_server.server
- new :url => url, :username => server.username, :password => server.password
- end
-
- def parse_session_options(opts, store_required)
- opts ||= {}
- options = {}
- if store_required
- raise ArgumentError.new('The parameter store is required') unless opts.has_key?(:store)
- options[:store] = opts[:store].is_a?(Array) ? "<#{opts[:store].join('>+<')}>" : "<#{opts[:store].to_s}>"
- end
- # See http://www.franz.com/agraph/support/documentation/current/http-protocol.html#sessions
- [:autoCommit, :lifetime, :loadInitFile, :script].each do |p|
- options[p] = opts[p] if opts.has_key?(p)
- end
- options
- end
- private :parse_session_options
-
+ def self.create(repository_or_server, options = { })
+ path = repository_or_server.path
+ raise ArgumentError, "The :store parameter is missing!" if path.empty? && !options.has_key?(:store)
+
+ response = repository_or_server.request_http :post,
+ path + "/session",
+ { :expected_status_code => 200, :parameters => build_parameters(options) }
+ url = response.sub(/^"/, "").sub(/"$/, "")
+ server = repository_or_server.server
+ new :url => url, :username => server.username, :password => server.password
end
private
@@ -74,6 +60,20 @@ def credentials
{ :auth_type => :basic, :username => @username, :password => @password }
end
+ def self.build_parameters(options)
+ parameters = { }
+
+ store = options[:store]
+ parameters[:store] = store.is_a?(Array) ? "<#{store.join('>+<')}>" : "<#{store}>" if store
+
+ # See http://www.franz.com/agraph/support/documentation/current/http-protocol.html#sessions
+ [ :autoCommit, :lifetime, :loadInitFile, :script ].each do |parameters_name|
+ parameters[parameters_name] = options[parameters_name] if options.has_key?(parameters_name)
+ end
+
+ parameters
+ end
+
end
end

0 comments on commit 337bbc2

Please sign in to comment.