lib 0.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lib.rb +200 -3
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a6c5895b5d8e06b57069bee3335ba038afbcda7
4
- data.tar.gz: b73b5befb6c51fcd503f2fe7a2719aa46eb38a57
3
+ metadata.gz: f4be579ab369a262f0df68a28b1a05b5f7dc1c37
4
+ data.tar.gz: c108730ba7360638a479c080b22c67c6f1a43435
5
5
  SHA512:
6
- metadata.gz: cb27d8330cfb705c51f846f5b9f644d795da2f78eb706a18c2567f44c040c183d9760a5f1193e1104161bce07c53bac75ed5aeb5f3e55670f91a84b7e510fd41
7
- data.tar.gz: c216ba764542ca8ed5590a8704fa29405e58e678afe2702b21fb403b1df6aba455fbda1d986b094b84de96a2a84a842f276b9f668801d47868c2ccc0e6a46107
6
+ metadata.gz: 0935e70df15a875deecf68a5eb3fa34626bd0af571054c33d24f25e620b22c06c83e1a639db0b7a4b5a60bff1f63f82dc5de8195a927f1eaf0d50176d2dd91a7
7
+ data.tar.gz: 881f80b62f72980ca0bf58f015c1ad594cce3b054a2032c317dd97e35b73300df13a0a82800ae193faccf644b284a140f87fd756c0e31172cbc74de85992ce34
data/lib/lib.rb CHANGED
@@ -1,5 +1,202 @@
1
- class Lib
2
- def self.lib
3
- puts "Lib Placeholder"
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module Lib
5
+
6
+ def self.[](name)
7
+ self.method_missing(name)
4
8
  end
9
+
10
+ def self.method_missing(name)
11
+ libGen = LibGen.new({})
12
+ libGen[name]
13
+ end
14
+
15
+ class LibGen
16
+
17
+ @@HOST = 'f.stdlib.com'
18
+ @@PORT = 443
19
+ @@PATH = '/'
20
+
21
+ def initialize(cfg={}, names=Array.new)
22
+ cfg[:host] = @@HOST unless cfg.has_key? :host
23
+ cfg[:port] = @@PORT unless cfg.has_key? :port
24
+ cfg[:path] = @@PATH unless cfg.has_key? :path
25
+ @cfg = cfg
26
+ @names = names
27
+ end
28
+
29
+ def to_str()
30
+ @names.join('.')
31
+ end
32
+
33
+ def __append_version__(names, str)
34
+
35
+ if /^@[A-Z0-9\-\.]+$/i !~ str then
36
+ raise StandardError, "#{names.join('.')} invalid version: #{str}"
37
+ end
38
+
39
+ return names + [str];
40
+
41
+ end
42
+
43
+ def __append_path__(names, str)
44
+
45
+ if /^[A-Z0-9\-]+$/i !~ str then
46
+
47
+ if str.include? '@' then
48
+ raise StandardError, "#{names.join('.')} invalid name: #{str}, please specify versions and environments with [@version]"
49
+ end
50
+
51
+ raise StandardError, "#{names.join('.')} invalid name: #{str}"
52
+
53
+ end
54
+
55
+ return names + [str]
56
+
57
+ end
58
+
59
+ def __append_lib_path__(names, str)
60
+
61
+ names = if names.length then names + [] else [] end
62
+ default_version = '@release'
63
+
64
+ if names.length === 0 && str === '' then
65
+
66
+ return names + [str]
67
+
68
+ elsif names.length === 0 && (str.include? '.') then
69
+
70
+ versionMatch = str.match /^[^\.]+?\.[^\.]*?(\[@[^\[\]]*?\])(\.|$)/
71
+ arr = []
72
+
73
+ if versionMatch then
74
+ version = versionMatch[1]
75
+ version = version.gsub /^\[?(.*?)\]?$/, '\1'
76
+ str = str.gsub versionMatch[1], ''
77
+ arr = str.split '.'
78
+ arr = arr[0...2] + [version] + (arr[2..-1] || [])
79
+ else
80
+ arr = if str == '.' then [''] else str.split '.' end
81
+ end
82
+
83
+ while arr.length > 0 do
84
+ names = __append_lib_path__(names, arr.shift)
85
+ end
86
+
87
+ return names
88
+
89
+ elsif names.length === 2 && names[0] != '' then
90
+
91
+ if str[0] === '@' then
92
+
93
+ return __append_version__(names, str)
94
+
95
+ else
96
+
97
+ return __append_path__(__append_version__(names, default_version), str)
98
+
99
+ end
100
+
101
+ else
102
+
103
+ return __append_path__(names, str)
104
+
105
+ end
106
+
107
+ end
108
+
109
+ def [](name)
110
+ self.method_missing(name)
111
+ end
112
+
113
+ def method_missing(name)
114
+ LibGen.new(@cfg, __append_lib_path__(@names, name.to_s))
115
+ end
116
+
117
+ def exec!(*args)
118
+
119
+ names = @names
120
+ name = names[0...2].join('/') + (if names[2..-1] then names[2..-1].join('/') else '' end)
121
+ kwargs = if args[-1].is_a? ::Hash then args.pop else {} end
122
+ is_local = names[0].empty?
123
+ response = nil
124
+
125
+ begin
126
+
127
+ args.each do |v|
128
+ if v != nil and
129
+ not v.is_a? ::TrueClass and
130
+ not v.is_a? ::FalseClass and
131
+ not v.is_a? ::String and
132
+ not v.is_a? ::Fixnum and
133
+ not v.is_a? ::Float and
134
+ not v.is_a? ::Bignum then
135
+ raise ArgumentError, "Lib.#{names.join('.')}: All arguments must be Boolean, Number, String or nil", caller[2..-1]
136
+ end
137
+ end
138
+
139
+ if is_local then
140
+ raise StandardError, "StdLib local execution currently unavailable in Ruby", caller
141
+ end
142
+
143
+ body = JSON.generate({args: args, kwargs: kwargs})
144
+
145
+ https = Net::HTTP.new(@cfg[:host], @cfg[:port])
146
+ https.use_ssl = true
147
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
148
+ req = Net::HTTP::Post.new "#{@cfg[:path]}#{name}"
149
+ req.add_field 'Content-Type', 'application/json'
150
+ req.body = body
151
+ res = https.request req
152
+
153
+ headers = {}
154
+ status = res.code.to_i
155
+ res.each_header { |header, value| headers[header.downcase] = value }
156
+ contentType = headers['content-type']
157
+ response = res.body
158
+
159
+ if contentType === 'application/json' then
160
+ response = response.to_s
161
+ begin
162
+ response = JSON.parse(response);
163
+ rescue
164
+ response = nil
165
+ end
166
+ elsif contentType =~ /^text\/.*$/i then
167
+ response = response.to_s
168
+ end
169
+
170
+ if status / 100 != 2 then
171
+ raise StandardError, "Lib.#{names.join('.')}: #{response}", caller
172
+ end
173
+
174
+ rescue Exception => e
175
+
176
+ if block_given? then
177
+ yield e, nil
178
+ return
179
+ else
180
+ raise e
181
+ end
182
+
183
+ end
184
+
185
+ if block_given? then
186
+ yield nil, response
187
+ return
188
+ else
189
+ return response
190
+ end
191
+
192
+ end
193
+
194
+ private :__append_version__
195
+ private :__append_path__
196
+ private :__append_lib_path__
197
+
198
+ end
199
+
200
+ private_constant :LibGen
201
+
5
202
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Horwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-19 00:00:00.000000000 Z
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Access microservices from stdlib easily
13
+ description: 'StdLib: Standard Library for Microservices Ruby Bindings'
14
14
  email: keithwhor@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -40,5 +40,5 @@ rubyforge_project:
40
40
  rubygems_version: 2.0.14.1
41
41
  signing_key:
42
42
  specification_version: 4
43
- summary: stdlib - Function as a Service SDK
43
+ summary: 'StdLib: Standard Library for Microservices Ruby Bindings'
44
44
  test_files: []