Class: RXFReader

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

Class Method Summary collapse

Class Method Details

.exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rxfreader.rb', line 35

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/rxfreader.rb', line 54

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

.filetype(x) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rxfreader.rb', line 58

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:



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

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rxfreader.rb', line 144

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