Class: JScrambler::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_config = nil) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/jscrambler/client.rb', line 6

def initialize(json_config=nil)
  setup(json_config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#apiObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/jscrambler/client.rb', line 48

def api
  @api ||= Faraday.new(url: url, ssl: {verify: false}) do |builder|
    builder.use       JScrambler::Middleware::DefaultParams
    builder.use       JScrambler::Middleware::Authentication
    builder.request   :multipart
    builder.request   :url_encoded
    # builder.response  :logger
    builder.adapter   Faraday.default_adapter
  end
end

#handle_response(response) ⇒ Object



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

def handle_response(response)
  begin
    body_hash = JSON.parse(response.body)
  rescue JSON::ParserError
    body_hash = response.body
  end

  LOGGER.debug response.body

  if response.status == 200
    yield(body_hash) if block_given?
  else
    raise JScrambler::ApiError, "Error: #{body_hash['message']}"
  end
end

#new_projectObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jscrambler/client.rb', line 14

def new_project
  zipfile = archiver.zip(config['filesSrc'].to_a)
  payload = {
      files: [Faraday::UploadIO.new(zipfile.path, 'application/octet-stream')]
  }.merge(config['params'])

  LOGGER.info "Uploading #{payload[:files].count} file(s) to JScrambler"
  handle_response(api.post('code.json', payload)) do |json_response|
    JScrambler::Project.new(json_response, self)
  end
end

#projects(options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/jscrambler/client.rb', line 26

def projects(options={})
  handle_response(api.get('code.json', options)) do |response|
    response.map { |project_hash| JScrambler::Project.new(project_hash, self) }
  end
end

#setup(json_config = nil) ⇒ Object



10
11
12
# File 'lib/jscrambler/client.rb', line 10

def setup(json_config=nil)
  @config = JScrambler::Config.new(json_config).to_hash
end