Module: Getbox

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

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#getGistsFromFile(file) ⇒ Object



48
49
50
51
# File 'lib/getbox.rb', line 48

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

#getGistsFromHtml(html) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/getbox.rb', line 53

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/getbox.rb', line 32

def getGistsFromSite(username, password)
  whitelist_urls

  puts "visiting app.gistboxapp.com"
  visit '/'
  click_on "Login"
  puts "filling out github login form"
  within("#login") do
    fill_in("login", :with => username)
    fill_in("password", :with => password)
    click_on "Sign in"
  end
  puts "gathering gists"
  getGistsFromHtml(page.html)
end

#promptObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/getbox.rb', line 16

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

  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



87
88
89
90
91
92
93
94
# File 'lib/getbox.rb', line 87

def whitelist_urls()
  page.driver.block_unknown_urls
  urls = [
    'app.gistboxapp.com',
    'github.com',
  ]
  urls.each { |url| page.driver.allow_url(url) }
end

#writeToFile(object, file) ⇒ Object



82
83
84
85
# File 'lib/getbox.rb', line 82

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