class RoadForest::HTTP::GraphTransfer

Attributes

trace[RW]
type_handling[W]
user_agent[RW]

Public Class Methods

new(user_agent) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 10
def initialize(user_agent)
  @trace = false
  @user_agent = user_agent
  @type_preferences = Hash.new{|h,k| k.nil? ? "*/*" : h[nil]}
end

Public Instance Methods

best_type_for(url) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 70
def best_type_for(url)
  return @type_preferences[url]
end
build_response(url, response) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 80
def build_response(url, response)
  parser = type_handling.choose_parser(response.headers["Content-Type"])
  graph = parser.to_graph(url, response.body_string)

  trace_graph("IN", graph)

  response = GraphResponse.new(url, response)
  response.graph = graph
  return response
rescue ContentHandling::UnrecognizedType
  return UnparseableResponse.new(url, response)
end
get(url) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 20
def get(url)
  make_request("GET", url)
end
make_request(method, url, graph, retry_limit=5) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 28
def make_request(method, url, graph, retry_limit=5)
  headers = {"Accept" => type_handling.parsers.types.accept_header}
  body = nil

  trace_graph("OUT", graph)

  if(%w{POST PUT PATCH}.include? method.upcase)
    content_type = best_type_for(url)
    renderer = type_handling.choose_renderer(content_type)
    headers["Content-Type"] = renderer.content_type_header
    body = renderer.from_graph(url, graph)
  end

  response = user_agent.make_request(method, url, headers, body, retry_limit)

  case response.status
  when 415 #Type not accepted
    record_accept_header(url, response.headers["Accept"])
    raise Retryable
  end

  build_response(url, response)
rescue Retryable
  raise unless (retry_limit -= 1) > 0
  retry
end
post(url, graph) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 24
def post(url, graph)
  make_request("POST", url, graph)
end
put(url, graph) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 16
def put(url, graph)
  make_request("PUT", url, graph)
end
record_accept_header(url, types) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 74
def record_accept_header(url, types)
  return if types.nil? or types.empty?
  @type_preferences[nil] = types
  @type_preferences[url] = types
end
trace_graph(tag, graph) click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 55
def trace_graph(tag, graph)
  return unless @trace
  require 'rdf/turtle'
  @trace = $stderr unless @trace.respond_to?(:puts)
  @trace.puts "<#{tag}"
  if graph.respond_to?(:dump)
    @trace.puts graph.dump(:ntriples, :standard_prefixes => true, :prefixes => { "af" => "http://judsonlester.info/affordance#"})
  end
  @trace.puts "#{tag}>"
end
type_handling() click to toggle source
# File lib/roadforest/http/graph-transfer.rb, line 66
def type_handling
  @type_handling || ContentHandling.rdf_engine
end