goflippy-ruby 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ff44c7c28fe21182366e2d89e03bb42289db183
4
+ data.tar.gz: 6b60d50ce3b985963fd102649b4c5c81abf59dcd
5
+ SHA512:
6
+ metadata.gz: a8e2146ef06ef3147e59fca1d7790abdcd656ff89875d71bd45915a6a10314eacca1b720c59147b1fcef231bec40ee3a870d3a49886e0cbeec67756adeae4e87
7
+ data.tar.gz: 5d2fba85118ccf23fd097ddcf87315dabe6813d751f7e2869478cff0783b2d51396a3d73e19c678eb36d3e85889ed71f682e489d334fdf03b684da938ebf7b1c
data/.gitignore ADDED
@@ -0,0 +1,124 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ ### https://raw.github.com/github/gitignore/8edb8a95c4c4b3dce71a378aaaf89275510b9cef/Ruby.gitignore
11
+
12
+ *.gem
13
+ *.rbc
14
+ /.config
15
+ /coverage/
16
+ /InstalledFiles
17
+ /pkg/
18
+ /spec/reports/
19
+ /spec/examples.txt
20
+ /test/tmp/
21
+ /test/version_tmp/
22
+ /tmp/
23
+
24
+ # Used by dotenv library to load environment variables.
25
+ # .env
26
+
27
+ ## Specific to RubyMotion:
28
+ .dat*
29
+ .repl_history
30
+ build/
31
+ *.bridgesupport
32
+ build-iPhoneOS/
33
+ build-iPhoneSimulator/
34
+
35
+ ## Specific to RubyMotion (use of CocoaPods):
36
+ #
37
+ # We recommend against adding the Pods directory to your .gitignore. However
38
+ # you should judge for yourself, the pros and cons are mentioned at:
39
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
40
+ #
41
+ # vendor/Pods/
42
+
43
+ ## Documentation cache and generated files:
44
+ /.yardoc/
45
+ /_yardoc/
46
+ /doc/
47
+ /rdoc/
48
+
49
+ ## Environment normalization:
50
+ /.bundle/
51
+ /vendor/bundle
52
+ /lib/bundler/man/
53
+
54
+ # for a library or gem, you might want to ignore these files since the code is
55
+ # intended to run in multiple environments; otherwise, check them in:
56
+ # Gemfile.lock
57
+ # .ruby-version
58
+ # .ruby-gemset
59
+
60
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
61
+ .rvmrc
62
+
63
+
64
+ ### https://raw.github.com/github/gitignore/8edb8a95c4c4b3dce71a378aaaf89275510b9cef/Global/macOS.gitignore
65
+
66
+ # General
67
+ *.DS_Store
68
+ .AppleDouble
69
+ .LSOverride
70
+
71
+ # Icon must end with two \r
72
+ Icon
73
+
74
+ # Thumbnails
75
+ ._*
76
+
77
+ # Files that might appear in the root of a volume
78
+ .DocumentRevisions-V100
79
+ .fseventsd
80
+ .Spotlight-V100
81
+ .TemporaryItems
82
+ .Trashes
83
+ .VolumeIcon.icns
84
+ .com.apple.timemachine.donotpresent
85
+
86
+ # Directories potentially created on remote AFP share
87
+ .AppleDB
88
+ .AppleDesktop
89
+ Network Trash Folder
90
+ Temporary Items
91
+ .apdisk
92
+
93
+
94
+ ### https://raw.github.com/github/gitignore/9a3dea4649365dc90bef39d1e6f76d2e47060d43/Global/JetBrains.gitignore
95
+
96
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
97
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
98
+
99
+ .idea
100
+
101
+ # CMake
102
+ cmake-build-debug/
103
+
104
+ ## File-based project format:
105
+ *.iws
106
+
107
+ ## Plugin-specific files:
108
+
109
+ # IntelliJ
110
+ /out/
111
+
112
+ # JIRA plugin
113
+ atlassian-ide-plugin.xml
114
+
115
+ # Cursive Clojure plugin
116
+ .idea/replstate.xml
117
+
118
+ # Crashlytics plugin (for Android Studio and IntelliJ)
119
+ com_crashlytics_export_strings.xml
120
+ crashlytics.properties
121
+ crashlytics-build.properties
122
+ fabric.properties
123
+
124
+
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Style/FrozenStringLiteralComment:
4
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in goflippy-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 neko-neko
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,37 @@
1
+ # Goflippy-ruby
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/goflippy/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'goflippy-ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install goflippy-ruby
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/neko-neko/goflippy-ruby.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'goflippy'
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__)
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'goflippy/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'goflippy-ruby'
9
+ spec.version = GoFlippy::VERSION
10
+ spec.authors = ['neko-neko']
11
+ spec.email = ['seiichi.nishikata456@gmail.com']
12
+ spec.summary = 'Goflippy Ruby SDK'
13
+ spec.homepage = 'https://github.com/neko-neko/goflippy-ruby'
14
+ spec.license = 'MIT'
15
+ spec.require_paths = ['lib']
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.add_dependency 'concurrent-ruby', '~> 1.0.5'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.15'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.6.0'
25
+ spec.add_development_dependency 'rubocop', '~> 0.49.1'
26
+
27
+ spec.required_ruby_version = '>= 2.3.0'
28
+ end
@@ -0,0 +1,23 @@
1
+ module GoFlippy
2
+ class Client
3
+ include Logger
4
+
5
+ def initialize(api_key, opts = {})
6
+ @api_key = api_key
7
+ @config = opts&.empty? ? Config.default : Config.new(opts)
8
+ @store = MemoryStore.new
9
+ @http_client = HttpClient.new(@api_key, @config)
10
+ @poller = Poller.new(@config.polling_interval, @http_client, @store)
11
+ end
12
+
13
+ def start
14
+ Logger.info('Start GoFlippy client')
15
+ @poller.start
16
+ end
17
+
18
+ def enabled?(key, uid, default)
19
+ uids = @store.find(key)
20
+ uids&.dig(uid.to_sym) || default
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module GoFlippy
2
+ class Config
3
+ include Logger
4
+ DEFAULT_API_URI = 'http://localhost'
5
+ DEFAULT_OPEN_TIMEOUT = 3
6
+ DEFAULT_READ_TIMEOUT = 10
7
+ DEFAULT_POLLING_INTERVAL = 300
8
+
9
+ attr_reader :api_uri
10
+ attr_reader :open_timeout
11
+ attr_reader :read_timeout
12
+ attr_reader :polling_interval
13
+
14
+ def initialize(opts = {})
15
+ @api_uri = (opts[:api_uri] || DEFAULT_API_URI).chomp('/')
16
+ @open_timeout = (opts[:open_timeout] || DEFAULT_OPEN_TIMEOUT)
17
+ @read_timeout = (opts[:read_timeout] || DEFAULT_READ_TIMEOUT)
18
+ @polling_interval = (opts[:polling_interval] || DEFAULT_POLLING_INTERVAL)
19
+ Logger.logger = opts[:logger] if opts[:logger]
20
+ end
21
+
22
+ def self.default
23
+ Config.new
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,70 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module GoFlippy
6
+ class HttpClient
7
+ include Logger
8
+ HTTP_API_KEY_HEADER = 'X-Api-Key'
9
+
10
+ def initialize(api_key, opts = {})
11
+ @api_key = api_key
12
+ @api_uri = opts.api_uri
13
+ @open_timeout = opts.open_timeout
14
+ @read_timeout = opts.read_timeout
15
+ end
16
+
17
+ def get(path, params = {})
18
+ uri = uri(path, params)
19
+
20
+ req = Net::HTTP::Get.new(uri.request_uri)
21
+ req['Accept'] = 'application/json'
22
+ req[HTTP_API_KEY_HEADER] = 'application/json'
23
+ request(uri, req)
24
+ end
25
+
26
+ def post(path, params = {})
27
+ uri = uri(path, params)
28
+
29
+ req = Net::HTTP::Post.new(uri.request_uri)
30
+ req['Accept'] = 'application/json'
31
+ req['Content-Type'] = 'application/json'
32
+ req[HTTP_API_KEY_HEADER] = 'application/json'
33
+ req.body = params.to_json unless params.empty?
34
+ request(uri, req)
35
+ end
36
+
37
+ private
38
+
39
+ def uri(path, params = {})
40
+ if params.empty?
41
+ URI.parse("#{@api_uri}#{path}")
42
+ else
43
+ encoded_params = URI.encode_www_form(params)
44
+ URI.parse("#{@api_uri}#{path}/?#{encoded_params}")
45
+ end
46
+ end
47
+
48
+ def request(uri, req)
49
+ begin
50
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
51
+ http.open_timeout = @open_timeout
52
+ http.read_timeout = @read_timeout
53
+ http.request(req)
54
+ end
55
+ case response
56
+ when Net::HTTPSuccess
57
+ json = response.body
58
+ JSON.parse(json, symbolize_names: true)
59
+ when Net::HTTPRedirection
60
+ request(URI.parse(response['location']), req)
61
+ else
62
+ Logger.error(response.value)
63
+ end
64
+ rescue => e
65
+ Logger.error(e)
66
+ end
67
+ response
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,41 @@
1
+ require 'logger'
2
+
3
+ module GoFlippy
4
+ module Logger
5
+ MESSAGE_PREFIX = '[GoFippy]'
6
+
7
+ def self.logger
8
+ @logger ||= defined?(Rails) && Rails.respond_to?(:logger) ?
9
+ Rails.logger :
10
+ ::Logger.new($stdout, level: ::Logger::DEBUG)
11
+ end
12
+
13
+ def self.logger=(logger)
14
+ @logger = logger
15
+ end
16
+
17
+ def self.debug(message)
18
+ self.logger.debug("#{MESSAGE_PREFIX} #{message}")
19
+ end
20
+
21
+ def self.info(message)
22
+ self.logger.info("#{MESSAGE_PREFIX} #{message}")
23
+ end
24
+
25
+ def self.warn(message)
26
+ self.logger.warn("#{MESSAGE_PREFIX} #{message}")
27
+ end
28
+
29
+ def self.error(message)
30
+ self.logger.error("#{MESSAGE_PREFIX} #{message}")
31
+ end
32
+
33
+ def self.fatal(message)
34
+ self.logger.fatal("#{MESSAGE_PREFIX} #{message}")
35
+ end
36
+
37
+ def self.unknown(message)
38
+ self.logger.unknown("#{MESSAGE_PREFIX} #{message}")
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ require 'concurrent'
2
+
3
+ module GoFlippy
4
+ class MemoryStore
5
+ def initialize
6
+ @store = Concurrent::Hash.new({})
7
+ end
8
+
9
+ def find(key)
10
+ @store[key]
11
+ end
12
+
13
+ def put(key, val)
14
+ @store[key.to_sym] = val
15
+ end
16
+
17
+ def refresh!(keys)
18
+ @store = Concurrent::Hash.new(@store.select { |k, _| keys.include?(k) })
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module GoFlippy
2
+ class Poller
3
+ include Logger
4
+
5
+ def initialize(polling_interval, http_client, store)
6
+ @polling_interval = polling_interval
7
+ @http_client = http_client
8
+ @store = store
9
+ end
10
+
11
+ def start
12
+ Logger.debug('Start polling worker process')
13
+ Worker.create(@polling_interval) do
14
+ keys = []
15
+ @http_client.get('/features')&.each do |feature|
16
+ uids = @store.find(feature[:key])
17
+ uids.each do |uid|
18
+ enabled = !!@http_client.get("/users/#{uid}/features/#{key}")&.dig(uid.to_sym)
19
+ @store.put(feature[:key], { uid: uid.to_sym, enabled: enabled })
20
+ end
21
+ keys << feature[:key]
22
+ end
23
+ Logger.info("Polling finished. #{keys.inspect}")
24
+ @store.refresh!(keys)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module GoFlippy
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'thread'
2
+
3
+ module GoFlippy
4
+ class Worker
5
+ include Logger
6
+
7
+ def self.create(interval, &block)
8
+ Thread.new do
9
+ loop do
10
+ begin
11
+ started_at = Time.now
12
+ yield
13
+ sleep(interval) if (interval - (Time.now - started_at)).positive?
14
+ rescue StandardError => e
15
+ Logger.error(e)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
data/lib/goflippy.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'goflippy/logger'
2
+ require 'goflippy/config'
3
+ require 'goflippy/client'
4
+ require 'goflippy/http_client'
5
+ require 'goflippy/memory_store'
6
+ require 'goflippy/poller'
7
+ require 'goflippy/version'
8
+ require 'goflippy/worker'
9
+
10
+ module GoFlippy
11
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goflippy-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - neko-neko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.6.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.6.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.49.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.49.1
83
+ description:
84
+ email:
85
+ - seiichi.nishikata456@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - goflippy-ruby.gemspec
99
+ - lib/goflippy.rb
100
+ - lib/goflippy/client.rb
101
+ - lib/goflippy/config.rb
102
+ - lib/goflippy/http_client.rb
103
+ - lib/goflippy/logger.rb
104
+ - lib/goflippy/memory_store.rb
105
+ - lib/goflippy/poller.rb
106
+ - lib/goflippy/version.rb
107
+ - lib/goflippy/worker.rb
108
+ homepage: https://github.com/neko-neko/goflippy-ruby
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.3.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.12
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Goflippy Ruby SDK
132
+ test_files: []