elibri_watermarking 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in elibri_watermarking_client.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "elibri_watermarking/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "elibri_watermarking"
7
+ s.version = ElibriWatermarking::VERSION
8
+ s.authors = ["Piotr Szmielew"]
9
+ s.email = ["p.szmielew@ava.waw.pl"]
10
+ s.homepage = "http://elibri.com.pl"
11
+ s.summary = %q{Gem designed to help in use of Elibri watermarking API.}
12
+ s.description = %q{Gem designed to help in use of Elibri watermarking API.}
13
+
14
+ s.rubyforge_project = "elibri_watermarking"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rake"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,7 @@
1
+ require "elibri_watermarking/version"
2
+ require "elibri_watermarking/exceptions"
3
+ require "elibri_watermarking/client"
4
+
5
+ module ElibriWatermarking
6
+
7
+ end
@@ -0,0 +1,59 @@
1
+ require 'net/http'
2
+ require 'digest/md5'
3
+
4
+ module ElibriWatermarking
5
+ class Client
6
+
7
+ attr_accessor :token, :secret, :url
8
+
9
+ def initialize(token, secret, url='https://elibri.com.pl/watermarking')
10
+ self.token = token
11
+ self.secret = secret
12
+ self.url = url
13
+ end
14
+
15
+ def watermark(ident, formats, visible_watermark, title_postfix)
16
+ ident =~ /^[0-9]+$/ ? ident_type = 'isbn' : ident_type = 'record_reference'
17
+ raise WrongFormats.new if formats.is_a?(String) && !formats =~ /^(epub|mobi|,)+$/
18
+ raise WrongFormats.new if formats.is_a?(Array) && (formats != ['epub','mobi'] && formats != ['mobi','epub'] && formats != ['mobi'] && formats != ['epub'])
19
+ uri = URI(self.url + '/watermark')
20
+ formats = formats.join(",") if formats.is_a?(Array)
21
+ timestamp = Time.now.to_i
22
+ sig = Digest::MD5.hexdigest("#{self.secret}_#{timestamp}")
23
+ res = Net::HTTP.post_form(uri, ident_type => ident, 'formats' => formats, 'visible_watermark' => visible_watermark,
24
+ 'title_postfix' => title_postfix, 'stamp' => timestamp, 'sig' => sig, 'token' => self.token)
25
+ return validate_response(res)
26
+ end
27
+
28
+ def deliver(trans_id)
29
+ uri = URI(self.url + '/deliver')
30
+ timestamp = Time.now.to_i
31
+ sig = Digest::MD5.hexdigest("#{self.secret}_#{timestamp}")
32
+ res = Net::HTTP.post_form(uri, 'stamp' => timestamp, 'sig' => sig, 'token' => self.token, 'trans_id' => trans_id)
33
+ return validate_response(res)
34
+ end
35
+
36
+ def watermark_and_deliver(ident, formats, visible_watermark, title_postfix)
37
+ trans_id = watermark(ident, formats, visible_watermark, title_postfix)
38
+ return trans_id if deliver(trans_id) == "OK"
39
+ end
40
+
41
+ protected
42
+
43
+ def validate_response(res)
44
+ case res.class.to_s
45
+ when "Net::HTTPBadRequest"
46
+ raise ParametersError.new
47
+ when "Net::HTTPUnauthorized"
48
+ raise AuthenticationError.new
49
+ when "Net::HTTPForbidden"
50
+ raise AuthorizationError.new
51
+ when "Net::HTTPInternalServerError"
52
+ raise ServerException.new
53
+ when "Net::HTTPOK"
54
+ return res.body
55
+ end
56
+ return res.body
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ module ElibriWatermarking
2
+
3
+ class ElibriException < Exception
4
+ end
5
+
6
+ class ParametersError < ElibriException
7
+ end
8
+
9
+ class AuthenticationError < ElibriException
10
+ end
11
+
12
+ class AuthorizationError < ElibriException
13
+ end
14
+
15
+ class ServerException < ElibriException
16
+ end
17
+
18
+ class WrongFormats < ElibriException
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module ElibriWatermarking
2
+ VERSION = "0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elibri_watermarking
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Piotr Szmielew
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2012-06-23 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ type: :development
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ segments:
26
+ - 0
27
+ version: "0"
28
+ name: rake
29
+ requirement: *id001
30
+ prerelease: false
31
+ description: Gem designed to help in use of Elibri watermarking API.
32
+ email:
33
+ - p.szmielew@ava.waw.pl
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ files:
41
+ - .gitignore
42
+ - Gemfile
43
+ - Rakefile
44
+ - elibri_watermarking.gemspec
45
+ - lib/elibri_watermarking.rb
46
+ - lib/elibri_watermarking/client.rb
47
+ - lib/elibri_watermarking/exceptions.rb
48
+ - lib/elibri_watermarking/version.rb
49
+ has_rdoc: true
50
+ homepage: http://elibri.com.pl
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: elibri_watermarking
75
+ rubygems_version: 1.3.6
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Gem designed to help in use of Elibri watermarking API.
79
+ test_files: []
80
+