Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
InstalledFiles
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
# YARD artifacts
.yardoc
_yardoc
doc/
53 changes: 0 additions & 53 deletions .rvmrc

This file was deleted.

35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PATH
remote: .
specs:
jaspersoft (0.0.3)
sawyer (~> 0.5.1)

GEM
remote: https://rubygems.org/
specs:
addressable (2.3.5)
diff-lcs (1.2.3)
faraday (0.8.8)
multipart-post (~> 1.2.0)
multipart-post (1.2.0)
rake (10.0.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
sawyer (0.5.1)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.3)
jaspersoft!
rake
rspec
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Jaspersoft

TODO: Write a gem description
A Ruby implementation of the v2 REST API for Jaspersoft Server

## Installation

Expand All @@ -26,4 +26,4 @@ TODO: Write usage instructions here
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
5. Create new Pull Request
8 changes: 4 additions & 4 deletions jaspersoft.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ require 'jaspersoft/version'
Gem::Specification.new do |spec|
spec.name = "jaspersoft"
spec.version = Jaspersoft::VERSION
spec.authors = ["Peter Cunningham"]
spec.email = ["peter@refreshmedia.com"]
spec.authors = ["Peter Cunningham", "Darin Richardson"]
spec.email = ["peter@refreshmedia.com", "darin@refreshmedia.com"]
spec.description = %q{Access and process Jaspersoft Reports.}
spec.summary = %q{Access list of available reports and process/view available reports.}
spec.homepage = ""
spec.homepage = "https://github.com/oldbonsai/jaspersoft"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "faraday"
spec.add_dependency 'sawyer', '~> 0.5.1'
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down
2 changes: 1 addition & 1 deletion lib/jaspersoft.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "jaspersoft/version"
require "jaspersoft/configuration"
require "jaspersoft/client"
require "jaspersoft/error"

module Jaspersoft
extend Configuration


def new(options={})
Client.new(options)
Expand Down
45 changes: 45 additions & 0 deletions lib/jaspersoft/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Jaspersoft

module Authentication

def basic_authenticated?
!!(username && password)
end

def session_authenticated?
!!(session)
end

def user_authenticated?
basic_authenticated? || token_authenticated?
end

def login
if session_authenticated?
authenticate_with_session
else
authenticate_with_basic
end
end

private

def authenticate_with_basic
basic_agent = new_agent{ |http| http.basic_auth(username, password) }
response = basic_agent.call(:post, URI.encode("#{ endpoint_url(v2: false) }/login/".to_s), { content_type: "application/x-www-form-urlencoded", j_username: username, j_password: password })

if response && response.status == 200 && response.headers["set-cookie"] != ""
self.session = response.headers["set-cookie"].match(/jsessionid=(.+?);/i)[1]
authenticate_with_session
else
raise AuthenticationError
end
end

def authenticate_with_session
@agent = new_agent{ |http| http.headers[:Cookie] = "$Version=0; JSESSIONID=#{session}; $Path=/jasperserver" }
end

end

end
55 changes: 43 additions & 12 deletions lib/jaspersoft/client.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
# lib/jaspersoft/client.rb
require 'sawyer'
require 'base64'
require 'jaspersoft/authentication'
require 'jaspersoft/configuration'
require 'jaspersoft/connection'
require 'jaspersoft/request'
require 'jaspersoft/client/input_controls'
require 'jaspersoft/client/reports'
require 'jaspersoft/client/resources'

module Jaspersoft

class Client
attr_accessor *Configuration::VALID_CONFIG_KEYS

def initialize(options={})
include Jaspersoft::Configuration
include Jaspersoft::Connection
include Jaspersoft::Request
include Jaspersoft::Authentication
include Jaspersoft::Client::InputControls
include Jaspersoft::Client::Reports
include Jaspersoft::Client::Resources

def initialize(options = {})
merged_options = Jaspersoft.options.merge(options)

Configuration::VALID_CONFIG_KEYS.each do |key|
send("#{key}=", merged_options[key])
end

login
end

def login(options)
params = prepare_request_params(options)

end


private

# Formats paths for folders and files
#
# @param path [String] Path to file or folers
# @option option [Boolean] :leading_slath Prepend a slash
# @option option [Boolean] :trailing_slath Append a slash
def normalize_path_slashes(path, options = {})
options = { leading_slash: false, trailing_slash: false }.merge(options)

if path != ""
if options[:leading_slash] == true
path = "/" + path.to_s if path[0] != "/"
else
path = path[1..-1] if path[0] == "/"
end
if options[:trailing_slash] == true
path = path + "/" if path[-1] != "/"
else
path = path[0..-2] if path[-1] == "/"
end
end

def prepare_request_params(options)

path
end

end
Expand Down
16 changes: 16 additions & 0 deletions lib/jaspersoft/client/input_controls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Jaspersoft
class Client
module InputControls

# Get a list of all the input controls for a given report
#
# @param reports [String] A path to a report
# @return [Array<Sawyer::Resource>] An array of input controls resources
def input_controls(path, params = {}, options = {})
response = get "#{endpoint_url}/reports/#{normalize_path_slashes(path)}/inputControls/", params, options
response.inputControl
end

end
end
end
98 changes: 98 additions & 0 deletions lib/jaspersoft/client/reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module Jaspersoft
class Client
module Reports

# Get a list of all reports for the optional path. Note this is can be very taxing because it is recursively checking all subfolders from provided path
#
# @param reports [String] A path to a report folder
# @option opts [Boolean] :recursive Search the folder recursively
# @return [Array<Sawyer::Resource>] An array of report resources
def reports(path = nil, params = {}, options = {})
params = { recursive: true }.merge(params)
params[:type] = 'reportUnit'

resources(path, params, options)
end

# Get a single report object
#
# @param path [String] A full path to a specific report
# @return [<Sawyer::Resource>] A sinlge report resource
def report(path, params = {}, options = {})
params = { content_type: "application/repository.reportUnit+json" }.merge(params)

get "#{endpoint_url}/resources#{normalize_path_slashes(path, leading_slash: true)}", params, options
end
alias :find_report :report

# Starts generating a report
#
# @param path [String] A full path to a specific report
# @option opts [String] :file_type
# @option opts [Hash] :params A hash of key/value pairs matching with the input controls defined for the report
# @return [String] A request ID
def enqueue_report(path, params = {}, options = {})
params = { file_type: report_file_type.to_s, params: {} }.merge(params)
params[:outputFormat] = params.delete(:file_type)
params[:parameters] = convert_report_params params.delete(:params)
params[:reportUnitUri] = path
params[:async] = true
params[:interactive] = false

response = post "#{endpoint_url}/reportExecutions", params, options
if response && response.requestId
return response.requestId
else
return false # TODO: Error handling
end
end
alias :enqueue :enqueue_report

# Polls the report execution status of a report
#
# @param request_id [String] A report ID, usually in the form of #####-#####-## (with varying digit counts in each group)
# @return [String] The execution status of the report
def poll_report(request_id, params = {}, options = {})
options[:raw_response] = true

response = get "#{endpoint_url}/reportExecutions/#{request_id}/status/", params, options
if response.status == 200
return response.data.value
else
return "not found"
end
end
alias :poll :poll_report

# Retrieve the formats of a finished report and download the primary format if the report is ready
#
# @param request_id [String] A report ID, usually in the form of #####-#####-## (with varying digit counts in each group)
# @return [File] Raw binary of the first format available
def download_report(request_id, params = {}, options = {})
report_response = get "#{endpoint_url}/reportExecutions/#{request_id}", params, options
if report_response.status == "ready"
format = report_response.exports[0].id # TODO: Accept options for which format to grab, verify it's available
get "#{endpoint_url}/reportExecutions/#{request_id}/exports/#{format}/outputResource", params, options
else
return false
end
end
alias :download :download_report

private

# Convert a normal looking hash into the format that Jaspersoft expects for JSON arguments
#
# @param params [Hash] Keys can be either symbols or strings. Values can be an array or a single value.
# @example
# convert_params(school_id: 1234, start_date: "2011-01-01") # => { reportParameter: [ { name: "school_id", value: ["7236"] }, { name: "start_date", value: ["2011-01-01"] } ] }
def convert_report_params(params)
converted_params = { reportParameter: [] }
params.each{ |key, value| converted_params[:reportParameter] << { name: key.to_s, value: (value.is_a? Array) ? value : [ value ] } }
converted_params
end

end

end
end
Loading