Class: RXFReader

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

Class Method Summary collapse

Class Method Details

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

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rxfreader.rb', line 74

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