Class: Gasoline::Drop

Inherits:
Object
  • Object
show all
Defined in:
lib/gasoline/drop.rb

Overview

A drop of Gasoline is a glorified hash that knows how to retrieve the javascript needed to make the Campfire even shinier.

Since Github’s gists are pretty much the easiest way to share pieces of code, Drop uses them for all things JS.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url, description) ⇒ Drop

Returns a new instance of Drop.



12
13
14
15
16
# File 'lib/gasoline/drop.rb', line 12

def initialize(name, url, description)
  @name = name
  @url = url
  @description = description
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/gasoline/drop.rb', line 10

def description
  @description
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/gasoline/drop.rb', line 10

def name
  @name
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/gasoline/drop.rb', line 10

def url
  @url
end

Class Method Details

.new_from_yml(hash) ⇒ Object

Takes the hash as usually found in the Jerrycan and creates a drop from it.

Returns a Drop



30
31
32
# File 'lib/gasoline/drop.rb', line 30

def self.new_from_yml(hash)
  new(hash[:name], hash[:url], hash[:description])
end

Instance Method Details

#download_contentObject

Returns the javascript from the gist URL.



40
41
42
43
# File 'lib/gasoline/drop.rb', line 40

def download_content
  puts "Downloading #{@name} (#{@url})"
  open(raw_gist_url).read
end

#headerObject

The header that will be displayed above the drop in the patched file.

Returns a String



49
50
51
52
53
54
55
56
57
# File 'lib/gasoline/drop.rb', line 49

def header
  <<HEADER
/*
 * #{@name}
 * #{@description}
 *
 */
HEADER
end

#patch(force_refresh = false) ⇒ Object

Fetches the JS content and adds a header to it, ready to be inserted in the patchor file.

force_refresh - true will redownload the JS

Returns a String



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gasoline/drop.rb', line 65

def patch(force_refresh = false)
  @js ||= download_content
  @js = download_content if force_refresh

  <<PATCH

#{header}
#{@js}

// console.log("Loaded #{@name}");
PATCH
rescue => error
  puts "  -> #{error.message}"
  nil
end

#raw_gist_urlObject

Returns the raw url for the specified gist



35
36
37
# File 'lib/gasoline/drop.rb', line 35

def raw_gist_url
  [@url, "raw"].join("/")
end

#to_hashObject



22
23
24
# File 'lib/gasoline/drop.rb', line 22

def to_hash
  {:name => @name, :url => @url, :description => @description}
end

#to_sObject



18
19
20
# File 'lib/gasoline/drop.rb', line 18

def to_s
  "%s: %s" % [@name, @description]
end