Module: Neo4j::Server::Resource
- Included in:
- CypherNode, CypherRelationship, CypherSession, CypherTransaction
- Defined in:
- lib/neo4j-server/resource.rb
Defined Under Namespace
Classes: ServerException
Instance Attribute Summary collapse
-
#resource_data ⇒ Object
readonly
Returns the value of attribute resource_data.
-
#resource_url(rel = nil) ⇒ Object
readonly
Returns the value of attribute resource_url.
Instance Method Summary collapse
- #convert_from_json_value(value) ⇒ Object
- #expect_response_code(response, expected_code, msg = "Error for request") ⇒ Object
- #handle_response_error(response, msg = "Error for request") ⇒ Object
- #init_resource_data(resource_data, resource_url) ⇒ Object
- #resource_headers ⇒ Object
- #resource_url_id(url = @resource_url) ⇒ Object
- #response_exception(response) ⇒ Object
- #wrap_resource(db, rel, resource_class, verb = :get, payload = {}, connection) ⇒ Object
Instance Attribute Details
#resource_data ⇒ Object (readonly)
Returns the value of attribute resource_data.
8 9 10 |
# File 'lib/neo4j-server/resource.rb', line 8 def resource_data @resource_data end |
#resource_url(rel = nil) ⇒ Object (readonly)
Returns the value of attribute resource_url.
8 9 10 |
# File 'lib/neo4j-server/resource.rb', line 8 def resource_url @resource_url end |
Instance Method Details
#convert_from_json_value(value) ⇒ Object
58 59 60 |
# File 'lib/neo4j-server/resource.rb', line 58 def convert_from_json_value(value) JSON.parse(value, :quirks_mode => true) end |
#expect_response_code(response, expected_code, msg = "Error for request") ⇒ Object
40 41 42 43 |
# File 'lib/neo4j-server/resource.rb', line 40 def expect_response_code(response, expected_code, msg="Error for request" ) handle_response_error(response, "Expected response code #{expected_code} #{msg}") unless response.status == expected_code response end |
#handle_response_error(response, msg = "Error for request") ⇒ Object
36 37 38 |
# File 'lib/neo4j-server/resource.rb', line 36 def handle_response_error(response, msg="Error for request" ) raise ServerException.new("#{msg} #{response.env && response.env[:url].to_s}, #{response.status}, #{response.status}") end |
#init_resource_data(resource_data, resource_url) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/neo4j-server/resource.rb', line 10 def init_resource_data(resource_data, resource_url) raise "Exception #{resource_data['exception']}" if resource_data['exception'] @resource_url = resource_url @resource_data = resource_data raise "expected @resource_data to be Hash got #{@resource_data.class}" unless @resource_data.respond_to?(:[]) self end |
#resource_headers ⇒ Object
50 51 52 |
# File 'lib/neo4j-server/resource.rb', line 50 def resource_headers {'Content-Type' => 'application/json', 'Accept' => 'application/json'} end |
#resource_url_id(url = @resource_url) ⇒ Object
54 55 56 |
# File 'lib/neo4j-server/resource.rb', line 54 def resource_url_id(url = @resource_url) url.match(/\/(\d+)$/)[1].to_i end |
#response_exception(response) ⇒ Object
45 46 47 48 |
# File 'lib/neo4j-server/resource.rb', line 45 def response_exception(response) return nil if response.body.nil? || response.body.empty? JSON.parse(response.body)['exception'] end |
#wrap_resource(db, rel, resource_class, verb = :get, payload = {}, connection) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/neo4j-server/resource.rb', line 19 def wrap_resource(db, rel, resource_class, verb=:get, payload={}, connection) url = resource_url(rel) response = case verb when :get then connection.get(url, payload) when :post then connection.post(url, payload) else raise "Illegal verb #{verb}" end response.status == 404 ? nil : resource_class.new(db, response, url, connection) end |