rasper_client 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c4a041873bc25ff2131ace5c3c375ff3982a28af94541d4486d9e6a9c43d6a8
4
+ data.tar.gz: c84ffdf095ca4ca14e4c9c256cdfcd61acf22ef85a6df9e680147662df0428df
5
+ SHA512:
6
+ metadata.gz: aeacb357e4fd2859c1616e41e912c7586ab5f60bfb18eaf1a307888a0ab8286353c0d7fbc41d27935881ce69da5294c72fff0e1ddc78c29087efa1dbd1605bee
7
+ data.tar.gz: be28beb6432db0dd655b895c570848918f6fdadce4ad49fa040abcf5a6cfcc38c88267612d33cc4800ffce2247d6339080a49c2adb3f1f85b456dd23cd3941fa
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/README.rst CHANGED
@@ -37,6 +37,12 @@ file content and, optionally, the images to be embedded in the report::
37
37
  { name: 'image2.jpg', content: 'content of image2' }])
38
38
 
39
39
 
40
+ For avoiding errors related to the request size, ``rasper_client`` supports
41
+ adding images separately::
42
+
43
+ client.add(images: [{ name: 'imagem.jpg', content: image_content }])
44
+
45
+
40
46
  With the report uploaded, it's possible generate PDF reports::
41
47
 
42
48
  client.generate(name: 'programmers',
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,8 +4,17 @@ require 'json'
4
4
 
5
5
  module RasperClient
6
6
  class Client
7
- def initialize(options)
8
- @host, @port = options.values_at(:host, :port)
7
+ def initialize(host:, port: nil, timeout: nil, path_prefix: nil,
8
+ secure: true, empty_nil_values: false,
9
+ username: nil, password: nil)
10
+ @host = host
11
+ @port = port
12
+ @timeout = timeout
13
+ @empty_nil_values = empty_nil_values
14
+ @path_prefix = path_prefix
15
+ @secure = secure
16
+ @username = username
17
+ @password = password
9
18
  end
10
19
 
11
20
  def add(options)
@@ -13,12 +22,12 @@ module RasperClient
13
22
  encode_options(options)
14
23
  response = execute_request(:add, options)
15
24
  JSON.parse(response.body) == { 'success' => true }
16
- rescue Errno::ECONNREFUSED
17
- raise ConnectionRefusedError
18
25
  end
19
26
 
20
27
  def generate(options)
21
28
  symbolize_keys(options)
29
+ empty_nil_values(options) if @empty_nil_values
30
+ options = encode_data(options)
22
31
  response = execute_request(:generate, options)
23
32
  result = JSON.parse(response.body)
24
33
  Base64.decode64(result['content'])
@@ -27,11 +36,21 @@ module RasperClient
27
36
  private
28
37
 
29
38
  def execute_request(action, options)
30
- Net::HTTP.start(@host, @port) do |http|
39
+ response = Net::HTTP.start(*build_request_params) do |http|
31
40
  request = Net::HTTP::Post.new(uri_for(action))
32
41
  request.body = options.to_json
42
+ request.basic_auth(@username, @password) if @username && @password
33
43
  http.request(request)
34
44
  end
45
+ check_for_errors(response)
46
+ response
47
+ end
48
+
49
+ def build_request_params
50
+ options = {}
51
+ options[:read_timeout] = @timeout if @timeout
52
+ options[:use_ssl] = true if @secure
53
+ [@host, @port, options].compact
35
54
  end
36
55
 
37
56
  def symbolize_keys(options)
@@ -47,11 +66,11 @@ module RasperClient
47
66
  end
48
67
 
49
68
  def symbolize_key(hash, key)
50
- 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)
51
70
  end
52
71
 
53
72
  def encode_options(options)
54
- options[:content] = Base64.encode64(options[:content])
73
+ options[:content] = Base64.encode64(options[:content]) if options[:content]
55
74
  if options[:images]
56
75
  options[:images].each do |image|
57
76
  image[:content] = Base64.encode64(image[:content])
@@ -59,8 +78,38 @@ module RasperClient
59
78
  end
60
79
  end
61
80
 
81
+ def encode_data(options)
82
+ { data: Base64.encode64(options.to_json) }
83
+ end
84
+
85
+ def empty_nil_values(hash)
86
+ hash.each_key do |key|
87
+ if hash[key].is_a?(Hash)
88
+ empty_nil_values(hash[key])
89
+ elsif hash[key].is_a?(Array)
90
+ hash[key].each {|item| empty_nil_values(item) if item.is_a?(Hash) }
91
+ else
92
+ hash[key] = '' if hash[key].nil?
93
+ end
94
+ end
95
+ end
96
+
62
97
  def uri_for(action)
63
- "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
64
113
  end
65
114
  end
66
- end
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
@@ -2,14 +2,20 @@ require 'sinatra/base'
2
2
  require 'json'
3
3
  require 'base64'
4
4
 
5
+ Rack::Utils.key_space_limit = 262144
6
+
5
7
  module RasperClient
6
8
  class FakeServer
7
9
  class << self
8
10
  attr_accessor :last_added_report, :last_generated_report
9
11
  end
10
12
 
11
- def start(port)
12
- @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
13
19
  sleep 1
14
20
  self
15
21
  end
@@ -20,17 +26,26 @@ module RasperClient
20
26
  end
21
27
  end
22
28
 
23
- class FakeApp < Sinatra::Application
24
- post '/add' do
25
- content_type :json
26
- FakeServer.last_added_report = JSON.parse(request.body.read)
27
- { success: true }.to_json
28
- 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
29
41
 
30
- post '/generate' do
31
- content_type :json
32
- FakeServer.last_generated_report = JSON.parse(request.body.read)
33
- { 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
34
49
  end
35
50
  end
36
- end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module RasperClient
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,78 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasper_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.1.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rodrigo Manhães
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-23 00:00:00.000000000 Z
11
+ date: 2013-10-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: sinatra
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
83
  description: Ruby client to RasperServer.
@@ -82,38 +87,36 @@ executables: []
82
87
  extensions: []
83
88
  extra_rdoc_files: []
84
89
  files:
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.rst
93
+ - Rakefile
85
94
  - lib/rasper_client.rb
86
- - lib/rasper_client/fake_server.rb
87
95
  - lib/rasper_client/client.rb
96
+ - lib/rasper_client/error.rb
97
+ - lib/rasper_client/fake_server.rb
88
98
  - lib/rasper_client/version.rb
89
- - lib/rasper_client/errors.rb
90
- - README.rst
91
- - LICENSE.txt
92
- - Rakefile
93
- - Gemfile
94
99
  homepage: https://github.com/rodrigomanhaes/rasper_client
95
100
  licenses:
96
101
  - MIT
102
+ metadata: {}
97
103
  post_install_message:
98
104
  rdoc_options: []
99
105
  require_paths:
100
106
  - lib
101
107
  required_ruby_version: !ruby/object:Gem::Requirement
102
- none: false
103
108
  requirements:
104
- - - ! '>='
109
+ - - ">="
105
110
  - !ruby/object:Gem::Version
106
111
  version: '0'
107
112
  required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
113
  requirements:
110
- - - ! '>='
114
+ - - ">="
111
115
  - !ruby/object:Gem::Version
112
116
  version: '0'
113
117
  requirements: []
114
- rubyforge_project:
115
- rubygems_version: 1.8.23
118
+ rubygems_version: 3.1.4
116
119
  signing_key:
117
- specification_version: 3
120
+ specification_version: 4
118
121
  summary: JRuby client to RasperServer.
119
122
  test_files: []
@@ -1,4 +0,0 @@
1
- module RasperClient
2
- class ConnectionRefusedError < RuntimeError
3
- end
4
- end