Class: MetaProject::ScmWeb::Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_project/scm_web/pathname.rb

Overview

A subset of the <a href=“www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html”>Pathname</a> API which is suitable for slurping SCM contents off the web.

Instance Method Summary collapse

Constructor Details

#initialize(browser, parent, basename, revision_identifier, directory) ⇒ Pathname

Returns a new instance of Pathname.



9
10
11
# File 'lib/meta_project/scm_web/pathname.rb', line 9

def initialize(browser, parent, basename, revision_identifier, directory)
  @browser, @parent, @basename, @revision_identifier, @directory = browser, parent, basename, revision_identifier, directory
end

Instance Method Details

#basenameObject

The basename of this instance



43
44
45
# File 'lib/meta_project/scm_web/pathname.rb', line 43

def basename
  @basename
end

#child(basename, revision_identifier = nil, directory = true) ⇒ Object

Returns a Pathname at the relative_path from this directory. Note: This only works if this instance is a directory?



30
31
32
33
# File 'lib/meta_project/scm_web/pathname.rb', line 30

def child(basename, revision_identifier=nil, directory=true)
  raise "Can't create children for files" unless @directory
  Pathname.new(@browser, self, basename, revision_identifier, directory)
end

#childrenObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/meta_project/scm_web/pathname.rb', line 52

def children
  result = []
  html = better_open(cleanpath).read

  html.scan(@browser.child_dirs_pattern) do |child_basename_rev_array|
    child_basename = child_basename_rev_array[0]
    child_revision_identifier = child_basename_rev_array[1] # nil for most browsers
#puts "DIR:#{child_basename}:#{child_revision_identifier}"
    result << child(child_basename, child_revision_identifier, true)
  end

  html.scan(@browser.child_files_pattern) do |child_basename_rev_array|
    child_basename = child_basename_rev_array[0]
    child_revision_identifier = child_basename_rev_array[1]
#puts "FILE:#{child_basename}:#{child_revision_identifier}"
    result << child(child_basename, child_revision_identifier, false)
  end
  result
end

#cleanpathObject

Returns the full path on the web (to the dir page for directories, history page for files)



48
49
50
# File 'lib/meta_project/scm_web/pathname.rb', line 48

def cleanpath
  directory? ? @browser.dir(path_from_root) : @browser.history(path_from_root)
end

#directory?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/meta_project/scm_web/pathname.rb', line 83

def directory?
  @directory
end

#open(&block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/meta_project/scm_web/pathname.rb', line 72

def open(&block)
  url = @browser.raw(path_from_root, revision_identifier)
  begin
    better_open(url, &block)
  rescue Errno::ECONNREFUSED => e
    # TODO: retry 2-3 times
    e.message += "(URL:#{url})"
    raise e
  end
end

#parentObject

The parent of this instance



38
39
40
# File 'lib/meta_project/scm_web/pathname.rb', line 38

def parent
  @parent
end

#path_from_rootObject

The relative path from the root of the browser instance

“” “var” “var/spool”



18
19
20
21
# File 'lib/meta_project/scm_web/pathname.rb', line 18

def path_from_root
  parent_path_from_root = @parent ? @parent.path_from_root : ""
  parent_path_from_root == "" ? @basename : "#{parent_path_from_root}/#{@basename}"
end

#revision_identifierObject

The revision of this file or nil if this is a directory



24
25
26
# File 'lib/meta_project/scm_web/pathname.rb', line 24

def revision_identifier
  @revision_identifier
end