volatile_wtf 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -27
- data/lib/volatile_wtf/cache.rb +17 -0
- data/lib/volatile_wtf/manager.rb +31 -14
- data/lib/volatile_wtf/storage.rb +32 -15
- data/lib/volatile_wtf/version.rb +1 -1
- data/lib/volatile_wtf.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90040b494c7a8b70f8f1173b631125a854ab0befd4366e276900e86d9b3e30b5
|
4
|
+
data.tar.gz: e74bb044370dd600514fb6dbb02aecde06ff5b95ee6bbc798785422e5816b530
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b68c40f821cc5a2ce42979651fc229f503fc6a87d9253431e88cc0b2566163fb7e7df645c101c4a79582213f96e9d153d0ab97766fc1c3191d2ae6291bfe3cc3
|
7
|
+
data.tar.gz: 33e436ee753f87d8671cb83c5031073ac939b055068c566f9d02c1e7235b10e7cd83ca462642fb8d4becb1e6da03a0805d8094004d2b303c0f815c6733f2e5ec
|
data/README.md
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
# VolatileWTF
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/volatile_wtf
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/volatile_wtf.png)](https://badge.fury.io/rb/volatile_wtf)
|
4
4
|
|
5
5
|
A Ruby wrapper for [Volatile](https://volatile.wtf/), a key-value pair API that everyone can use.
|
6
6
|
|
7
|
+
More documentation at [RDoc](https://www.rubydoc.info/gems/volatile_wtf).
|
8
|
+
|
7
9
|
## Installation
|
8
10
|
|
9
|
-
|
11
|
+
```bash
|
12
|
+
gem install volatile_wtf
|
13
|
+
```
|
10
14
|
|
11
15
|
or for a Gemfile:
|
12
16
|
|
@@ -20,12 +24,6 @@ bundle install
|
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
Don't forget to require the gem:
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
require 'volatile_wtf'
|
27
|
-
```
|
28
|
-
|
29
27
|
### Initialize storage object
|
30
28
|
|
31
29
|
```ruby
|
@@ -38,26 +36,24 @@ Storage = Volatile::Storage.new
|
|
38
36
|
Storage['user_name'] = 'Alice' # => 'Alice'
|
39
37
|
```
|
40
38
|
|
41
|
-
If you want to create a pair independent from your Storage
|
39
|
+
If you want to create a pair independent from your Storage:
|
42
40
|
|
43
41
|
```ruby
|
44
|
-
Storage.
|
42
|
+
Storage.set('random_key', 'random_val')
|
45
43
|
```
|
46
44
|
|
47
|
-
You can use symbols as keys, but
|
45
|
+
You can use symbols as keys, but it is not recommended.
|
48
46
|
|
49
47
|
### Retrieve a value by key
|
50
48
|
|
51
|
-
Using `#[](key)` retrieves a key from current Storage instance:
|
52
|
-
|
53
49
|
```ruby
|
54
50
|
Storage['user_name'] # => 'Alice'
|
55
51
|
```
|
56
52
|
|
57
|
-
If you want to retrieve a value by a custom key independent from your Storage, use `Storage.
|
53
|
+
If you want to retrieve a value by a custom key independent from your Storage, use `Storage.get`:
|
58
54
|
|
59
55
|
```ruby
|
60
|
-
Storage.
|
56
|
+
Storage.get('random_key') # => 'random_val'
|
61
57
|
```
|
62
58
|
|
63
59
|
### `created` and `modified` timestamps
|
@@ -74,31 +70,39 @@ Storage.pull('random_key') # => 'random_val'
|
|
74
70
|
Storage.modified('user_name') # => 2019-11-12 17:40:45 +0300
|
75
71
|
```
|
76
72
|
|
77
|
-
###
|
73
|
+
### Namespaces
|
74
|
+
|
75
|
+
Namespace is used for distinguishing keys from different Storages.
|
76
|
+
|
77
|
+
#### Passing your own namespace
|
78
78
|
|
79
|
-
|
79
|
+
By default, Storage is initialized with a namaspace equal to `SecureRandom.hex[0..5]` and makes keys look like `0123ab_some` instead of just `some`. You can pass your own value like this:
|
80
80
|
|
81
|
-
|
81
|
+
```ruby
|
82
|
+
Storage = Volatile::Storage.new('my_ns')
|
83
|
+
```
|
82
84
|
|
83
|
-
|
85
|
+
Or you can change namespace later:
|
84
86
|
|
85
87
|
```ruby
|
86
|
-
Storage =
|
88
|
+
Storage.namespace = 'my_own_ns'
|
87
89
|
```
|
88
90
|
|
89
|
-
|
91
|
+
__Warning!__ You can loose links to previously stored data if you change a namespace.
|
92
|
+
|
93
|
+
#### Generating a namespaced key name
|
90
94
|
|
91
|
-
If you want to get a
|
95
|
+
If you want to get a namespaced key name, you should use `Storage#namespaced_key`:
|
92
96
|
|
93
97
|
```ruby
|
94
|
-
Storage.
|
98
|
+
Storage.namespaced_key('nice_key') # => '0123ab_nice_key'
|
95
99
|
```
|
96
100
|
|
97
|
-
Don't forget that every `Volatile::Storage.new` has its own
|
101
|
+
Don't forget that every `Volatile::Storage.new` has its own namespace.
|
98
102
|
|
99
103
|
### Hash conversion (`#to_h`)
|
100
104
|
|
101
|
-
By default, `#to_h` generates a hash with friendly keys without
|
105
|
+
By default, `#to_h` generates a hash with friendly keys without a namespace:
|
102
106
|
|
103
107
|
```ruby
|
104
108
|
{
|
@@ -107,10 +111,10 @@ By default, `#to_h` generates a hash with friendly keys without salt:
|
|
107
111
|
}
|
108
112
|
```
|
109
113
|
|
110
|
-
To generate a hash with
|
114
|
+
To generate a hash with namespaced keys, use `use_namespace: true` parameter:
|
111
115
|
|
112
116
|
```ruby
|
113
|
-
Storage.to_h(
|
117
|
+
Storage.to_h(use_namespace: true)
|
114
118
|
```
|
115
119
|
|
116
120
|
This will return the following result:
|
data/lib/volatile_wtf/manager.rb
CHANGED
@@ -2,39 +2,56 @@ require 'net/http'
|
|
2
2
|
require 'uri'
|
3
3
|
|
4
4
|
module Volatile
|
5
|
-
|
5
|
+
BASE_URL = 'https://volatile.wtf'
|
6
|
+
|
7
|
+
# Manages the server side of Volatile storage, handles API requests
|
8
|
+
class Manager
|
9
|
+
def initialize
|
10
|
+
@cache = Cache.new
|
11
|
+
end
|
12
|
+
|
6
13
|
def store(key, value)
|
7
|
-
get_from
|
14
|
+
get_from api_uri key, value
|
15
|
+
@cache.save(key, value)
|
8
16
|
end
|
9
17
|
|
10
18
|
def retrieve(key)
|
11
|
-
get_from
|
19
|
+
@cache.fetch(key) { get_from api_uri key }
|
12
20
|
end
|
13
21
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
22
|
+
def reload(key)
|
23
|
+
fresh_value = get_from api_uri key
|
24
|
+
store(key, fresh_value)
|
17
25
|
end
|
18
|
-
alias created created_at
|
19
26
|
|
20
|
-
def
|
21
|
-
|
22
|
-
Time.at(
|
27
|
+
def created(key)
|
28
|
+
time = key_modificator key, :created
|
29
|
+
Time.at(time.to_i)
|
30
|
+
end
|
31
|
+
|
32
|
+
def modified(key)
|
33
|
+
time = key_modificator key, :modified
|
34
|
+
Time.at(time.to_i)
|
23
35
|
end
|
24
|
-
alias modified updated_at
|
25
36
|
|
26
37
|
private
|
27
38
|
|
39
|
+
# API URI helper for accessing key modificators data
|
40
|
+
def key_modificator(key, mod)
|
41
|
+
get_from api_uri key, nil, mod
|
42
|
+
end
|
43
|
+
|
28
44
|
def get_from(uri)
|
29
45
|
Net::HTTP.get(uri)
|
30
46
|
end
|
31
47
|
|
32
|
-
|
33
|
-
|
48
|
+
# Builds a Volatile API URI
|
49
|
+
def api_uri(key = nil, value = nil, modifier = nil)
|
50
|
+
url = "#{BASE_URL}/?key=#{key}"
|
34
51
|
url << "&#{modifier}" if modifier
|
35
52
|
url << "&val=#{value}" if value
|
36
53
|
|
37
54
|
URI(url)
|
38
55
|
end
|
39
56
|
end
|
40
|
-
end
|
57
|
+
end
|
data/lib/volatile_wtf/storage.rb
CHANGED
@@ -1,51 +1,68 @@
|
|
1
1
|
require 'securerandom'
|
2
2
|
require 'set'
|
3
|
+
|
3
4
|
module Volatile
|
4
5
|
class Storage
|
5
|
-
attr_accessor :
|
6
|
-
attr_reader :key, :value
|
6
|
+
attr_accessor :namespace
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
8
|
+
def initialize(namespace = nil)
|
9
|
+
@namespace = namespace || SecureRandom.hex[0..5]
|
10
10
|
@keys = Set.new
|
11
11
|
@manager = Manager.new
|
12
12
|
end
|
13
13
|
|
14
|
+
# Gets value by key (with a namespace)
|
15
|
+
# Returns a `Volatile::Pair` instance, so that #modified and #created
|
16
|
+
# are easily accessible.
|
14
17
|
def [](key)
|
15
|
-
|
18
|
+
key = namespaced_key(key)
|
19
|
+
get(key)
|
16
20
|
end
|
17
21
|
|
22
|
+
# Sets value for a given key (with a namespace). Keeps the key for later
|
23
|
+
# to use in `to_h`
|
18
24
|
def []=(key, value)
|
19
|
-
|
20
|
-
@
|
25
|
+
key = namespaced_key(key)
|
26
|
+
@keys << key
|
27
|
+
set(key, value)
|
21
28
|
end
|
22
29
|
|
23
|
-
|
30
|
+
# Gets value by key (without a namespace)
|
31
|
+
# Returns a `Volatile::Pair` instance, so that #modified and #created
|
32
|
+
# are easily accessible.
|
33
|
+
def get(key)
|
24
34
|
@manager.retrieve(key)
|
25
35
|
end
|
26
36
|
|
27
|
-
|
37
|
+
# Sets value for a given key (without a namespace)
|
38
|
+
def set(key, value)
|
28
39
|
@manager.store(key, value)
|
29
40
|
{ key => value }
|
30
41
|
end
|
31
42
|
|
43
|
+
# Refetch cached key
|
44
|
+
def reload(key)
|
45
|
+
@manager.reload(key)
|
46
|
+
end
|
47
|
+
|
32
48
|
def created(key)
|
33
|
-
@manager.
|
49
|
+
@manager.created(key)
|
34
50
|
end
|
35
51
|
|
36
52
|
def modified(key)
|
37
|
-
@manager.
|
53
|
+
@manager.modified(key)
|
38
54
|
end
|
39
55
|
|
40
|
-
|
41
|
-
|
56
|
+
# Generates a namespaced option for a key name
|
57
|
+
def namespaced_key(key)
|
58
|
+
"#{@namespace}_#{key}"
|
42
59
|
end
|
43
60
|
|
44
|
-
def to_h(
|
61
|
+
def to_h(use_namespace = false)
|
45
62
|
manager = @manager
|
46
63
|
|
47
64
|
@keys.each_with_object({}) do |key, hash|
|
48
|
-
hash_key =
|
65
|
+
hash_key = use_namespace ? key : key.sub("#{@namespace}_", '')
|
49
66
|
hash[hash_key] = manager.retrieve(key)
|
50
67
|
end
|
51
68
|
end
|
data/lib/volatile_wtf/version.rb
CHANGED
data/lib/volatile_wtf.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: volatile_wtf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Uladzislau Andreyeu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- bin/console
|
67
67
|
- bin/setup
|
68
68
|
- lib/volatile_wtf.rb
|
69
|
+
- lib/volatile_wtf/cache.rb
|
69
70
|
- lib/volatile_wtf/manager.rb
|
70
71
|
- lib/volatile_wtf/storage.rb
|
71
72
|
- lib/volatile_wtf/version.rb
|
@@ -93,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
94
|
- !ruby/object:Gem::Version
|
94
95
|
version: '0'
|
95
96
|
requirements: []
|
96
|
-
rubygems_version: 3.0.
|
97
|
+
rubygems_version: 3.0.6
|
97
98
|
signing_key:
|
98
99
|
specification_version: 4
|
99
100
|
summary: A Ruby wrapper for Volatile, a key-value pair API that everyone can use.
|