Class: Opener::Webservice::InputExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/webservice/input_extractor.rb

Overview

Extracts the KAF/text input to use from a set of input parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputExtractor

Returns a new instance of InputExtractor.



12
13
14
# File 'lib/opener/webservice/input_extractor.rb', line 12

def initialize
  @http = HTTPClient.new
end

Instance Attribute Details

#httpHTTPClient (readonly)

Returns:

  • (HTTPClient)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/opener/webservice/input_extractor.rb', line 9

class InputExtractor
  attr_reader :http

  def initialize
    @http = HTTPClient.new
  end

  ##
  # @param [Hash] options
  #
  # @option options [String] input_url A URL to download input from.
  # @option options [String] input The direct input to process.
  #
  # @return [String]
  #
  # @raise [RuntimeError] Raised when the input could not be downloaded.
  #
  def extract(options)
    if options['input_url']
      resp = http.get(options['input_url'], :follow_redirect => true)

      unless resp.ok?
        raise "Failed to download input from #{options['input_url']}"
      end

      input = resp.body
    else
      input = options['input']
    end

    return input
  end
end

Instance Method Details

#extract(options) ⇒ String

Parameters:

  • options (Hash)

Options Hash (options):

  • input_url (String)

    A URL to download input from.

  • input (String)

    The direct input to process.

Returns:

  • (String)

Raises:

  • (RuntimeError)

    Raised when the input could not be downloaded.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opener/webservice/input_extractor.rb', line 26

def extract(options)
  if options['input_url']
    resp = http.get(options['input_url'], :follow_redirect => true)

    unless resp.ok?
      raise "Failed to download input from #{options['input_url']}"
    end

    input = resp.body
  else
    input = options['input']
  end

  return input
end