Class: MajortomConnector::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/majortom_connector/request.rb

Constant Summary collapse

AVAILABLE_COMMANDS =
{
  'topics' => 'topics',
  'topicmaps' => 'list_maps',
  'resolvetm' => 'find_topic_map_id_by_base_iri',
  'xtm' => 'to_xtm',
  'ctm' => 'to_ctm',
  'tmql' => 'tmql',
  'sparql' => 'sparql',
  'beru' => 'search',
  'clearcache' => 'clear_cache',
  'connectiontest' => 'established?'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Request

Returns a new instance of Request.



28
29
30
# File 'lib/majortom_connector/request.rb', line 28

def initialize(config)
  @config = config
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



17
18
19
# File 'lib/majortom_connector/request.rb', line 17

def result
  @result
end

Class Method Details

.available_commandsObject



20
21
22
# File 'lib/majortom_connector/request.rb', line 20

def self.available_commands
  AVAILABLE_COMMANDS
end

.command_available?(command) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/majortom_connector/request.rb', line 24

def self.command_available?(command)
  available_commands.keys.include?(command)
end

Instance Method Details

#get(command, query = "") ⇒ Object



46
47
48
# File 'lib/majortom_connector/request.rb', line 46

def get(command, query = "")
  @result.parse(Net::HTTP.get_response(URI.parse("#{server_uri}/tm/#{command}#{parameter_builder(command, query)}")))
end

#post(command, query) ⇒ Object



50
51
52
# File 'lib/majortom_connector/request.rb', line 50

def post(command, query)
  @result.parse(Net::HTTP.post_form(URI.parse("#{server_uri}/tm/#{command}/#{@config.map_id}/"), {:query => query, :apikey => @config.api_key}))
end

#run(command, param = "") ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/majortom_connector/request.rb', line 32

def run(command, param = "")
  raise ArgumentError, "Command #{MajortomConnector::Request.available_commands[command]} not available. Try one of the following: #{MajortomConnector::Request.available_commands.values.join(', ')}" unless self.class.command_available?(command)
  @result = Result.new
  post(command, param) if %w[tmql sparql beru].include?(command)
  get(command, param) if %w[topics topicmaps resolvetm clearcache].include?(command)
  stream(command) if %w[xtm ctm].include?(command)
  test if %w[connectiontest].include?(command)
  return @result
end

#stream(command) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/majortom_connector/request.rb', line 54

def stream(command)
  buffer = ""
  response = Net::HTTP.get_response(URI.parse("#{server_uri}/tm/#{command}/#{@config.map_id}?apikey=#{@config.api_key}")) do |res|
    res.read_body do |segment|
      buffer << segment
    end
  end
  @result.parse(response, command, buffer)
end

#successful?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/majortom_connector/request.rb', line 42

def successful?
  @result.result_successful?
end

#testObject



64
65
66
# File 'lib/majortom_connector/request.rb', line 64

def test
  @result.parse(Net::HTTP.get_response(URI.parse("#{server_uri}/")), 'html')
end