Class: RXFReader

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

Class Method Summary collapse

Class Method Details

.dirname(s) ⇒ Object



36
37
38
# File 'lib/rxfreader.rb', line 36

def self.dirname(s)
  File.dirname s
end

.exist?(filename) ⇒ Boolean



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rxfreader.rb', line 40

def self.exist?(filename)

  type = self.filetype(filename)

  filex = case type
  when :file
    File
  when :dfs
    DfsFile
  else
    nil
  end

  return nil unless filex

  filex.exist? filename

end

.exists?(filename) ⇒ Boolean



59
60
61
# File 'lib/rxfreader.rb', line 59

def self.exists?(filename)
  self.exist?(filename)
end

.filetype(x) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rxfreader.rb', line 63

def self.filetype(x)

  return :string if x.lines.length > 1

  case x
  when /^https?:\/\//
    :http
  when /^dfs:\/\//
    :dfs
  when /^file:\/\//
    :file
  else

    if File.exist?(x) then
      :file
    else
      :text
    end

  end
end

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

Raises:



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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rxfreader.rb', line 85

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.class.to_s =~ /Rexle$/ then

    [x.xml, :rexle]

  elsif 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.exist?(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.exist?(x)
      [DfsFile.read(x).force_encoding('UTF-8'), :dfs]
    else
      [x, :unknown]
    end
    
  else

    [x, :unknown]
  end
end

.reveal(uri_str, a = []) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rxfreader.rb', line 149

def self.reveal(uri_str, a=[])

  u = URI.parse(uri_str)
  response = Net::HTTP.get_response(u)

  url = case response
    when Net::HTTPRedirection then response['location']
  end

  return a << uri_str if url.nil?
  url.prepend u.origin if not url[/^http/]

  reveal(url, a << uri_str)
end