Class: Concord::Resource

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/concord/resource.rb

Constant Summary collapse

SHORT_FILENAME_REGEX =
/([^\/]+)$/
URL_REGEX =

!!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME: Right now, the code extracts the matching url from the first match group. Ruby 1.9 supports named groups – once 1.9 is ubiquitous,

we should switch to using named groups to allow more complex regex matchers

!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/(http[s]?:\/\/[^'"<>]+)/i
SRC_REGEX =

the imageBytes can be referenced by a OTImage object MW and Netlogo models use authoredDataURL attributes

/(?:src|href|imageBytes|authoredDataURL)[ ]?=[ ]?['"](.+?)['"<>]/i
ALWAYS_SKIP_REGEXES =
[]
RECURSE_ONCE_REGEX =
/html$/i
RECURSE_FOREVER_REGEX =
/(otml|cml|mml|nlogo|config)$/i
FILE_SPECIFIC_REGEXES =
{}
NLOGO_REGEX =

These regexes will only match within a file that ends with .nlogo

/.*\.nlogo/
MW_REGEX =

These regexes will only match within a file that ends with .cml or .mml

/.*\.(:?cml|mml)/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#write_property_map

Class Attribute Details

.cache_headersObject

Returns the value of attribute cache_headers.



62
63
64
# File 'lib/concord/resource.rb', line 62

def cache_headers
  @cache_headers
end

.cacherObject

Returns the value of attribute cacher.



67
68
69
# File 'lib/concord/resource.rb', line 67

def cacher
  @cacher
end

.create_mapObject

Returns the value of attribute create_map.



64
65
66
# File 'lib/concord/resource.rb', line 64

def create_map
  @create_map
end

.custom_skipsObject

Returns the value of attribute custom_skips.



70
71
72
# File 'lib/concord/resource.rb', line 70

def custom_skips
  @custom_skips
end

.debugObject

Returns the value of attribute debug.



60
61
62
# File 'lib/concord/resource.rb', line 60

def debug
  @debug
end

.errorsObject (readonly)

Returns the value of attribute errors.



66
67
68
# File 'lib/concord/resource.rb', line 66

def errors
  @errors
end

.filename_generatorObject

Returns the value of attribute filename_generator.



68
69
70
# File 'lib/concord/resource.rb', line 68

def filename_generator
  @filename_generator
end

.relative_hostsObject

Returns the value of attribute relative_hosts.



69
70
71
# File 'lib/concord/resource.rb', line 69

def relative_hosts
  @relative_hosts
end

.rewrite_urlsObject

Returns the value of attribute rewrite_urls.



63
64
65
# File 'lib/concord/resource.rb', line 63

def rewrite_urls
  @rewrite_urls
end

.url_mapObject (readonly)

Returns the value of attribute url_map.



65
66
67
# File 'lib/concord/resource.rb', line 65

def url_map
  @url_map
end

.verboseObject

Returns the value of attribute verbose.



61
62
63
# File 'lib/concord/resource.rb', line 61

def verbose
  @verbose
end

Instance Attribute Details

#cache_dirObject

Returns the value of attribute cache_dir.



13
14
15
# File 'lib/concord/resource.rb', line 13

def cache_dir
  @cache_dir
end

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/concord/resource.rb', line 11

def content
  @content
end

#extrasObject

Returns the value of attribute extras.



15
16
17
# File 'lib/concord/resource.rb', line 15

def extras
  @extras
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/concord/resource.rb', line 11

def headers
  @headers
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/concord/resource.rb', line 12

def parent
  @parent
end

#should_recurseObject

Returns the value of attribute should_recurse.



14
15
16
# File 'lib/concord/resource.rb', line 14

def should_recurse
  @should_recurse
end

#uriObject

Returns the value of attribute uri.



8
9
10
# File 'lib/concord/resource.rb', line 8

def uri
  @uri
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/concord/resource.rb', line 8

def url
  @url
end

Class Method Details

.clear_errorsObject



86
87
88
# File 'lib/concord/resource.rb', line 86

def self.clear_errors
  @errors = {}
end

.clear_mapObject



90
91
92
# File 'lib/concord/resource.rb', line 90

def self.clear_map
  @url_map = {}
end

.error(u, str) ⇒ Object



81
82
83
84
# File 'lib/concord/resource.rb', line 81

def self.error(u,str)
  @errors[u] ||= []
  @errors[u] << str
end

.map(k, v) ⇒ Object



73
74
75
# File 'lib/concord/resource.rb', line 73

def self.map(k,v)
  @url_map[k] = v
end

.unmap(k) ⇒ Object



77
78
79
# File 'lib/concord/resource.rb', line 77

def self.unmap(k)
  @url_map.delete(k)
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/concord/resource.rb', line 113

def exists?
  File.exists?(self.cache_dir + self.local_filename)
end

#has_codebase?Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/concord/resource.rb', line 127

def has_codebase?
  return false unless self.content
  return self.content =~ /<otrunk[^>]+codebase[ ]?=[ ]?['"]([^'"]+)/
end

#loadObject



117
118
119
120
121
122
123
124
125
# File 'lib/concord/resource.rb', line 117

def load
  open(self.uri_str) do |r|
    self.headers = r.respond_to?("meta") ? r.meta : {}
    self.headers['_http_version'] = "HTTP/1.1 #{r.respond_to?("status") ? r.status.join(" ") : "200 OK"}"
    self.content = r.read
  end
  self.remove_codebase if self.class.rewrite_urls
  ::Concord::Resource.map(self.uri_str, self.local_filename) if self.class.create_map
end

#local_filenameObject



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/concord/resource.rb', line 168

def local_filename
  return @local_filename if @local_filename
  if (self.relativize_only?)
    @local_filename = self.uri.path
    @local_filename << "?#{self.uri.query}" if self.uri.query
    @local_filename << "##{self.uri.fragment}" if self.uri.fragment
  else
    @local_filename = self.class.filename_generator.generate_filename(self)
  end
  return @local_filename
end

#processObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/concord/resource.rb', line 136

def process
  print "\n#{self.remote_filename}: " if self.class.verbose
  processed_lines = []
  ending_newlines = self.content[/([\n]+)$/m, 1]
  lines = self.content.split("\n")
  lines.each do |line|
    processed_lines << _process_line(line)
  end

  print ".\n" if self.class.verbose
  self.content = processed_lines.join("\n") + (ending_newlines || '')
end

#recursable?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/concord/resource.rb', line 180

def recursable?
  return (RECURSE_ONCE_REGEX.match(self.remote_filename) || RECURSE_FOREVER_REGEX.match(self.remote_filename))
end

#relativize_only?Boolean

Returns:

  • (Boolean)


189
190
191
192
# File 'lib/concord/resource.rb', line 189

def relativize_only?
  return true if ::Concord::Resource.relative_hosts.include?(self.uri.host.to_s)
  return false
end

#releaseObject



108
109
110
111
# File 'lib/concord/resource.rb', line 108

def release
  FileUtils.rm(self.cache_dir + @local_filename) if @local_filename
  ::Concord::Resource.unmap(self.uri_str) if self.class.create_map
end

#remote_filenameObject



161
162
163
164
165
166
# File 'lib/concord/resource.rb', line 161

def remote_filename
  return @remote_filename if @remote_filename
  @remote_filename = self.uri.path[SHORT_FILENAME_REGEX,1]
  @remote_filename = 'index.html' unless @remote_filename
  return @remote_filename
end

#remove_codebaseObject



132
133
134
# File 'lib/concord/resource.rb', line 132

def remove_codebase
  self.content.sub!(/codebase[ ]?=[ ]?['"][^'"]+['"]/,"")
end

#reserveObject

Reserving the file will prohibit any further references to this same file to be skipped, this avoiding endlessly recursing references



104
105
106
# File 'lib/concord/resource.rb', line 104

def reserve
  FileUtils.touch(self.cache_dir + self.local_filename)
end

#should_recurse?Boolean

Returns:

  • (Boolean)


184
185
186
187
# File 'lib/concord/resource.rb', line 184

def should_recurse?
  return true if self.should_recurse || RECURSE_FOREVER_REGEX.match(self.remote_filename)
  return false
end

#skip?Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
# File 'lib/concord/resource.rb', line 154

def skip?
  return true if self.url.length < 1
  return true if self.class.custom_skips.detect{|r| r.match(self.url) }
  return true if ALWAYS_SKIP_REGEXES.detect{|r| r.match(self.url) }
  return false
end

#uri_strObject



149
150
151
152
# File 'lib/concord/resource.rb', line 149

def uri_str
  return nil unless self.uri
  self.uri.scheme == 'file' ? self.uri.path : self.uri.to_s
end

#writeObject



94
95
96
97
98
99
100
101
# File 'lib/concord/resource.rb', line 94

def write
  File.open(self.cache_dir + self.local_filename, "w") do |f|
    f.write(self.content)
    f.flush
  end
  write_property_map(self.cache_dir + self.local_filename + ".hdrs", self.headers) if self.class.cache_headers
  
end