Class: RXFReader

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

Class Method Summary collapse

Class Method Details

.read(x, h = {}) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rxfreader.rb', line 32

def self.read(x, h={})

  opt = {debug: false, auto: false}.merge(h)

  debug = opt[:debug]

  raise RXFReaderException, 'nil found, expected a string' if x.nil?

  if x.strip[/^<(\?xml|[^\?])/] then

    [x, :xml]

  elsif x.lines.length == 1 then

    if x[/^https?:\/\//] then

      puts 'before GPDRequest'.info if debug

      r = if opt[:username] and opt[:password] then
        GPDRequest.new(opt[:username], opt[:password]).get(x)
      else
        response = RestClient.get(x)
      end

      case r.code
      when '404'
        raise(RXFReaderException, "404 %s not found" % x)
      when '401'
        raise(RXFReaderException, "401 %s unauthorized access" % x)
      end

      [r.body, :url]

    elsif  x[/^dfs:\/\//] then

      r = DfsFile.read(x).force_encoding('UTF-8')
      [r, :dfs]


    elsif x[/^file:\/\//] or File.exists?(x) then

      puts 'RXFHelper.read before File.read' if debug
      contents = File.read(File.expand_path(x.sub(%r{^file://}, '')))

      [contents, :file]

    elsif x =~ /\s/
      [x, :text]
    elsif DfsFile.exists?(x)
      [DfsFile.read(x).force_encoding('UTF-8'), :dfs]
    else
      [x, :unknown]
    end

  else

    [x, :unknown]
  end
end