Class: RXFReader

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

Class Method Summary collapse

Class Method Details

.exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.exists?(filename)

  type = self.filetype(filename)

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

  return nil unless filex

  filex.exists? filename

end

.filetype(x) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rxfreader.rb', line 53

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.exists?(x) then
      :file
    else
      :text
    end

  end
end

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

Raises:



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
133
134
135
136
137
# File 'lib/rxfreader.rb', line 75

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.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

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rxfreader.rb', line 139

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

  response = Net::HTTP.get_response(URI.parse(uri_str))
  url = case response
    when Net::HTTPRedirection then response['location']
  end

  return a << uri_str if url.nil?

  reveal(url, a << uri_str)
end