safelylaunch 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ea7229e64502f350239eafb2a748a5180c7b2e6575b5dca259f25d89845024d
4
- data.tar.gz: b004e8c7675d3082c3a095c7161f7e41596ef4f89f973baa9d402e8f0b969cee
3
+ metadata.gz: 561fc09f13e32437d193dd5c258958a354afb56b1aa655ea31a429563b2a823f
4
+ data.tar.gz: e13e8e848d7dbf3f8a938829c08422ce66a425076a766c351a81b8764bb9b750
5
5
  SHA512:
6
- metadata.gz: 8bc1e13e435f9cf4658a1c6dc8482d5c9dad84b2b10fc1eb6fdf7ab2edda570bfe48c8e4410faa5edf51b116eb8fa4fef65e9a952e0d1d56911b2fb1add3c5fe
7
- data.tar.gz: a73924816255fe27662720be0809edec4f7b18fff8f70c2381545d14fab03eea40f7146b3bf50eb134dcb3cc1cf1a223f584df45c8bfccac9458c1aa756b0427
6
+ metadata.gz: a5d41e4ecb547cbf302b5bebec2770cdc1bfe5fabe2fc190e8c5be832f030e4cb78cd2881a0a93b666ffc32b5fbd28276e19eacb6f467489589dab898909733f
7
+ data.tar.gz: 8175518512944ad55f438a04be3ed3453fa0e37b20df1ac7a4100c3cc100344f0a50ec0fbda515c1b61b89016f94f95f1e0f81925f0dbf8545f3d3795794d5b2
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
- --format documentation
2
1
  --color
3
2
  --require spec_helper
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in safelylaunch.gemspec
6
6
  gemspec
7
+
8
+ gem 'vcr'
9
+ gem 'webmock'
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ safelylaunch (0.1.0)
5
+ concurrent-ruby
6
+ faraday
7
+ faraday_middleware
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.5.2)
13
+ public_suffix (>= 2.0.2, < 4.0)
14
+ concurrent-ruby (1.1.4)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ faraday (0.15.4)
19
+ multipart-post (>= 1.2, < 3)
20
+ faraday_middleware (0.12.2)
21
+ faraday (>= 0.7.4, < 1.0)
22
+ hashdiff (0.3.8)
23
+ multipart-post (2.0.0)
24
+ public_suffix (3.0.3)
25
+ rake (10.5.0)
26
+ rspec (3.8.0)
27
+ rspec-core (~> 3.8.0)
28
+ rspec-expectations (~> 3.8.0)
29
+ rspec-mocks (~> 3.8.0)
30
+ rspec-core (3.8.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-expectations (3.8.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-mocks (3.8.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-support (3.8.0)
39
+ safe_yaml (1.0.4)
40
+ vcr (4.0.0)
41
+ webmock (3.5.1)
42
+ addressable (>= 2.3.6)
43
+ crack (>= 0.3.2)
44
+ hashdiff
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.16)
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+ safelylaunch!
54
+ vcr
55
+ webmock
56
+
57
+ BUNDLED WITH
58
+ 1.16.1
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Safelylaunch
2
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/safelylaunch`. 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
3
+ Ruby client for SafelyLaunch project
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,49 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Initialization
24
+ First step: require client
25
+
26
+ ```ruby
27
+ require 'safelylaunch'
28
+ ```
29
+
30
+ After that you need to create connection object and use it as a client instance dependency:
31
+
32
+ ```ruby
33
+ connection = Safelylaunch::HttpConnection.new(
34
+ api_token: 'your env token', # required
35
+ host: 'your project instance host', # optional, default 'TODO'
36
+ logger: Logger.new, # optional, default Logger.new
37
+ cache_time: 10 # optional, how long cache requests in seconds. If `nil` will disable cache. Default `nil`
38
+ )
39
+
40
+ client = Safelylaunch::Client.new(connection)
41
+ ```
42
+
43
+ ### Usage
44
+ Now we support only bool toggles without any params
45
+
46
+ ```ruby
47
+ client.enable?('toggle-key') # => bool value
48
+ ```
49
+
50
+ ### Testing
51
+ You can use mock connection for easy testing. It will store all defined toggles in memory and use it in your code:
52
+
53
+ ```ruby
54
+ connection = Safelylaunch::MockConnection.new(
55
+ toggles: {
56
+ 'toggle-key' => true,
57
+ 'disable-toggle-key' => false,
58
+ }
59
+ logger: Logger.new # optional, default Logger.new
60
+ )
61
+
62
+ client = Safelylaunch::Client.new(connection)
63
+ client.enable?('toggle-key') # => true
64
+ client.enable?('disable-toggle-key') # => false
65
+ ```
26
66
 
27
67
  ## Development
28
68
 
@@ -32,7 +72,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
72
 
33
73
  ## Contributing
34
74
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/safelylaunch. 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.
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/safelylaunch/ruby-client. 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
76
 
37
77
  ## License
38
78
 
@@ -40,4 +80,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
80
 
41
81
  ## Code of Conduct
42
82
 
43
- Everyone interacting in the Safelylaunch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/safelylaunch/blob/master/CODE_OF_CONDUCT.md).
83
+ Everyone interacting in the Safelylaunch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/safelylaunch/ruby-client/blob/master/CODE_OF_CONDUCT.md).
84
+
85
+ ## About SafelyLaunch
@@ -1,4 +1,8 @@
1
1
  require "safelylaunch/version"
2
+ require 'safelylaunch/http_connection'
3
+ require 'safelylaunch/mock_connection'
4
+ require 'safelylaunch/client'
5
+ require 'safelylaunch/http_cache'
2
6
 
3
7
  module Safelylaunch
4
8
  # Your code goes here...
@@ -0,0 +1,14 @@
1
+ module Safelylaunch
2
+ class Client
3
+ attr_reader :connection, :faraday_connection
4
+
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def enable?(toggle_key)
10
+ response = connection.get(toggle_key)
11
+ response[:enable]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ require 'concurrent/map'
2
+
3
+ module Safelylaunch
4
+ class HttpCache
5
+ Value = Struct.new(:expired_time, :data)
6
+
7
+ def initialize
8
+ @hash = Concurrent::Map.new
9
+ end
10
+
11
+ def put(key, payload, expired_in)
12
+ @hash.put_if_absent(key, Value.new(Time.new.to_i + expired_in, payload))
13
+ payload
14
+ end
15
+
16
+ def get(key)
17
+ value = @hash[key]
18
+
19
+ if value && (Time.new.to_i < value.expired_time)
20
+ value.data
21
+ else
22
+ @hash.delete(key)
23
+ nil
24
+ end
25
+ end
26
+
27
+ # only for tests
28
+ def keys
29
+ @hash.keys
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ require 'logger'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+
5
+ module Safelylaunch
6
+ class HttpConnection
7
+ attr_reader :api_token, :logger, :host, :connection, :cache_time
8
+
9
+ def initialize(api_token:, logger: Logger.new(STDOUT), cache_time: nil, host: 'http://localhost:2300')
10
+ @api_token = api_token
11
+ @logger = logger
12
+ @host = host
13
+ @cache_time = cache_time
14
+
15
+ @cache = HttpCache.new
16
+
17
+ @connection = Faraday.new(url: host, request: { timeout: 10 }) do |conn|
18
+ conn.response :json, content_type: %r{application/json}, parser_options: { symbolize_names: true }
19
+ conn.response :logger, logger
20
+
21
+ conn.adapter Faraday.default_adapter
22
+ end
23
+ end
24
+
25
+ def get(key)
26
+ cache_time ? cached_check_toggle(key) : check_toggle(key)
27
+ end
28
+
29
+ private
30
+
31
+ def cached_check_toggle(key)
32
+ @cache.get(key) || @cache.put(key, check_toggle(key), cache_time)
33
+ end
34
+
35
+ def check_toggle(key)
36
+ response = connection.get('/api/v1/check', token: api_token, key: key)
37
+ response.body
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ module Safelylaunch
2
+ class MockConnection
3
+ attr_reader :api_token, :logger, :host, :connection
4
+
5
+ def initialize(api_token:, toggles: {}, logger: Logger.new(STDOUT), host: 'http://localhost:2300')
6
+ @api_token = api_token
7
+ @logger = logger
8
+ @host = host
9
+ @connection = nil
10
+ @toggles = toggles
11
+ end
12
+
13
+ def get(key)
14
+ result = @toggles.fetch(key, false)
15
+ { key: key, enable: result }
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Safelylaunch
2
- VERSION = "0.0.0"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -21,6 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
+ spec.add_dependency "faraday"
25
+ spec.add_dependency "faraday_middleware"
26
+ spec.add_dependency "concurrent-ruby"
27
+
24
28
  spec.add_development_dependency "bundler", "~> 1.16"
25
29
  spec.add_development_dependency "rake", "~> 10.0"
26
30
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safelylaunch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Davydov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-15 00:00:00.000000000 Z
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: concurrent-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: bundler
15
57
  requirement: !ruby/object:Gem::Requirement
@@ -64,12 +106,17 @@ files:
64
106
  - ".travis.yml"
65
107
  - CODE_OF_CONDUCT.md
66
108
  - Gemfile
109
+ - Gemfile.lock
67
110
  - LICENSE.txt
68
111
  - README.md
69
112
  - Rakefile
70
113
  - bin/console
71
114
  - bin/setup
72
115
  - lib/safelylaunch.rb
116
+ - lib/safelylaunch/client.rb
117
+ - lib/safelylaunch/http_cache.rb
118
+ - lib/safelylaunch/http_connection.rb
119
+ - lib/safelylaunch/mock_connection.rb
73
120
  - lib/safelylaunch/version.rb
74
121
  - safelylaunch.gemspec
75
122
  homepage: https://github.com/safelylaunch/ruby-client