Module: Getbox

Includes:
Capybara::DSL
Defined in:
lib/getbox.rb,
lib/getbox/version.rb

Constant Summary collapse

VERSION =
"1.0.4"

Instance Method Summary collapse

Instance Method Details

#getGistsFromFile(file) ⇒ Object

convenience wrapper for file.open. saving gistbox’s dashboard html in chrome is faster than fetching it in ruby.



66
67
68
69
# File 'lib/getbox.rb', line 66

def getGistsFromFile(file)
  f = File.open(file)
  getGistsFromHtml(f)
end

#getGistsFromHtml(html) ⇒ Object

main function to parse the gist data from gistbox’s interface



72
73
74
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
# File 'lib/getbox.rb', line 72

def getGistsFromHtml(html)
  doc = Nokogiri::HTML(html)

  gists = []

  doc.css('div.split-gist').each do |gist|
    data = {}

    id = gist.attribute('data-id').value
    data[:url] = "https://gist.github.com/#{id}"

    data[:title] = gist.css('div.gist-name').inner_text

    data[:timestamp] = gist.css('span.gist-created-updated').inner_text

    description = gist.css('div.gist-description').inner_text.strip
    description = nil if description == 'No Description'
    data[:description] = description if description

    labels = []
    gist.css('span.gist-label').each {|label| labels.push label.inner_text}
    data[:labels] = labels

    gists.push data
  end

  gists
end

#getGistsFromSite(username, password) ⇒ Object

method to go to app.gistboxapp.com, and log in with github credentials



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/getbox.rb', line 45

def getGistsFromSite(username, password)
  # partly to avoid warnings, and partly to avoid hitting analytics
  whitelist_urls

  puts "visiting app.gistboxapp.com"
  visit '/'
  click_on "Login"      # redirect to github login page

  puts "filling out github login form"
  within("#login") do
    fill_in("login", :with => username)
    fill_in("password", :with => password)
    click_on "Sign in"  # back to gistbox.app
  end

  puts "gathering gists"
  getGistsFromHtml(page.html)
end

#promptObject

to use getbox interactively and write output to a file



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/getbox.rb', line 16

def prompt()
  # people shouldn't feel comfortable typing passwords into things.
  puts \
    "I'm about to ask for your github password. \n"\
    "You should probably read my source code\n"\
    "before you go through with this...\n"\
    "https://github.com/amonks/getbox/blob/master/lib/getbox.rb\n\n"\
    \
    "are you sure you want to continue?"

  raise "Well, OK then." if gets.chomp.downcase.include? "no"

  puts "What's your github username?"
  username = gets.chomp
  puts "How about your password, eh??"
  password = gets.chomp

  # doesn't support anything cool like ~, only a locally relative path
  puts "Where should I save your gists? [gists.json]"
  file = gets.chomp
  file = "gists.json" if file.empty?

  gists = getGistsFromSite(username, password)

  puts "Saving #{gists.length} gists to #{Dir.pwd}/#{file}"
  writeToFile(gists, file)
end

#whitelist_urlsObject

set up capybara-webkit’s whitelest



108
109
110
111
112
113
114
115
# File 'lib/getbox.rb', line 108

def whitelist_urls()
  page.driver.block_unknown_urls  # block tracking and media, disable warnings
  urls = [
    'app.gistboxapp.com',         # whitelist the servers we need
    'github.com',
  ]
  urls.each { |url| page.driver.allow_url(url) }
end

#writeToFile(object, file) ⇒ Object

convenience wrapper for json-and-print



102
103
104
105
# File 'lib/getbox.rb', line 102

def writeToFile(object, file)
  json = JSON.pretty_generate(object)
  File.open(file, 'w') { |f| f.write(json) }
end