ig3client 0.4.0 → 0.4.5

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/lib/ig3client.rb CHANGED
@@ -1,181 +1 @@
1
- require 'etc'
2
- require 'net/http'
3
- require 'yaml'
4
- require 'timeout'
5
- require 'rubygems'
6
- require 'facets'
7
- require 'lib/util.rb'
8
- require 'lib/errors.rb'
9
-
10
- # External Options:
11
- #
12
- # ENV doesnt accept true (needs String)
13
- # ENV["IG3TOOL_CACHE"] = "yes"/"no"
14
- #
15
-
16
-
17
- class YAML::Object
18
- def real_id
19
- ivars["attributes"]["id"]
20
- end
21
-
22
- def [](k)
23
- k = k.to_s
24
- if ivars["attributes"].include? k
25
- return ivars["attributes"][k]
26
- elsif ivars.include? k
27
- return ivars[k]
28
- else
29
- keys = ivars['attributes'].keys.join(', ')
30
- warn "No such attribute: #{k.to_s} (#{keys})"
31
- return nil
32
- end
33
- end
34
-
35
- def method_missing(sym,*args)
36
- self[sym]
37
- end
38
- end
39
-
40
- module Ig3tool
41
-
42
- class Client
43
-
44
- attr_accessor :token, :username
45
-
46
- def initialize(host="infogroep.be", port="2007", timeout=0)
47
- @host = host
48
- @port = port
49
- @timeout = timeout # 0 = no timeout.
50
-
51
- begin
52
- @token = File.read(File.expand_path("~/.ig3token")).strip
53
- rescue Exception => e
54
- @token = nil
55
- end
56
-
57
- begin
58
- @username = File.read(File.expand_path("~/.ig3user")).strip
59
- rescue Exception => e
60
- @username = Etc.getpwuid(Process::Sys.geteuid).name
61
- end
62
- end
63
-
64
- def valid_token?
65
- # XXX send no-op to server
66
- false if @token.nil?
67
- begin
68
- self.validate
69
- rescue
70
- return false
71
- end
72
- end
73
-
74
- def request!(h)
75
- wannabe!(h)
76
- end
77
-
78
- def wannabe!(h)
79
- username = h["username"]
80
- passwd = h["password"]
81
- # request token
82
- # if ok => store in file
83
- # else => throw exception
84
- @token = method_missing("wannabe!", {"username" => username,"password" => passwd})
85
-
86
- File.open(File.expand_path("~/.ig3token"), "wb") do |f|
87
- f.write @token
88
- end
89
-
90
- File.open(File.expand_path("~/.ig3user"), "wb") do |f|
91
- f.write @username
92
- end
93
- #rescue Token => t
94
- # raise PermissionDenied
95
- end
96
-
97
-
98
- def method_missing(msg, *args, &block)
99
- msg = msg.to_s
100
-
101
- if match = /^async_(.*)/.match(msg)
102
- raise "Async method called, no block given!" unless block_given?
103
- Thread.new do
104
- ans = self.send(match[1], *args)
105
- block.call(ans)
106
- end
107
- else
108
- uri = "/"+msg.gsub('_','/').sub(/!$/,'')
109
- if msg.ends_with? '!'
110
- args = [Hash[*args]] if args.length % 2 == 0
111
- uri += "//" + @token if @token
112
- req = Net::HTTP::Post.new(uri)
113
- req["Content-Type"] = "text/yaml"
114
- req.body = args.first.to_yaml
115
- else
116
- args = args[0].to_a.flatten if args[0].is_a? Hash
117
- args.unshift(@token) if @token
118
- extra = args.join('/')
119
- extra = '//'+extra unless extra.empty?
120
- req = Net::HTTP::Get.new(uri+extra)
121
- end
122
-
123
-
124
- req["Accept"] = "text/yaml"
125
-
126
-
127
- n = Net::HTTP.new(@host,@port)
128
- Timeout::timeout(@timeout) do
129
- n.request(req) do |res|
130
- rez = YAML::load(res.read_body)
131
- _reraise(rez["type"].to_sym, rez["message"]) if res.code.to_i > 200
132
- if block_given?
133
- yield rez
134
- else
135
- return rez
136
- end
137
- end
138
- end
139
- end
140
- end
141
-
142
- if ENV["IG3TOOL_CACHE"] == "yes"
143
- @@CACHE = {}
144
- EXPIRY = 900 # 15 minuten
145
- BIG_QUERIES = [ :person_debuggers, :person_honorarymembers,
146
- :person_members, :person_nonmembers,
147
- :person_everybody,
148
- :product_categories, :product_all ]
149
-
150
- BIG_QUERIES.each do |sym|
151
- body = Proc.new do |*params|
152
- if params.length > 0
153
- method_missing(sym, *params)
154
- else
155
- entry = @@CACHE[sym]
156
- time = Time.now
157
- if entry.nil? or time > entry[0]
158
- result = method_missing(sym)
159
- @@CACHE[sym] = [time + EXPIRY, result]
160
- result
161
- else
162
- entry[1]
163
- end
164
- end
165
- end
166
-
167
- define_method sym, body
168
- end
169
- end
170
-
171
- private
172
-
173
- def _reraise(sym, msg)
174
- raise constant(sym), msg
175
- rescue NameError
176
- raise "#{sym}: #{msg}"
177
- end
178
-
179
- end
180
-
181
- end
1
+ require 'ig3client/marshal_client.rb'
@@ -0,0 +1,110 @@
1
+ require 'etc'
2
+ require 'net/http'
3
+ require 'timeout'
4
+ require 'rubygems'
5
+ require 'facets'
6
+ require 'lib/util.rb'
7
+ require 'lib/errors.rb'
8
+
9
+ module Ig3tool
10
+
11
+ class GenericClient
12
+
13
+ attr_accessor :token, :username
14
+
15
+ def initialize(host="infogroep.be", port="2007", timeout=0)
16
+ @host = host
17
+ @port = port
18
+ @timeout = timeout # 0 = no timeout.
19
+
20
+ begin
21
+ @token = File.read(File.expand_path("~/.ig3token")).strip
22
+ rescue Exception => e
23
+ @token = nil
24
+ end
25
+
26
+ begin
27
+ @username = File.read(File.expand_path("~/.ig3user")).strip
28
+ rescue Exception => e
29
+ @username = Etc.getpwuid(Process::Sys.geteuid).name
30
+ end
31
+ end
32
+
33
+ def valid_token?
34
+ # XXX send no-op to server
35
+ false if @token.nil?
36
+ begin
37
+ self.validate
38
+ rescue
39
+ return false
40
+ end
41
+ end
42
+
43
+ def request!(h)
44
+ wannabe!(h)
45
+ end
46
+
47
+ def wannabe!(h)
48
+ username = h["username"]
49
+ passwd = h["password"]
50
+ # request token
51
+ # if ok => store in file
52
+ # else => throw exception
53
+ @token = method_missing("wannabe!", {"username" => username,"password" => passwd})
54
+
55
+ File.open(File.expand_path("~/.ig3token"), "wb") do |f|
56
+ f.write @token
57
+ end
58
+
59
+ File.open(File.expand_path("~/.ig3user"), "wb") do |f|
60
+ f.write @username
61
+ end
62
+ #rescue Token => t
63
+ # raise PermissionDenied
64
+ end
65
+
66
+
67
+ def method_missing(msg, *args, &block)
68
+ raise Exception, "this is an abstract class"
69
+ end
70
+
71
+ if ENV["IG3TOOL_CACHE"] == "yes"
72
+ @@CACHE = {}
73
+ EXPIRY = 900 # 15 minuten
74
+ BIG_QUERIES = [ :person_debuggers, :person_honorarymembers,
75
+ :person_members, :person_nonmembers,
76
+ :person_everybody,
77
+ :product_categories, :product_all ]
78
+
79
+ BIG_QUERIES.each do |sym|
80
+ body = Proc.new do |*params|
81
+ if params.length > 0
82
+ method_missing(sym, *params)
83
+ else
84
+ entry = @@CACHE[sym]
85
+ time = Time.now
86
+ if entry.nil? or time > entry[0]
87
+ result = method_missing(sym)
88
+ @@CACHE[sym] = [time + EXPIRY, result]
89
+ result
90
+ else
91
+ entry[1]
92
+ end
93
+ end
94
+ end
95
+
96
+ define_method sym, body
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def _reraise(sym, msg)
103
+ raise constant(sym), msg
104
+ rescue NameError
105
+ raise "#{sym}: #{msg}"
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,91 @@
1
+ require 'ig3client/generic_client.rb'
2
+
3
+ # External Options:
4
+ #
5
+ # ENV doesnt accept true (needs String)
6
+ # ENV["IG3TOOL_CACHE"] = "yes"/"no"
7
+ #
8
+
9
+
10
+
11
+ # Capture serialised IG3Marshal Object
12
+ class IG3Marshal
13
+
14
+ def initialize(attributes)
15
+ @attributes = attributes
16
+ end
17
+
18
+ def real_id
19
+ @attributes["id"]
20
+ end
21
+
22
+ def [](k)
23
+ k = k.to_s
24
+ if @attributes.include? k
25
+ return @attributes[k]
26
+ else
27
+ keys = @attributes.keys.join(', ')
28
+ warn "No such attribute: #{k.to_s} (#{keys})"
29
+ return nil
30
+ end
31
+ end
32
+ def method_missing(sym,*args)
33
+ self[sym]
34
+ end
35
+ end
36
+
37
+
38
+ module Ig3tool
39
+
40
+ class Client < GenericClient
41
+
42
+
43
+ def method_missing(msg, *args, &block)
44
+ msg = msg.to_s
45
+
46
+ if match = /^async_(.*)/.match(msg)
47
+ raise "Async method called, no block given!" unless block_given?
48
+ Thread.new do
49
+ ans = self.send(match[1], *args)
50
+ block.call(ans)
51
+ end
52
+ else
53
+ uri = "/"+msg.gsub('_','/').sub(/!$/,'')
54
+
55
+
56
+ if msg.ends_with? '!'
57
+ args = [Hash[*args]] if args.length % 2 == 0
58
+ uri += "//" + @token if @token
59
+ req = Net::HTTP::Post.new(uri)
60
+ req["Content-Type"] = "text/marshal"
61
+ req.body = Marshal.dump(args.first)
62
+ else
63
+ args = args[0].to_a.flatten if args[0].is_a? Hash
64
+ args.unshift(@token) if @token
65
+ extra = args.join('/')
66
+ extra = '//'+extra unless extra.empty?
67
+ req = Net::HTTP::Get.new(uri+extra)
68
+ end
69
+ req["Accept"] = "text/marshal"
70
+
71
+
72
+ n = Net::HTTP.new(@host,@port)
73
+ Timeout::timeout(@timeout) do
74
+ n.request(req) do |res|
75
+ a = res.read_body
76
+ rez = Marshal.load(a)
77
+ raise rez["type"].to_sym, rez["message"].to_s if (res.code.to_i > 200)
78
+ if block_given?
79
+ yield rez
80
+ else
81
+ return rez
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,82 @@
1
+ require 'yaml'
2
+ require 'ig3client/generic_client.rb'
3
+
4
+
5
+ # Capture serialised YAML Object
6
+ class YAML::Object
7
+ def real_id
8
+ ivars["attributes"]["id"]
9
+ end
10
+
11
+ def [](k)
12
+ k = k.to_s
13
+ if ivars["attributes"].include? k
14
+ return ivars["attributes"][k]
15
+ elsif ivars.include? k
16
+ return ivars[k]
17
+ else
18
+ keys = ivars['attributes'].keys.join(', ')
19
+ warn "No such attribute: #{k.to_s} (#{keys})"
20
+ return nil
21
+ end
22
+ end
23
+
24
+ def method_missing(sym,*args)
25
+ self[sym]
26
+ end
27
+
28
+ end
29
+
30
+
31
+ module Ig3tool
32
+
33
+ class YAMLClient < GenericClient
34
+
35
+ def method_missing(msg, *args, &block)
36
+ msg = msg.to_s
37
+
38
+ if match = /^async_(.*)/.match(msg)
39
+ raise "Async method called, no block given!" unless block_given?
40
+ Thread.new do
41
+ ans = self.send(match[1], *args)
42
+ block.call(ans)
43
+ end
44
+ else
45
+ uri = "/"+msg.gsub('_','/').sub(/!$/,'')
46
+
47
+
48
+ if msg.ends_with? '!'
49
+ args = [Hash[*args]] if args.length % 2 == 0
50
+ uri += "//" + @token if @token
51
+ req = Net::HTTP::Post.new(uri)
52
+ req["Content-Type"] = "text/yaml"
53
+ req.body = args.first.to_yaml
54
+ else
55
+ args = args[0].to_a.flatten if args[0].is_a? Hash
56
+ args.unshift(@token) if @token
57
+ extra = args.join('/')
58
+ extra = '//'+extra unless extra.empty?
59
+ req = Net::HTTP::Get.new(uri+extra)
60
+ end
61
+ req["Accept"] = "text/yaml"
62
+
63
+
64
+ n = Net::HTTP.new(@host,@port)
65
+ Timeout::timeout(@timeout) do
66
+ n.request(req) do |res|
67
+ rez = YAML::load(res.read_body)
68
+ _reraise(rez["type"].to_sym, rez["message"]) if res.code.to_i > 200
69
+ if block_given?
70
+ yield rez
71
+ else
72
+ return rez
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ end
81
+
82
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ig3client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Pinte
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-25 00:00:00 +02:00
12
+ date: 2008-04-30 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,8 +30,10 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
 
32
32
  files:
33
- - lib/lib
34
33
  - lib/ig3client.rb
34
+ - lib/ig3client/generic_client.rb
35
+ - lib/ig3client/marshal_client.rb
36
+ - lib/ig3client/yaml_client.rb
35
37
  - lib/lib/errors.rb
36
38
  - lib/lib/util.rb
37
39
  has_rdoc: false
@@ -42,7 +44,7 @@ rdoc_options: []
42
44
  require_paths:
43
45
  - lib
44
46
  - lib
45
- - lib/ui
47
+ - lib/ig3client
46
48
  required_ruby_version: !ruby/object:Gem::Requirement
47
49
  requirements:
48
50
  - - ">="