rasper_client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rodrigo Manhães
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rst ADDED
@@ -0,0 +1,63 @@
1
+ rasper_client
2
+ =============
3
+
4
+ Ruby client to `rasper_server <https://github.com/rodrigomanhaes/rasper_server>`_.
5
+
6
+
7
+ Installation
8
+ ------------
9
+
10
+ Add this line to your application's Gemfile::
11
+
12
+ gem 'rasper_client'
13
+
14
+ And then execute::
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as::
19
+
20
+ $ gem install rasper_client
21
+
22
+
23
+ Usage
24
+ -----
25
+
26
+ Create a rasper client object, giving host and port::
27
+
28
+
29
+ client = RasperClient::Client.new(host: 'host', port: 3000)
30
+
31
+
32
+ Having a client, you can add a report to server, passing a name, JRXML report
33
+ file content and, optionally, the images to be embedded in the report::
34
+
35
+ client.add(name: 'programmers', content: 'content of JRXML',
36
+ images: [{ name: 'image1.jpg', content: 'content of image1' },
37
+ { name: 'image2.jpg', content: 'content of image2' }])
38
+
39
+
40
+ With the report uploaded, it's possible generate PDF reports::
41
+
42
+ client.generate(name: 'programmers',
43
+ data: [
44
+ { name: 'Linus', software: 'Linux' },
45
+ { name: 'Yukihiro', software: 'Ruby' },
46
+ { name: 'Guido', software: 'Python' }
47
+ ],
48
+ parameters: { 'DATE' => 'January 12, 2013' })
49
+
50
+
51
+ The keys for ``data``'s inner hashes are report-specific. The parameters' keys
52
+ are report-specific, and are mandatory if report has parameters with no default
53
+ values.
54
+
55
+
56
+ Contributing
57
+ ------------
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,66 @@
1
+ require 'net/http'
2
+ require 'base64'
3
+ require 'json'
4
+
5
+ module RasperClient
6
+ class Client
7
+ def initialize(options)
8
+ @host, @port = options.values_at(:host, :port)
9
+ end
10
+
11
+ def add(options)
12
+ symbolize_keys(options)
13
+ encode_options(options)
14
+ response = execute_request(:add, options)
15
+ JSON.parse(response.body) == { 'success' => true }
16
+ rescue Errno::ECONNREFUSED
17
+ raise ConnectionRefusedError
18
+ end
19
+
20
+ def generate(options)
21
+ symbolize_keys(options)
22
+ response = execute_request(:generate, options)
23
+ result = JSON.parse(response.body)
24
+ Base64.decode64(result['content'])
25
+ end
26
+
27
+ private
28
+
29
+ def execute_request(action, options)
30
+ Net::HTTP.start(@host, @port) do |http|
31
+ request = Net::HTTP::Post.new(uri_for(action))
32
+ request.body = options.to_json
33
+ http.request(request)
34
+ end
35
+ end
36
+
37
+ def symbolize_keys(options)
38
+ %w(name content images report data parameters).each do |s|
39
+ symbolize_key(options, s)
40
+ end
41
+ if options[:images]
42
+ options[:images].each do |image|
43
+ symbolize_key(image, :name)
44
+ symbolize_key(image, :content)
45
+ end
46
+ end
47
+ end
48
+
49
+ def symbolize_key(hash, key)
50
+ hash[key.to_sym] = hash.delete(key.to_s) if hash.has_key?(key.to_s)
51
+ end
52
+
53
+ def encode_options(options)
54
+ options[:content] = Base64.encode64(options[:content])
55
+ if options[:images]
56
+ options[:images].each do |image|
57
+ image[:content] = Base64.encode64(image[:content])
58
+ end
59
+ end
60
+ end
61
+
62
+ def uri_for(action)
63
+ "http://#{@host}:#{@port}/#{action}"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ module RasperClient
2
+ class ConnectionRefusedError < RuntimeError
3
+ end
4
+ end
@@ -0,0 +1,36 @@
1
+ require 'sinatra/base'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+ module RasperClient
6
+ class FakeServer
7
+ class << self
8
+ attr_accessor :last_added_report, :last_generated_report
9
+ end
10
+
11
+ def start(port)
12
+ @thread = Thread.new { FakeApp.run! :port => port }
13
+ sleep 1
14
+ self
15
+ end
16
+
17
+ def stop
18
+ @thread.kill
19
+ self
20
+ end
21
+ end
22
+
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
+
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
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module RasperClient
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "rasper_client/version"
2
+
3
+ module RasperClient
4
+ autoload :Client, 'rasper_client/client'
5
+ autoload :FakeServer, 'rasper_client/fake_server'
6
+ autoload :ConnectionRefusedError, 'rasper_client/errors'
7
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rasper_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Manhães
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sinatra
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby client to RasperServer.
79
+ email:
80
+ - rmanhaes@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/rasper_client.rb
86
+ - lib/rasper_client/fake_server.rb
87
+ - lib/rasper_client/client.rb
88
+ - lib/rasper_client/version.rb
89
+ - lib/rasper_client/errors.rb
90
+ - README.rst
91
+ - LICENSE.txt
92
+ - Rakefile
93
+ - Gemfile
94
+ homepage: https://github.com/rodrigomanhaes/rasper_client
95
+ licenses:
96
+ - MIT
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.23
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: JRuby client to RasperServer.
119
+ test_files: []