volatile_wtf 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e782e17afbbc489f5f6c6736539c576c073b43038728eda4a0fcda2858e37398
4
+ data.tar.gz: 72a91368b25c5397c061b6fffcf7fdc0ae8dad9e1f537f4c32055985a8b9c955
5
+ SHA512:
6
+ metadata.gz: 0f45924ba8a0426e119f5e347df81c7538f4c67bb13ea85aa2159ef040a09da21e16f146d0f976e356cd3ddeac30ce5f31add10a0dced822489b6d74eed89c6a
7
+ data.tar.gz: decee9b5e799b5213ff5e26e1d36619786e03b484a6dbb72aff4034a967394627bec6d82c5d11fa432b630e61b0c7c65d5dd0e43d36325e0b067c39c805e7212
data/CHANGELOG.md ADDED
File without changes
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Uladzislau Andreyeu
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,122 @@
1
+ # VolatileWTF
2
+
3
+ A Ruby wrapper for [Volatile](https://volatile.wtf/), a key-value pair API that everyone can use.
4
+
5
+ ## Installation
6
+
7
+ `gem install volatile_wtf`
8
+
9
+ or for a Gemfile:
10
+
11
+ ```ruby
12
+ gem 'volatile_wtf'
13
+ ```
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Don't forget to require the gem:
22
+
23
+ ```ruby
24
+ require 'volatile_wtf'
25
+ ```
26
+
27
+ ### Initialize storage object
28
+
29
+ ```ruby
30
+ Storage = Volatile::Storage.new
31
+ ```
32
+
33
+ ### Create a key-value pair
34
+
35
+ ```ruby
36
+ Storage['user_name'] = 'Alice' # => 'Alice'
37
+ ```
38
+
39
+ If you want to create a pair independent from your Storage, use `Storage.push`:
40
+
41
+ ```ruby
42
+ Storage.push('random_key', 'random_val')
43
+ ```
44
+
45
+ You can use symbols as keys, but you'll still have to use strings to get values.
46
+
47
+ ### Retrieve a value by key
48
+
49
+ Using `#[](key)` retrieves a key from current Storage instance:
50
+
51
+ ```ruby
52
+ Storage['user_name'] # => 'Alice'
53
+ ```
54
+
55
+ If you want to retrieve a value by a custom key independent from your Storage, use `Storage.pull`:
56
+
57
+ ```ruby
58
+ Storage.pull('random_key') # => 'random_val'
59
+ ```
60
+
61
+ ### `created` and `modified` timestamps
62
+
63
+ [Volatile](https://volatile.wtf/) allows to get information about when a key-pair was created and modified.
64
+
65
+ - Created
66
+ ```ruby
67
+ Storage.created('user_name') # => 2019-11-12 16:55:45 +0300
68
+ ```
69
+ - Modified
70
+
71
+ ```ruby
72
+ Storage.modified('user_name') # => 2019-11-12 17:40:45 +0300
73
+ ```
74
+
75
+ ### Salted keys (or simply key prefixes)
76
+
77
+ Salt is used for keys to make them trackable within this gem.
78
+
79
+ #### Passing your own salt
80
+
81
+ 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:
82
+
83
+ ```ruby
84
+ Storage = Volatile::Storage.new('my_own_salt')
85
+ ```
86
+
87
+ #### Retrieving a real key name
88
+
89
+ If you want to get a real (salted) key name, you should use `Storage#real_key`:
90
+
91
+ ```ruby
92
+ Storage.real_key('nice_key') # => '0123ab_nice_key'
93
+ ```
94
+
95
+ Don't forget that every `Volatile::Storage.new` has its own salt value.
96
+
97
+ ### Hash conversion (`#to_h`)
98
+
99
+ By default, `#to_h` generates a hash with friendly keys without salt:
100
+
101
+ ```ruby
102
+ {
103
+ "user_name" => "Alice",
104
+ "email" => "alice@example.com"
105
+ }
106
+ ```
107
+
108
+ To generate a hash with real keys, use `with_real_keys: true` parameter:
109
+
110
+ ```ruby
111
+ Storage.to_h(with_real_keys: true)
112
+ ```
113
+
114
+ This will return the following result:
115
+
116
+ ```ruby
117
+ {
118
+ "2365fc_user_name" => "Alice",
119
+ "2365fc_email" => "alice@example.com"
120
+ }
121
+ ```
122
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "volatile_wtf"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module Volatile
5
+ class Manager
6
+ def store(key, value)
7
+ get_from uri_with key, value
8
+ end
9
+
10
+ def retrieve(key)
11
+ get_from uri_with key
12
+ end
13
+
14
+ def created_at(key)
15
+ resp = get_from uri_with key, nil, 'created'
16
+ Time.at(resp.to_i)
17
+ end
18
+ alias created created_at
19
+
20
+ def updated_at(key)
21
+ resp = get_from uri_with key, nil, 'modified'
22
+ Time.at(resp.to_i)
23
+ end
24
+ alias modified updated_at
25
+
26
+ private
27
+
28
+ def get_from(uri)
29
+ Net::HTTP.get(uri)
30
+ end
31
+
32
+ def uri_with(key = nil, value = nil, modifier = nil)
33
+ url = "https://volatile.wtf/?key=#{key}"
34
+ url << "&#{modifier}" if modifier
35
+ url << "&val=#{value}" if value
36
+
37
+ URI(url)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,53 @@
1
+ require 'securerandom'
2
+ require 'set'
3
+ module Volatile
4
+ class Storage
5
+ attr_accessor :salt
6
+ attr_reader :key, :value
7
+
8
+ def initialize(salt = nil)
9
+ @salt = salt || SecureRandom.hex[0..5]
10
+ @keys = Set.new
11
+ @manager = Manager.new
12
+ end
13
+
14
+ def [](key)
15
+ @manager.retrieve(real_key(key))
16
+ end
17
+
18
+ def []=(key, value)
19
+ @keys << real_key(key)
20
+ @manager.store(real_key(key), value)
21
+ end
22
+
23
+ def pull(key)
24
+ @manager.retrieve(key)
25
+ end
26
+
27
+ def push(key, value)
28
+ @manager.store(key, value)
29
+ { key => value }
30
+ end
31
+
32
+ def created(key)
33
+ @manager.created_at(key)
34
+ end
35
+
36
+ def modified(key)
37
+ @manager.updated_at(key)
38
+ end
39
+
40
+ def real_key(key)
41
+ "#{@salt}_#{key}"
42
+ end
43
+
44
+ def to_h(with_real_keys = false)
45
+ manager = @manager
46
+
47
+ @keys.each_with_object({}) do |key, hash|
48
+ hash_key = with_real_keys ? key : key.sub("#{@salt}_", '')
49
+ hash[hash_key] = manager.retrieve(key)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module VolatileWtf
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'volatile_wtf/manager'
2
+ require 'volatile_wtf/storage'
3
+ require 'volatile_wtf/version'
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "volatile_wtf/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "volatile_wtf"
8
+ spec.version = VolatileWtf::VERSION
9
+ spec.authors = ["Uladzislau Andreyeu"]
10
+ spec.email = ["me@vld.by"]
11
+
12
+ spec.summary = %q{A Ruby wrapper for Volatile, a key-value pair API that everyone can use.}
13
+ spec.homepage = "https://github.com/vladyio/volatile-wtf-rb"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/vladyio/volatile-wtf-rb"
23
+ spec.metadata["changelog_uri"] = "https://github.com/vladyio/volatile-wtf-rb/blob/master/CHANGELOG.md"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: volatile_wtf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Uladzislau Andreyeu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-12 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
+ - me@vld.by
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/volatile_wtf.rb
69
+ - lib/volatile_wtf/manager.rb
70
+ - lib/volatile_wtf/storage.rb
71
+ - lib/volatile_wtf/version.rb
72
+ - volatile_wtf.gemspec
73
+ homepage: https://github.com/vladyio/volatile-wtf-rb
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org/
78
+ homepage_uri: https://github.com/vladyio/volatile-wtf-rb
79
+ source_code_uri: https://github.com/vladyio/volatile-wtf-rb
80
+ changelog_uri: https://github.com/vladyio/volatile-wtf-rb/blob/master/CHANGELOG.md
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A Ruby wrapper for Volatile, a key-value pair API that everyone can use.
100
+ test_files: []