Class: RoadForest::HTTP::GraphTransfer

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/http/graph-transfer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent) ⇒ GraphTransfer

Returns a new instance of GraphTransfer.



10
11
12
13
14
# 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

Instance Attribute Details

#traceObject

Returns the value of attribute trace.



8
9
10
# File 'lib/roadforest/http/graph-transfer.rb', line 8

def trace
  @trace
end

#type_handlingObject



66
67
68
# File 'lib/roadforest/http/graph-transfer.rb', line 66

def type_handling
  @type_handling || ContentHandling.rdf_engine
end

#user_agentObject

Returns the value of attribute user_agent.



8
9
10
# File 'lib/roadforest/http/graph-transfer.rb', line 8

def user_agent
  @user_agent
end

Instance Method Details

#best_type_for(url) ⇒ Object



70
71
72
# File 'lib/roadforest/http/graph-transfer.rb', line 70

def best_type_for(url)
  return @type_preferences[url]
end

#build_response(url, response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



20
21
22
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



24
25
26
# File 'lib/roadforest/http/graph-transfer.rb', line 24

def post(url, graph)
  make_request("POST", url, graph)
end

#put(url, graph) ⇒ Object



16
17
18
# File 'lib/roadforest/http/graph-transfer.rb', line 16

def put(url, graph)
  make_request("PUT", url, graph)
end

#record_accept_header(url, types) ⇒ Object



74
75
76
77
78
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# 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