volatile_wtf 0.0.4 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3953d7df5d35fd562901234c1d6857fb8bf30faedd9260995a24ad953592bae
4
- data.tar.gz: 0ee37dc66758146e026ce79662afc81a21d2c6fb136318314b8633c2922e2fc2
3
+ metadata.gz: 90040b494c7a8b70f8f1173b631125a854ab0befd4366e276900e86d9b3e30b5
4
+ data.tar.gz: e74bb044370dd600514fb6dbb02aecde06ff5b95ee6bbc798785422e5816b530
5
5
  SHA512:
6
- metadata.gz: '090fc56acf621cbdbe34aadce952ed3d2c4d10edabb4f02af8fb39564761e7d2dbabbc7fcfde39a9d7326bd78c024af6bf1ebe35287ff4aff8364dd07f34b840'
7
- data.tar.gz: 22dd81ca15265c37b266f985cfca1ae604985f128b15b3e1b307a44f273bb4532579a01ef1269cce465fdd01d7d5dc1e3c4deb784f606e0157515c3b1c5bbc8a
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@2x.png)](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
- `gem install volatile_wtf`
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, use `Storage.push`:
39
+ If you want to create a pair independent from your Storage:
42
40
 
43
41
  ```ruby
44
- Storage.push('random_key', 'random_val')
42
+ Storage.set('random_key', 'random_val')
45
43
  ```
46
44
 
47
- You can use symbols as keys, but you'll still have to use strings to get values.
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.pull`:
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.pull('random_key') # => 'random_val'
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
- ### Salted keys (or simply key prefixes)
73
+ ### Namespaces
74
+
75
+ Namespace is used for distinguishing keys from different Storages.
76
+
77
+ #### Passing your own namespace
78
78
 
79
- Salt is used for keys to make them trackable within this gem.
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
- #### Passing your own salt
81
+ ```ruby
82
+ Storage = Volatile::Storage.new('my_ns')
83
+ ```
82
84
 
83
- By default, Storage is initialized with salt equal to `SecureRandom.hex[0..5]` and makes keys look like `0123ab_some` instead of just `some`. You can pass your own salt value like this:
85
+ Or you can change namespace later:
84
86
 
85
87
  ```ruby
86
- Storage = Volatile::Storage.new('my_own_salt')
88
+ Storage.namespace = 'my_own_ns'
87
89
  ```
88
90
 
89
- #### Retrieving a real key name
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 real (salted) key name, you should use `Storage#real_key`:
95
+ If you want to get a namespaced key name, you should use `Storage#namespaced_key`:
92
96
 
93
97
  ```ruby
94
- Storage.real_key('nice_key') # => '0123ab_nice_key'
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 salt value.
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 salt:
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 real keys, use `with_real_keys: true` parameter:
114
+ To generate a hash with namespaced keys, use `use_namespace: true` parameter:
111
115
 
112
116
  ```ruby
113
- Storage.to_h(with_real_keys: true)
117
+ Storage.to_h(use_namespace: true)
114
118
  ```
115
119
 
116
120
  This will return the following result:
@@ -0,0 +1,17 @@
1
+ module Volatile
2
+ class Cache
3
+ attr_reader :storage
4
+
5
+ def initialize
6
+ @storage = {}
7
+ end
8
+
9
+ def save(key, value)
10
+ @storage[key] = value
11
+ end
12
+
13
+ def fetch(key, &block)
14
+ @storage.fetch(key) { block.call }
15
+ end
16
+ end
17
+ end
@@ -2,39 +2,56 @@ require 'net/http'
2
2
  require 'uri'
3
3
 
4
4
  module Volatile
5
- class Manager
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 uri_with key, value
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 uri_with key
19
+ @cache.fetch(key) { get_from api_uri key }
12
20
  end
13
21
 
14
- def created_at(key)
15
- resp = get_from uri_with key, nil, 'created'
16
- Time.at(resp.to_i)
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 updated_at(key)
21
- resp = get_from uri_with key, nil, 'modified'
22
- Time.at(resp.to_i)
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
- def uri_with(key = nil, value = nil, modifier = nil)
33
- url = "https://volatile.wtf/?key=#{key}"
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
@@ -1,51 +1,68 @@
1
1
  require 'securerandom'
2
2
  require 'set'
3
+
3
4
  module Volatile
4
5
  class Storage
5
- attr_accessor :salt
6
- attr_reader :key, :value
6
+ attr_accessor :namespace
7
7
 
8
- def initialize(salt = nil)
9
- @salt = salt || SecureRandom.hex[0..5]
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
- @manager.retrieve(real_key(key))
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
- @keys << real_key(key)
20
- @manager.store(real_key(key), value)
25
+ key = namespaced_key(key)
26
+ @keys << key
27
+ set(key, value)
21
28
  end
22
29
 
23
- def pull(key)
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
- def push(key, value)
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.created_at(real_key(key))
49
+ @manager.created(key)
34
50
  end
35
51
 
36
52
  def modified(key)
37
- @manager.updated_at(real_key(key))
53
+ @manager.modified(key)
38
54
  end
39
55
 
40
- def real_key(key)
41
- "#{@salt}_#{key}"
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(with_real_keys = false)
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 = with_real_keys ? key : key.sub("#{@salt}_", '')
65
+ hash_key = use_namespace ? key : key.sub("#{@namespace}_", '')
49
66
  hash[hash_key] = manager.retrieve(key)
50
67
  end
51
68
  end
@@ -1,3 +1,3 @@
1
1
  module VolatileWtf
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/volatile_wtf.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'volatile_wtf/cache'
1
2
  require 'volatile_wtf/manager'
2
3
  require 'volatile_wtf/storage'
3
4
  require 'volatile_wtf/version'
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
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-12 00:00:00.000000000 Z
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.3
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.