microkit-ruby-sdk 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/microkit/config.rb +40 -0
- data/lib/microkit/config_item.rb +4 -0
- data/lib/microkit/feature_item.rb +19 -0
- data/lib/microkit/group.rb +66 -0
- data/lib/microkit/http_client.rb +35 -0
- data/lib/microkit/item.rb +37 -0
- data/lib/microkit/microkit.rb +81 -0
- data/lib/microkit/network_controller.rb +30 -0
- data/lib/microkit/publisher.rb +27 -0
- data/lib/microkit/version.rb +3 -0
- data/lib/microkit.rb +15 -0
- data/microkit-ruby-sdk.gemspec +42 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5ea86c15b4480abad502e8dbb1ca3ae577f16ec070ee2c49687e0726e2724240
|
4
|
+
data.tar.gz: e3eef504391cd55d517d50670ff5c546a413135ef7e4c0ec6c799b357d3eff78
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 793a28b659b08da186cd16d2ae24215f08570e914680a45895204b0e8a2ecbd066c4563cb4dbf3fed982d9866f4bd754ed996e4eedb2bd669275a14833dd0484
|
7
|
+
data.tar.gz: f88d76e930b1975db7b297d9b4a3ecfbb7a17833acdd601b085bc184da01aa3030d8f7929475c480f44a7820509bef642ea68c84af6972e4b53fd931cfee1988
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# These are some examples of commonly ignored file patterns.
|
2
|
+
# You should customize this list as applicable to your project.
|
3
|
+
# Learn more about .gitignore:
|
4
|
+
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore
|
5
|
+
|
6
|
+
# Node artifact files
|
7
|
+
node_modules/
|
8
|
+
dist/
|
9
|
+
|
10
|
+
# Compiled Java class files
|
11
|
+
*.class
|
12
|
+
|
13
|
+
# Compiled Python bytecode
|
14
|
+
*.py[cod]
|
15
|
+
|
16
|
+
# Log files
|
17
|
+
*.log
|
18
|
+
|
19
|
+
# Package files
|
20
|
+
*.jar
|
21
|
+
|
22
|
+
# Maven
|
23
|
+
target/
|
24
|
+
dist/
|
25
|
+
|
26
|
+
# JetBrains IDE
|
27
|
+
.idea/
|
28
|
+
|
29
|
+
# Unit test reports
|
30
|
+
TEST*.xml
|
31
|
+
|
32
|
+
# Generated by MacOS
|
33
|
+
.DS_Store
|
34
|
+
|
35
|
+
# Generated by Windows
|
36
|
+
Thumbs.db
|
37
|
+
|
38
|
+
# Applications
|
39
|
+
*.app
|
40
|
+
*.exe
|
41
|
+
*.war
|
42
|
+
|
43
|
+
# Large media files
|
44
|
+
*.mp4
|
45
|
+
*.tiff
|
46
|
+
*.avi
|
47
|
+
*.flv
|
48
|
+
*.mov
|
49
|
+
*.wmv
|
50
|
+
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at chaim@daneltech.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
microkit-ruby-sdk (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.5.0)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.12.0)
|
12
|
+
rspec-core (~> 3.12.0)
|
13
|
+
rspec-expectations (~> 3.12.0)
|
14
|
+
rspec-mocks (~> 3.12.0)
|
15
|
+
rspec-core (3.12.0)
|
16
|
+
rspec-support (~> 3.12.0)
|
17
|
+
rspec-expectations (3.12.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.12.0)
|
20
|
+
rspec-mocks (3.12.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.12.0)
|
23
|
+
rspec-support (3.12.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.17)
|
30
|
+
microkit-ruby-sdk!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 chaimvaid
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Microkit::Ruby::Sdk
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/microkit/ruby/sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'microkit-ruby-sdk'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install microkit-ruby-sdk
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/microkit-ruby-sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Microkit::Ruby::Sdk project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/microkit-ruby-sdk/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "microkit"
|
5
|
+
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require "singleton"
|
3
|
+
module Microkit
|
4
|
+
class Config
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def init (options)
|
8
|
+
@config = {"version" => "v1"}
|
9
|
+
@config["base_url"] = options.key?("base_url") ? options["base_url"] : "sdk.microkit.app"
|
10
|
+
@config["api_key"] = self.validate_require(options, "api_key")
|
11
|
+
@config["port"] = options.key?("port") ? options["port"] : nil
|
12
|
+
@config["http"] = options.key?("http") ? options["http"] : false
|
13
|
+
@config["polling_interval"] = options.key?("polling_interval") ? options["polling_interval"] : 30
|
14
|
+
@config["service"] = options.key?("service") ? options["service"] : ""
|
15
|
+
@config["polling_on"] = options.key?("polling_on") ? options["polling_on"] : true
|
16
|
+
@config["user"] = options.key?("user") ? options["user"] : {}
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def get(conf_name)
|
21
|
+
if @config.key?(conf_name) then
|
22
|
+
return @config[conf_name]
|
23
|
+
else
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def set(key, value)
|
29
|
+
@config[key] = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate_require(options, value_name)
|
33
|
+
if options[value_name] then
|
34
|
+
return options[value_name]
|
35
|
+
else
|
36
|
+
raise Error.new("#{value_name} is required.")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Microkit
|
2
|
+
class FeatureItem < Item
|
3
|
+
def initialize(value, name)
|
4
|
+
super(value, name)
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_value (user = nil)
|
8
|
+
if !user or !@item["targeting_groups"] or @item["targeting_groups"].length == 0 then
|
9
|
+
return self.value
|
10
|
+
else
|
11
|
+
features = {}
|
12
|
+
features[@name] = {"value" => @item["value"], "type" => @item["type"], "targeting_groups" => @item["targeting_groups"]}
|
13
|
+
res = HttpClient.instance.post("get_user_data", "get_feature_by_user", {"user" => user, "features" => features})
|
14
|
+
res = JSON.parse(res)
|
15
|
+
return self.get_item_value(res["features"][@name])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Microkit
|
4
|
+
class Group
|
5
|
+
attr_reader :change
|
6
|
+
def initialize(type, values)
|
7
|
+
if !['config', 'features'].include? type then
|
8
|
+
raise Error('Group type is not valid')
|
9
|
+
end
|
10
|
+
@change = Publisher.new
|
11
|
+
@group = {}
|
12
|
+
@type = type
|
13
|
+
@updated = false
|
14
|
+
@initialized = false
|
15
|
+
self.update(values)
|
16
|
+
end
|
17
|
+
|
18
|
+
def update (values)
|
19
|
+
prev_value = self.value
|
20
|
+
values.each do |key, item|
|
21
|
+
if @group.key?(key) then
|
22
|
+
@group[key].update(item)
|
23
|
+
else
|
24
|
+
if @initialized then
|
25
|
+
@updated = true
|
26
|
+
end
|
27
|
+
if item.key?("value") and item["value"].class != Hash then
|
28
|
+
if @type == "config" then
|
29
|
+
@group[key] = ConfigItem.new(item, key)
|
30
|
+
elsif @type == "features" then
|
31
|
+
@group[key] = FeatureItem.new(item, key)
|
32
|
+
end
|
33
|
+
published = Proc.new do |current_val, prev_val|
|
34
|
+
@updated = true
|
35
|
+
end
|
36
|
+
|
37
|
+
@group[key].change.subscribe(published)
|
38
|
+
else
|
39
|
+
@group[key] = Group.new(@type, item)
|
40
|
+
published = Proc.new do |current_val, prev_val|
|
41
|
+
@updated = true
|
42
|
+
end
|
43
|
+
@group[key].change.subscribe(published)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
var_name = "@#{key}" # the '@' is required
|
47
|
+
self.instance_variable_set(var_name, @group[key])
|
48
|
+
self.class.module_eval { attr_accessor key}
|
49
|
+
|
50
|
+
end
|
51
|
+
if !@initialized then
|
52
|
+
@initialized = true
|
53
|
+
end
|
54
|
+
if @updated then
|
55
|
+
@change.publish(self.value ,prev_value)
|
56
|
+
@updated = false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
def value
|
60
|
+
@group.reduce({}) {|a, (key, item)| a[key] = item.value; return a}
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'singleton'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
require 'openssl'
|
8
|
+
require 'base64'
|
9
|
+
require 'uri'
|
10
|
+
|
11
|
+
module Microkit
|
12
|
+
class HttpClient
|
13
|
+
include Singleton
|
14
|
+
def post (action, uri = nil, post_data = nil)
|
15
|
+
data = {"options" => {"service" => Config.instance.get('service'), "action" => action, "lang" =>"ruby"}}
|
16
|
+
if post_data then
|
17
|
+
data = data.merge(post_data)
|
18
|
+
end
|
19
|
+
if Config.instance.get("key_vars_values") then
|
20
|
+
data["key_vars_values"] = Config.instance.get("key_vars_values")
|
21
|
+
end
|
22
|
+
|
23
|
+
uri = URI("#{Config.instance.get("http") ? 'http' : 'https'}://#{Config.instance.get("base_url")}#{Config.instance.get("port") != "" ? ":#{Config.instance.get("port")}" : ""}/#{Config.instance.get("version")}/#{uri ? uri : ""}")
|
24
|
+
https = Net::HTTP.new(uri.host,uri.port)
|
25
|
+
https.use_ssl = !Config.instance.get("http")
|
26
|
+
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json','Authorization' => "Bearer #{Config.instance.get("api_key")}"})
|
27
|
+
req.body = data.to_json
|
28
|
+
res = https.request(req)
|
29
|
+
|
30
|
+
return res.body
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Microkit
|
4
|
+
class Item
|
5
|
+
attr_reader :change
|
6
|
+
def initialize(value, name)
|
7
|
+
@change = Publisher.new
|
8
|
+
@item = value
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
self.get_item_value @item
|
14
|
+
end
|
15
|
+
|
16
|
+
def update(value)
|
17
|
+
prev_value = self.value
|
18
|
+
have_been_changed = @item["value"] != value["value"] or @item["type"] != value["type"]
|
19
|
+
@item = value
|
20
|
+
if have_been_changed then
|
21
|
+
@change.publish(self.value, prev_value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_item_value(item)
|
26
|
+
case item["type"]
|
27
|
+
when "string"
|
28
|
+
return item["value"].to_s
|
29
|
+
when "number"
|
30
|
+
return item["value"].to_f
|
31
|
+
when "boolean"
|
32
|
+
return item["value"] == true || item["value"] == "true"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
|
2
|
+
require "singleton"
|
3
|
+
module Microkit
|
4
|
+
def self.initialize_kit (key, user = {}, options = {})
|
5
|
+
|
6
|
+
secret_size = 24
|
7
|
+
sdk_key = key[0..key.length-secret_size-2]
|
8
|
+
secret = key[-secret_size..-1]
|
9
|
+
options["api_key"] = sdk_key
|
10
|
+
options["user"] = user
|
11
|
+
|
12
|
+
Config.instance.init(options)
|
13
|
+
# res = Microkit.instance.network_controller.get_latest_data('init')
|
14
|
+
# res = json.loads(res.decode('utf-8'))
|
15
|
+
|
16
|
+
# Microkit.instance.config.set("key_vars_values", res["key_vars_values"])
|
17
|
+
# Microkit.instance.config_kit = Group('config', Microkit.decrypt(res['config'], secret) if 'config' in res and res['config'] != '' else {})
|
18
|
+
# Microkit.instance.features_kit = Group('features', res["features"] if 'features' in res and type(res["features"]) == dict else {})
|
19
|
+
res = NetworkController.instance.get_latest_data('init')
|
20
|
+
res = JSON.parse(res)
|
21
|
+
config = JSON.parse(self.decrypt(res["config"], secret))
|
22
|
+
Config.instance.set("key_vars_values", res["key_vars_values"])
|
23
|
+
Microkit.instance.instance_variable_set("@config_kit", Group.new("config", config))
|
24
|
+
Microkit.instance.instance_variable_set("@features_kit", Group.new("features", res["features"]))
|
25
|
+
Microkit.instance.class.module_eval { attr_accessor :config_kit}
|
26
|
+
Microkit.instance.class.module_eval { attr_accessor :features_kit}
|
27
|
+
published = Proc.new do |current_val, prev_val|
|
28
|
+
res = JSON.parse(current_val)
|
29
|
+
config = JSON.parse(self.decrypt(res["config"], secret))
|
30
|
+
Microkit.instance.config_kit.update(config)
|
31
|
+
Microkit.instance.features_kit.update(res["features"])
|
32
|
+
end
|
33
|
+
Microkit.instance.subscription = NetworkController.instance.change.subscribe(published)
|
34
|
+
if Config.instance.get("polling_on") then
|
35
|
+
NetworkController.instance.start_update_interval
|
36
|
+
end
|
37
|
+
return Microkit.instance
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.kit_ready
|
42
|
+
if Microkit.instance.instance_variable_defined?(:@config_kit) then
|
43
|
+
Microkit.instance
|
44
|
+
else
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def self.decrypt (encrypted, key)
|
52
|
+
cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
|
53
|
+
cipher.decrypt
|
54
|
+
cipher.key = Base64.urlsafe_decode64(key)
|
55
|
+
cipher.iv = Base64.urlsafe_decode64(encrypted)[0..15]
|
56
|
+
|
57
|
+
# Start the decryption
|
58
|
+
encrypted = Base64.urlsafe_decode64(encrypted)[16..-1]
|
59
|
+
decrypted = cipher.update(encrypted) + cipher.final
|
60
|
+
return decrypted
|
61
|
+
end
|
62
|
+
class Microkit
|
63
|
+
include Singleton
|
64
|
+
attr_accessor :subscription
|
65
|
+
def initialize
|
66
|
+
@subscription = nil
|
67
|
+
end
|
68
|
+
def close
|
69
|
+
if self.instance_variable_defined?(:@config_kit) then
|
70
|
+
self.remove_instance_variable(:@config_kit)
|
71
|
+
self.remove_instance_variable(:@features_kit)
|
72
|
+
end
|
73
|
+
NetworkController.instance.stop_update_interval
|
74
|
+
if @subscription then
|
75
|
+
@subscription.unsubscribe
|
76
|
+
@subscription = nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "singleton"
|
2
|
+
module Microkit
|
3
|
+
class NetworkController
|
4
|
+
include Singleton
|
5
|
+
attr_reader :change
|
6
|
+
def initialize
|
7
|
+
@change = Publisher.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_latest_data(action)
|
11
|
+
return HttpClient.instance.post(action, nil, {"user" => Config.instance.get("user")})
|
12
|
+
end
|
13
|
+
|
14
|
+
def start_update_interval ()
|
15
|
+
@t = Thread.new do
|
16
|
+
loop do
|
17
|
+
sleep Config.instance.get('polling_interval')
|
18
|
+
res = self.get_latest_data('update')
|
19
|
+
@change.publish(res, "")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def stop_update_interval ()
|
25
|
+
if @t == Thread then
|
26
|
+
@t.kill
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
module Microkit
|
3
|
+
class Publisher
|
4
|
+
def initialize
|
5
|
+
@subscribers = {}
|
6
|
+
end
|
7
|
+
def subscribe(callback)
|
8
|
+
key = (0...12).map { (65 + rand(26)).chr }.join
|
9
|
+
@subscribers[key] = callback
|
10
|
+
return Subscription.new(@subscribers, key)
|
11
|
+
end
|
12
|
+
def publish(current_value, prev_value)
|
13
|
+
@subscribers.each do |key, callback|
|
14
|
+
callback.call(current_value, prev_value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
class Subscription
|
19
|
+
def initialize(subscribers, key)
|
20
|
+
@subscribers = subscribers
|
21
|
+
@key = key
|
22
|
+
end
|
23
|
+
def unsubscribe
|
24
|
+
@subscribers.delete(@key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/microkit.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "microkit/version"
|
2
|
+
require "microkit/publisher"
|
3
|
+
require "microkit/http_client"
|
4
|
+
require "microkit/config"
|
5
|
+
require "microkit/group"
|
6
|
+
require "microkit/item"
|
7
|
+
require "microkit/config_item"
|
8
|
+
require "microkit/feature_item"
|
9
|
+
require "microkit/network_controller"
|
10
|
+
require "microkit/microkit"
|
11
|
+
|
12
|
+
module Microkit
|
13
|
+
class Error < StandardError; end
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "microkit/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "microkit-ruby-sdk"
|
8
|
+
spec.version = Microkit::VERSION
|
9
|
+
spec.authors = ["chaimvaid"]
|
10
|
+
spec.email = ["chaim@daneltech.com"]
|
11
|
+
|
12
|
+
spec.summary = "A SDK to be used with MicroKit Platform"
|
13
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://microkit.app"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://bitbucket.org/micro-kit/microkit-ruby-sdk/src/master/"
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: microkit-ruby-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- chaimvaid
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- chaim@daneltech.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/microkit.rb
|
72
|
+
- lib/microkit/config.rb
|
73
|
+
- lib/microkit/config_item.rb
|
74
|
+
- lib/microkit/feature_item.rb
|
75
|
+
- lib/microkit/group.rb
|
76
|
+
- lib/microkit/http_client.rb
|
77
|
+
- lib/microkit/item.rb
|
78
|
+
- lib/microkit/microkit.rb
|
79
|
+
- lib/microkit/network_controller.rb
|
80
|
+
- lib/microkit/publisher.rb
|
81
|
+
- lib/microkit/version.rb
|
82
|
+
- microkit-ruby-sdk.gemspec
|
83
|
+
homepage: https://microkit.app
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
allowed_push_host: https://rubygems.org
|
88
|
+
homepage_uri: https://microkit.app
|
89
|
+
source_code_uri: https://bitbucket.org/micro-kit/microkit-ruby-sdk/src/master/
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.0.3.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: A SDK to be used with MicroKit Platform
|
109
|
+
test_files: []
|