Skip to content

Commit

Permalink
removed some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Brüll committed Mar 30, 2010
1 parent bf06b93 commit 4dab2ad
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/allegro_graph/catalog.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/allegro_graph/federation.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/allegro_graph/proxy/geometric.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/allegro_graph/proxy/mapping.rb
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/allegro_graph/proxy/query.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/allegro_graph/proxy/statements.rb
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/allegro_graph/repository.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/allegro_graph/session.rb
Expand Up @@ -6,6 +6,7 @@

module AllegroGraph

# The Session class wrap the corresponding resource on the AllegroGraph server.
class Session

attr_reader :url
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions lib/allegro_graph/transport.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4dab2ad

Please sign in to comment.