ig3client 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.
Files changed (4) hide show
  1. data/lib/ig3client.rb +161 -0
  2. data/lib/lib/errors.rb +90 -0
  3. data/lib/lib/util.rb +89 -0
  4. metadata +63 -0
data/lib/ig3client.rb ADDED
@@ -0,0 +1,161 @@
1
+ require 'lib/util.rb'
2
+ require 'lib/errors.rb'
3
+ require 'net/http'
4
+ require 'yaml'
5
+ require 'timeout'
6
+ require 'rubygems'
7
+ require 'facets'
8
+
9
+ # External Options:
10
+ #
11
+ # ENV doesnt accept true (needs String)
12
+ # ENV["IG3TOOL_CACHE"] = "yes"/"no"
13
+ #
14
+
15
+
16
+ class YAML::Object
17
+ def real_id
18
+ ivars["attributes"]["id"]
19
+ end
20
+
21
+ def [](k)
22
+ k = k.to_s
23
+ if ivars["attributes"].include? k
24
+ return ivars["attributes"][k]
25
+ elsif ivars.include? k
26
+ return ivars[k]
27
+ else
28
+ keys = ivars['attributes'].keys.join(', ')
29
+ warn "No such attribute: #{k.to_s} (#{keys})"
30
+ return nil
31
+ end
32
+ end
33
+
34
+ def method_missing(sym,*args)
35
+ self[sym]
36
+ end
37
+ end
38
+
39
+ module Ig3tool
40
+
41
+ class Client
42
+
43
+ attr_accessor :token
44
+
45
+ def initialize(host="infogroep.be", port="2007", timeout=0)
46
+ @host = host
47
+ @port = port
48
+ @timeout = timeout # 0 = no timeout.
49
+
50
+ begin
51
+ @token = File.read(File.expand_path("~/.ig3token")).strip
52
+ rescue Exception => e
53
+ @token = nil
54
+ end
55
+ end
56
+
57
+ def valid_token?
58
+ # XXX send no-op to server
59
+ false if @token.nil?
60
+ begin
61
+ self.validate
62
+ rescue
63
+ return false
64
+ end
65
+ end
66
+
67
+ def request!(h)
68
+ wannabe!(h)
69
+ end
70
+
71
+ def wannabe!(h)
72
+ username = h["username"]
73
+ passwd = h["password"]
74
+ # request token
75
+ # if ok => store in file
76
+ # else => throw exception
77
+ @token = method_missing("wannabe!", {"username" => username,"password" => passwd})
78
+ f = File.new(File.expand_path("~/.ig3token"), "wb")
79
+ f.write @token
80
+ f.close
81
+ #rescue Token => t
82
+ # raise PermissionDenied
83
+ end
84
+
85
+
86
+ def method_missing(msg, *args, &block)
87
+ msg = msg.to_s
88
+
89
+
90
+ if match = /^async_(.*)/.match(msg)
91
+ raise "Async method called, no block given!" unless block_given?
92
+ Thread.new do
93
+ ans = self.send(match[1], *args)
94
+ block.call(ans)
95
+ end
96
+ else
97
+ uri = "/"+msg.gsub('_','/').sub(/!$/,'')
98
+ if msg.ends_with? '!'
99
+ args = [Hash[*args]] if args.length % 2 == 0
100
+ uri += "//" + @token if @token
101
+ req = Net::HTTP::Post.new(uri)
102
+ req["Content-Type"] = "text/yaml"
103
+ req.body = args.first.to_yaml
104
+ else
105
+ args = args[0].to_a.flatten if args[0].is_a? Hash
106
+ args.unshift(@token) if @token
107
+ extra = args.join('/')
108
+ extra = '//'+extra unless extra.empty?
109
+ req = Net::HTTP::Get.new(uri+extra)
110
+ end
111
+
112
+
113
+ req["Accept"] = "text/yaml"
114
+
115
+
116
+ n = Net::HTTP.new(@host,@port)
117
+ Timeout::timeout(@timeout) do
118
+ n.request(req) do |res|
119
+ rez = YAML::load(res.read_body)
120
+ raise rez["type"].to_sym.to_const, rez["message"].to_s if (res.code.to_i > 200)
121
+ if block_given?
122
+ yield rez
123
+ else
124
+ return rez
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ if ENV["IG3TOOL_CACHE"] == "yes"
132
+ @@CACHE = {}
133
+ EXPIRY = 900 # 15 minuten
134
+ BIG_QUERIES = [ :person_debuggers, :person_honorarymembers,
135
+ :person_members, :person_nonmembers,
136
+ :person_everybody,
137
+ :product_categories, :product_all ]
138
+
139
+ BIG_QUERIES.each do |sym|
140
+ body = Proc.new do |*params|
141
+ if params.length > 0
142
+ method_missing(sym, *params)
143
+ else
144
+ entry = @@CACHE[sym]
145
+ time = Time.now
146
+ if entry.nil? or time > entry[0]
147
+ result = method_missing(sym)
148
+ @@CACHE[sym] = [time + EXPIRY, result]
149
+ result
150
+ else
151
+ entry[1]
152
+ end
153
+ end
154
+ end
155
+
156
+ define_method sym, body
157
+ end
158
+ end
159
+ end
160
+
161
+ end
data/lib/lib/errors.rb ADDED
@@ -0,0 +1,90 @@
1
+ module Ig3tool
2
+
3
+ class IG3Error < Exception
4
+ end
5
+
6
+ class Token < IG3Error
7
+ end
8
+
9
+ class ProductNotFound < IG3Error
10
+ end
11
+
12
+ class PermissionDenied < IG3Error
13
+ end
14
+
15
+ class NeedDebugger < IG3Error
16
+ end
17
+
18
+ class NotADebugger < IG3Error
19
+ end
20
+
21
+ class NotAMember < IG3Error
22
+ end
23
+
24
+ class Needed < IG3Error
25
+ end
26
+
27
+ class NotFound < IG3Error
28
+ end
29
+
30
+ class WrongRequestType < IG3Error
31
+ end
32
+
33
+
34
+ # PRINTING
35
+
36
+ class PrintBackend < IG3Error
37
+ end
38
+
39
+ class NoPrintAccount < IG3Error
40
+ end
41
+
42
+ class NotEnoughCredit < IG3Error
43
+ end
44
+
45
+ class WrongPrintlogType < IG3Error
46
+ end
47
+
48
+ class SaveFailed < IG3Error
49
+ end
50
+
51
+ class AliasNotFound < IG3Error
52
+ end
53
+
54
+ class TransactionNotFound < IG3Error
55
+ end
56
+
57
+ class PrintUserNotFound < IG3Error
58
+ end
59
+
60
+ # INTERNE
61
+
62
+ class TransactionLogFailed < IG3Error
63
+ end
64
+
65
+ class NoPositiveAmount < IG3Error
66
+ end
67
+
68
+ class TransactionFailed < IG3Error
69
+ end
70
+
71
+ class SaldoNotZero < IG3Error
72
+ end
73
+
74
+ class Afgesloten < IG3Error
75
+ end
76
+
77
+ # BIB
78
+
79
+ class StillHaveLoans < IG3Error
80
+ end
81
+
82
+ class BibSectionNotFound < IG3Error
83
+ end
84
+
85
+ class InvalidISBN < IG3Error
86
+ end
87
+
88
+ class BookNotAvaible < IG3Error
89
+ end
90
+ end
data/lib/lib/util.rb ADDED
@@ -0,0 +1,89 @@
1
+ module Gtk
2
+ class TextView
3
+ def text
4
+ buffer.text
5
+ end
6
+
7
+ def text=(str)
8
+ buffer.text = str
9
+ end
10
+ end
11
+ end
12
+
13
+ class String
14
+
15
+ def starts_with?(str)
16
+ self.index( str ) == 0
17
+ end
18
+
19
+ def ends_with?(str)
20
+ self.rindex( str ) == self.length - str.length
21
+ end
22
+
23
+ def to_c
24
+ self.to_f.to_c
25
+ end
26
+
27
+ def from_c
28
+ self.to_f.from_c
29
+ end
30
+
31
+ def smaller(l=75)
32
+ if self.size > l
33
+ self[0..l] + "..."
34
+ else
35
+ self
36
+ end
37
+ end
38
+ end
39
+
40
+ class Numeric
41
+ def to_c
42
+ (self.to_f * 100).round.to_i
43
+ end
44
+
45
+ def from_c
46
+ (self.to_f / 100)
47
+ end
48
+
49
+ def to_b
50
+ return (not (self.zero?))
51
+ end
52
+ end
53
+
54
+ class Hash
55
+ # File facets/hash/new.rb, line 44
56
+ def self.zipnew(keys,values) # or some better name
57
+ h = {}
58
+ keys.size.times{ |i| h[ keys[i] ] = values[i] }
59
+ h
60
+ end
61
+ # File facets/hash/keyize.rb, line 49
62
+ def normalize_keys!( &block )
63
+ keys.each{ |k|
64
+ nk = block[k]
65
+ self[nk]=delete(k) if nk
66
+ }
67
+ self
68
+ end
69
+ end
70
+
71
+ class Time
72
+ # bepaling van de week van het academiejaar
73
+ def week
74
+ t = start_werkjaar(Time.werkjaar)
75
+ return 1 + ((self - t) / (86400*7)).to_int
76
+ end
77
+
78
+ # we bepalen de start van het werkjaar (laatste ma van sept)
79
+ def self.start_werkjaar(jaar)
80
+ t = Time.mktime(jaar, 9, 30)
81
+ t = t - 86400 until t.wday == 1
82
+ t
83
+ end
84
+
85
+ def self.werkjaar
86
+ t = Time.now()
87
+ t > start_werkjaar(t.year) ? t.year : t.year - 1
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ig3client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Pinte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: facets
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.1
23
+ version:
24
+ description:
25
+ email: igtool@infogroep.be
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/ig3client.rb
34
+ - lib/lib/errors.rb
35
+ - lib/lib/util.rb
36
+ has_rdoc: false
37
+ homepage: http://infogroep.be
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.0.1
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: base client library of ig3tool
62
+ test_files: []
63
+