Class: JScrambler::Client
- Inherits:
-
Object
- Object
- JScrambler::Client
- Defined in:
- lib/jscrambler/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #api ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(json_config = nil) ⇒ Client
constructor
A new instance of Client.
- #new_project ⇒ Object
- #projects(options = {}) ⇒ Object
- #setup(json_config = nil) ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/jscrambler/client.rb', line 4 def config @config end |
Instance Method Details
#api ⇒ Object
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_project ⇒ Object
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(={}) handle_response(api.get('code.json', )) 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 |