From 4dab2ad47a6dda4e46525568e05234785c564216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Br=C3=BCll?= Date: Tue, 30 Mar 2010 14:35:15 +0200 Subject: [PATCH] removed some code smells --- lib/allegro_graph/catalog.rb | 2 ++ lib/allegro_graph/federation.rb | 2 ++ lib/allegro_graph/proxy/geometric.rb | 1 + lib/allegro_graph/proxy/mapping.rb | 1 + lib/allegro_graph/proxy/query.rb | 6 ++++-- lib/allegro_graph/proxy/statements.rb | 2 ++ lib/allegro_graph/repository.rb | 10 ++++++++-- lib/allegro_graph/session.rb | 4 +++- lib/allegro_graph/transport.rb | 13 ++++++------- 9 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/allegro_graph/catalog.rb b/lib/allegro_graph/catalog.rb index 6cd8e5b..4fffc42 100644 --- a/lib/allegro_graph/catalog.rb +++ b/lib/allegro_graph/catalog.rb @@ -2,6 +2,8 @@ module AllegroGraph + # The Catalog class wrap the corresponding resource on the AllegroGraph server. A catalog can hold + # many repositories. class Catalog attr_reader :server diff --git a/lib/allegro_graph/federation.rb b/lib/allegro_graph/federation.rb index 1ba1a95..359756d 100644 --- a/lib/allegro_graph/federation.rb +++ b/lib/allegro_graph/federation.rb @@ -6,6 +6,8 @@ module AllegroGraph + # The Federation class wrap the corresponding resource on the AllegroGraph server. A federation is a collection + # of many repositories that acts like a single one. Only read access is allowed. class Federation attr_reader :server diff --git a/lib/allegro_graph/proxy/geometric.rb b/lib/allegro_graph/proxy/geometric.rb index c6b220d..e4ed80b 100644 --- a/lib/allegro_graph/proxy/geometric.rb +++ b/lib/allegro_graph/proxy/geometric.rb @@ -4,6 +4,7 @@ module AllegroGraph module Proxy + # The Geometric class acts as proxy to the geo-functions of the AllegroGraph server. class Geometric attr_reader :resource diff --git a/lib/allegro_graph/proxy/mapping.rb b/lib/allegro_graph/proxy/mapping.rb index e1670ab..10062c6 100644 --- a/lib/allegro_graph/proxy/mapping.rb +++ b/lib/allegro_graph/proxy/mapping.rb @@ -3,6 +3,7 @@ module AllegroGraph module Proxy + # The Mapping class acts as proxy to the data type mapping functions of the AllegroGraph server. class Mapping attr_reader :resource diff --git a/lib/allegro_graph/proxy/query.rb b/lib/allegro_graph/proxy/query.rb index 6eefc97..442f134 100644 --- a/lib/allegro_graph/proxy/query.rb +++ b/lib/allegro_graph/proxy/query.rb @@ -3,6 +3,7 @@ module AllegroGraph module Proxy + # The Query class acts as proxy that bypasses SparQL and Prolog queries to the AllegroGraph server. class Query LANGUAGES = [ :sparql, :prolog ].freeze unless defined?(LANGUAGES) @@ -21,8 +22,9 @@ def path end def language=(value) - raise NotImplementedError, "query langauge [#{value}] is not implemented" unless LANGUAGES.include?(value.to_sym) - @language = value.to_sym + value = value.to_sym + raise NotImplementedError, "query langauge [#{value}] is not implemented" unless LANGUAGES.include?(value) + @language = value end def perform(query) diff --git a/lib/allegro_graph/proxy/statements.rb b/lib/allegro_graph/proxy/statements.rb index 2f6d1ae..58643ec 100644 --- a/lib/allegro_graph/proxy/statements.rb +++ b/lib/allegro_graph/proxy/statements.rb @@ -4,6 +4,8 @@ module AllegroGraph module Proxy + # The Statement class acts as proxy to functions that add, remove or find statements + # in the AllegroGraph data store. class Statements attr_reader :resource diff --git a/lib/allegro_graph/repository.rb b/lib/allegro_graph/repository.rb index 558f434..1db8fb8 100644 --- a/lib/allegro_graph/repository.rb +++ b/lib/allegro_graph/repository.rb @@ -7,6 +7,8 @@ module AllegroGraph + # The Repository class wrap the corresponding resource on the AllegroGraph server. A repository acts as a scope for + # statements. Simple management methods are provided. class Repository attr_reader :server @@ -74,14 +76,18 @@ def size end def transaction(&block) - session = Session.create self + self.class.transaction self, &block + end + + def self.transaction(repository, &block) + session = Session.create repository begin session.instance_eval &block rescue Object => error session.rollback raise error end - session.commit + session.commit end end diff --git a/lib/allegro_graph/session.rb b/lib/allegro_graph/session.rb index 62ca711..f3765ba 100644 --- a/lib/allegro_graph/session.rb +++ b/lib/allegro_graph/session.rb @@ -6,6 +6,7 @@ module AllegroGraph + # The Session class wrap the corresponding resource on the AllegroGraph server. class Session attr_reader :url @@ -50,7 +51,8 @@ def self.create(repository) url = repository.request :post, repository.path + "/session", :expected_status_code => 200 url.sub! /^"/, "" url.sub! /"$/, "" - new :url => url, :username => repository.server.username, :password => repository.server.password + server = repository.server + new :url => url, :username => server.username, :password => server.password end private diff --git a/lib/allegro_graph/transport.rb b/lib/allegro_graph/transport.rb index 5a23cc6..4efb081 100644 --- a/lib/allegro_graph/transport.rb +++ b/lib/allegro_graph/transport.rb @@ -62,11 +62,8 @@ def serialize_parameters def quote_parameters @quoted_parameters = { } @parameters.each do |key, value| - if value.is_a?(Array) - @quoted_parameters[ CGI.escape("#{key}") ] = value.map{ |element| CGI.escape element } - else - @quoted_parameters[ CGI.escape("#{key}") ] = CGI.escape value - end + quoted_key = CGI.escape(key.to_s) + @quoted_parameters[quoted_key] = value.is_a?(Array) ? value.map{ |element| CGI.escape element } : CGI.escape(value) end end @@ -93,6 +90,7 @@ def self.request(http_method, url, options = { }) end + # Extended transport layer for http transfers. Basic authorization and JSON transfers are supported. class ExtendedTransport < Transport # The UnexpectedStatusCodeError is raised if the :expected_status_code option is given to @@ -159,9 +157,10 @@ def check_status_code end def parse_response - @response = @response.body.nil? ? nil : JSON.parse(@response.body) + body = @response.body + @response = body.nil? ? nil : JSON.parse(body) rescue JSON::ParserError - @response = @response.body.to_s + @response = body.to_s end end