Module: OpenRemote::Browser

Extended by:
OS
Included in:
OpenRemote
Defined in:
lib/or-browser.rb

Overview

Web browser opening commands

Class Method Summary collapse

Methods included from OS

os_name

Class Method Details

.browse(remote) ⇒ Object

Generate and open approprate website from ssh/git link



24
25
26
# File 'lib/or-browser.rb', line 24

def browse(remote)
  open prepare remote
end

.browserObject

Return the right command for opening a website from the terminal



30
31
32
33
34
35
36
37
38
39
# File 'lib/or-browser.rb', line 30

def browser
  case os_name
  when "mac"
    "open "
  when "dos"
    "start "
  when "nix"
    "xdg-open "
  end
end

.git_at_to_https(base, url) ⇒ Object

Helper transformations



75
76
77
78
79
80
81
# File 'lib/or-browser.rb', line 75

def git_at_to_https(base, url)
  info = url.partition("@").last
  host = info.partition(":").first
  user_repo = info.partition(":").last
  user_repo.sub!(/\.git$/, "")
  "#{base}#{host}/#{user_repo}"
end

.git_colon_to_https(base, url) ⇒ Object



83
84
85
86
87
# File 'lib/or-browser.rb', line 83

def git_colon_to_https(base, url)
  info = url.partition("://").last
  info.sub!(/\.git$/, "")
  base << info
end

.https_to_app(base, url) ⇒ Object



95
96
97
98
99
# File 'lib/or-browser.rb', line 95

def https_to_app(base, url)
  app = url.partition(".com/").last
  app.sub!(/\.git$/, "")
  base << app
end

.open(url) ⇒ Object

Make the system call to open up a website



43
44
45
46
# File 'lib/or-browser.rb', line 43

def open(url)
  puts "Opening: ".green + url
  system browser + url
end

.prepare(url) ⇒ Object

Parse remote to determine whether it is https/ssh, give link



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/or-browser.rb', line 50

def prepare(url)
  hb = "https://" # https base url
  if /^https:\/\/git\.heroku/.match(url) # is heroku, change to app
    https_to_app hb + "dashboard.heroku.com/apps/", url

  elsif /^https/.match(url) # is website, return w/o .git ending
    url.sub(/\.git$/, "")

  elsif /^git@/.match(url) # git remote w/ @
    git_at_to_https hb, url

  elsif /^git:/.match(url) # git remote w/ :
    git_colon_to_https hb, url

  elsif /^ssh/.match(url) # is ssh link, change to website
    ssh_to_https hb, url

  else # unknown, return a generic link
    raise "Malformed remote url: " + url
  end
end

.ssh_to_https(base, url) ⇒ Object



89
90
91
92
93
# File 'lib/or-browser.rb', line 89

def ssh_to_https(base, url)
  info = url.partition("@").last
  info.sub!(/\.git$/, "")
  base << info
end