Class: JScrambler::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/jscrambler/project.rb

Defined Under Namespace

Classes: File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, client) ⇒ Project

Returns a new instance of Project.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jscrambler/project.rb', line 6

def initialize(options={}, client)
  raise ArgumentError, 'Cannot create project without ID' unless options['id']

  @client = client

  @id = options['id']
  @sources = options['sources']
  @received_at = options['received_at']
  @finished_at = options['finished_at']
  @js_files = options['js_files']
  @html_files = options['html_files']
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def client
  @client
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def finished_at
  @finished_at
end

#html_filesObject (readonly)

Returns the value of attribute html_files.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def html_files
  @html_files
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def id
  @id
end

#js_filesObject (readonly)

Returns the value of attribute js_files.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def js_files
  @js_files
end

#received_atObject (readonly)

Returns the value of attribute received_at.



4
5
6
# File 'lib/jscrambler/project.rb', line 4

def received_at
  @received_at
end

Instance Method Details

#deleteObject



54
55
56
57
58
59
60
# File 'lib/jscrambler/project.rb', line 54

def delete
  client.handle_response(client.api.delete("code/#{id}.zip")) do |json_response|
    is_deleted = json_response['deleted']
    LOGGER.info "Deleted project #{id}" if is_deleted
    is_deleted
  end
end

#downloadObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jscrambler/project.rb', line 38

def download
  if status == :finished
    client.handle_response(client.api.get("code/#{id}.zip")) do |response|
      LOGGER.info "Downloading source files for #{id}..."
      temp = Tempfile.new(%w(jscrambler .zip))
      temp.write(response.force_encoding('UTF-8'))
      temp.close
      JScrambler::Archiver.new(temp).unzip(client.config['filesDest'])
    end
    delete if client.config['deleteProject']
    true
  else
    false
  end
end

#filesObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/jscrambler/project.rb', line 19

def files
  @files ||= begin
    if @sources.nil?
      @sources = client.handle_response(client.api.get("code/#{id}.json")) do |json_response|
        json_response['sources']
      end
    end
    @sources.to_a.map { |file_hash| JScrambler::Project::File.new(file_hash.merge('project_id' => id), client) }
  end
end

#statusObject



30
31
32
33
34
35
36
# File 'lib/jscrambler/project.rb', line 30

def status
  client.handle_response(client.api.get("code/#{id}.json")) do |json_response|
    status = json_response['finished_at'] ? :finished : :processing
    LOGGER.debug "Status for project #{id}: #{status}"
    status
  end
end