Class: Phantomblaster::Models::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/phantomblaster/models/script.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Script

Returns a new instance of Script.



31
32
33
34
35
36
37
# File 'lib/phantomblaster/models/script.rb', line 31

def initialize(params)
  @id = params['id']
  @name = params['name']
  @source = params['source']
  @last_saved_at = params['lastSaveDate']
  @text = params['text'].to_s
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



29
30
31
# File 'lib/phantomblaster/models/script.rb', line 29

def id
  @id
end

#last_saved_atObject (readonly)

Returns the value of attribute last_saved_at.



29
30
31
# File 'lib/phantomblaster/models/script.rb', line 29

def last_saved_at
  @last_saved_at
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/phantomblaster/models/script.rb', line 29

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



29
30
31
# File 'lib/phantomblaster/models/script.rb', line 29

def source
  @source
end

Class Method Details

.allObject



14
15
16
17
# File 'lib/phantomblaster/models/script.rb', line 14

def self.all
  data = Phantomblaster::Client.get('/scripts')
  data.map { |params| new(params) }
end

.find(id) ⇒ Object



4
5
6
7
# File 'lib/phantomblaster/models/script.rb', line 4

def self.find(id)
  data = Phantomblaster::Client.get("/script/by-id/json/#{id}", withoutText: false)
  new(data)
end

.find_by_name(name) ⇒ Object



9
10
11
12
# File 'lib/phantomblaster/models/script.rb', line 9

def self.find_by_name(name)
  data = Phantomblaster::Client.get("/script/by-name/json/#{name}", withoutText: false)
  new(data)
end

.upload(name) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
# File 'lib/phantomblaster/models/script.rb', line 19

def self.upload(name)
  pathname = Pathname.new("#{Phantomblaster.configuration.scripts_dir}/#{name}")
  raise MissingFileError, "#{pathname.realdirpath} not found" unless pathname.file?

  text = pathname.open(&:read)
  Phantomblaster::Client.post("/script/#{name}", text: text,
                                                 insertOnly: false,
                                                 source: :phantomblaster)
end

Instance Method Details

#textObject



39
40
41
# File 'lib/phantomblaster/models/script.rb', line 39

def text
  @text ||= self.class.find(id).instance_variable_get(:@text)
end