rasper_client 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e095ad00114767244b0b647545500383c9b1ed138934afd5cb0d0aad3ca3334d
4
- data.tar.gz: 94d5c03f652b4e7044d7a95118ed18069f3d23a327ada852df25dac0e5181129
3
+ metadata.gz: 5c4a041873bc25ff2131ace5c3c375ff3982a28af94541d4486d9e6a9c43d6a8
4
+ data.tar.gz: c84ffdf095ca4ca14e4c9c256cdfcd61acf22ef85a6df9e680147662df0428df
5
5
  SHA512:
6
- metadata.gz: 4b37451fddf6dcb7dc397f966331c1509645d1652e58bb7f50012e39ed32bd1006781ef4e8b0fde5f041cb208db728b924e52753bb8008c206e9e2ba1a842259
7
- data.tar.gz: a3947cbe7e623c748f75b829a5a57d1877d06a87d2a3b032f51ed5e3cff9b350d393efde588aba6f40ef9437108bc224bf0137ac9b1ea5a5599d25559c8bf168
6
+ metadata.gz: aeacb357e4fd2859c1616e41e912c7586ab5f60bfb18eaf1a307888a0ab8286353c0d7fbc41d27935881ce69da5294c72fff0e1ddc78c29087efa1dbd1605bee
7
+ data.tar.gz: be28beb6432db0dd655b895c570848918f6fdadce4ad49fa040abcf5a6cfcc38c88267612d33cc4800ffce2247d6339080a49c2adb3f1f85b456dd23cd3941fa
data/lib/rasper_client.rb CHANGED
@@ -3,5 +3,5 @@ require "rasper_client/version"
3
3
  module RasperClient
4
4
  autoload :Client, 'rasper_client/client'
5
5
  autoload :FakeServer, 'rasper_client/fake_server'
6
- autoload :ConnectionRefusedError, 'rasper_client/errors'
7
- end
6
+ autoload :Error, 'rasper_client/error'
7
+ end
@@ -4,12 +4,17 @@ require 'json'
4
4
 
5
5
  module RasperClient
6
6
  class Client
7
- def initialize(host:, port:, timeout: nil, empty_nil_values: false)
7
+ def initialize(host:, port: nil, timeout: nil, path_prefix: nil,
8
+ secure: true, empty_nil_values: false,
9
+ username: nil, password: nil)
8
10
  @host = host
9
11
  @port = port
10
12
  @timeout = timeout
11
13
  @empty_nil_values = empty_nil_values
12
- @request_params = build_request_params
14
+ @path_prefix = path_prefix
15
+ @secure = secure
16
+ @username = username
17
+ @password = password
13
18
  end
14
19
 
15
20
  def add(options)
@@ -17,8 +22,6 @@ module RasperClient
17
22
  encode_options(options)
18
23
  response = execute_request(:add, options)
19
24
  JSON.parse(response.body) == { 'success' => true }
20
- rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL
21
- raise ConnectionRefusedError
22
25
  end
23
26
 
24
27
  def generate(options)
@@ -33,17 +36,21 @@ module RasperClient
33
36
  private
34
37
 
35
38
  def execute_request(action, options)
36
- Net::HTTP.start(*@request_params) do |http|
39
+ response = Net::HTTP.start(*build_request_params) do |http|
37
40
  request = Net::HTTP::Post.new(uri_for(action))
38
41
  request.body = options.to_json
42
+ request.basic_auth(@username, @password) if @username && @password
39
43
  http.request(request)
40
44
  end
45
+ check_for_errors(response)
46
+ response
41
47
  end
42
48
 
43
49
  def build_request_params
44
- params = [@host, @port]
45
- params << { read_timeout: @timeout } if @timeout
46
- params
50
+ options = {}
51
+ options[:read_timeout] = @timeout if @timeout
52
+ options[:use_ssl] = true if @secure
53
+ [@host, @port, options].compact
47
54
  end
48
55
 
49
56
  def symbolize_keys(options)
@@ -59,7 +66,7 @@ module RasperClient
59
66
  end
60
67
 
61
68
  def symbolize_key(hash, key)
62
- hash[key.to_sym] = hash.delete(key.to_s) if hash.has_key?(key.to_s)
69
+ hash[key.to_sym] = hash.delete(key.to_s) if hash.key?(key.to_s)
63
70
  end
64
71
 
65
72
  def encode_options(options)
@@ -88,7 +95,21 @@ module RasperClient
88
95
  end
89
96
 
90
97
  def uri_for(action)
91
- "http://#{@host}:#{@port}/#{action}"
98
+ uri_build_class
99
+ .build(
100
+ host: @host,
101
+ port: @port,
102
+ path: "#{@path_prefix}/#{action}"
103
+ )
104
+ .to_s
105
+ end
106
+
107
+ def uri_build_class
108
+ @secure ? URI::HTTPS : URI::HTTP
109
+ end
110
+
111
+ def check_for_errors(response)
112
+ raise RasperClient::Error.invalid_credentials if response.code.to_i == 401
92
113
  end
93
114
  end
94
115
  end
@@ -0,0 +1,11 @@
1
+ module RasperClient
2
+ class Error < ::RuntimeError
3
+ INVALID_CREDENTIALS = 'Invalid credentials'.freeze
4
+
5
+ class << self
6
+ def invalid_credentials
7
+ new(INVALID_CREDENTIALS)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -10,8 +10,12 @@ module RasperClient
10
10
  attr_accessor :last_added_report, :last_generated_report
11
11
  end
12
12
 
13
- def start(port)
14
- @thread = Thread.new { FakeApp.run! :port => port }
13
+ def start(port, username, password)
14
+ @thread = Thread.new do
15
+ FakeAppCreator
16
+ .create(username: username, password: password)
17
+ .run!(port: port, quiet: true)
18
+ end
15
19
  sleep 1
16
20
  self
17
21
  end
@@ -22,18 +26,26 @@ module RasperClient
22
26
  end
23
27
  end
24
28
 
25
- class FakeApp < Sinatra::Application
26
- post '/add' do
27
- content_type :json
28
- FakeServer.last_added_report = JSON.parse(request.body.read)
29
- { success: true }.to_json
30
- end
29
+ class FakeAppCreator < Sinatra::Application
30
+ def self.create(username: nil, password: nil)
31
+ Class.new(Sinatra::Application) do
32
+ use Rack::Auth::Basic, "Protected Area" do |user, pass|
33
+ username == user && password == pass
34
+ end if username && password
35
+
36
+ post '/add' do
37
+ content_type :json
38
+ FakeServer.last_added_report = JSON.parse(request.body.read)
39
+ { success: true }.to_json
40
+ end
31
41
 
32
- post '/generate' do
33
- content_type :json
34
- FakeServer.last_generated_report =
35
- JSON.parse(Base64.decode64(JSON.parse(request.body.read)['data']))
36
- { content: Base64.encode64(File.read(resource('dummy.pdf'))) }.to_json
42
+ post '/generate' do
43
+ content_type :json
44
+ FakeServer.last_generated_report =
45
+ JSON.parse(Base64.decode64(JSON.parse(request.body.read)['data']))
46
+ { content: Base64.encode64(File.read(resource('dummy.pdf'))) }.to_json
47
+ end
48
+ end
37
49
  end
38
50
  end
39
- end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module RasperClient
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasper_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Manhães
@@ -93,7 +93,7 @@ files:
93
93
  - Rakefile
94
94
  - lib/rasper_client.rb
95
95
  - lib/rasper_client/client.rb
96
- - lib/rasper_client/errors.rb
96
+ - lib/rasper_client/error.rb
97
97
  - lib/rasper_client/fake_server.rb
98
98
  - lib/rasper_client/version.rb
99
99
  homepage: https://github.com/rodrigomanhaes/rasper_client
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubygems_version: 3.0.3
118
+ rubygems_version: 3.1.4
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: JRuby client to RasperServer.
@@ -1,4 +0,0 @@
1
- module RasperClient
2
- class ConnectionRefusedError < RuntimeError
3
- end
4
- end