flagsmith 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35fb5ceee9613cc9425904d67f7429b82d56fcb42d3bfff4f0adb7c4d7a61ea2
4
+ data.tar.gz: a3d9a18ac02ad8a5f29f7f1ad116b8c47c563a7754f650320b693b349c3d03bf
5
+ SHA512:
6
+ metadata.gz: 8b3017bfcf9e4c81965b719e499a992954a3ffb3c4d7984eaeab61befb5074268e2507e7ce57291eeeff59f99ec6dc06ed539f5112be24a97fb0b4eed9dcda4c
7
+ data.tar.gz: 42bb17bdcf66b4ff8bfdd70abd645c79eae85b60dec89163e4450b1920e8dbec6a6ce0e5136210e7bc22622a8613328676c1f8febf5d1ad1ba061042280d0ea4
@@ -0,0 +1,57 @@
1
+ .vscode/*
2
+ !.vscode/settings.json
3
+ !.vscode/tasks.json
4
+ !.vscode/launch.json
5
+ !.vscode/extensions.json
6
+
7
+
8
+ *.gem
9
+ *.rbc
10
+ /.config
11
+ /coverage/
12
+ /InstalledFiles
13
+ /pkg/
14
+ /spec/reports/
15
+ /spec/examples.txt
16
+ /test/tmp/
17
+ /test/version_tmp/
18
+ /tmp/
19
+
20
+ # Used by dotenv library to load environment variables.
21
+ # .env
22
+
23
+ ## Specific to RubyMotion:
24
+ .dat*
25
+ .repl_history
26
+ build/
27
+ *.bridgesupport
28
+ build-iPhoneOS/
29
+ build-iPhoneSimulator/
30
+
31
+ ## Specific to RubyMotion (use of CocoaPods):
32
+ #
33
+ # We recommend against adding the Pods directory to your .gitignore. However
34
+ # you should judge for yourself, the pros and cons are mentioned at:
35
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
36
+ #
37
+ # vendor/Pods/
38
+
39
+ ## Documentation cache and generated files:
40
+ /.yardoc/
41
+ /_yardoc/
42
+ /doc/
43
+ /rdoc/
44
+
45
+ ## Environment normalization:
46
+ /.bundle/
47
+ /vendor/bundle
48
+ /lib/bundler/man/
49
+
50
+ # for a library or gem, you might want to ignore these files since the code is
51
+ # intended to run in multiple environments; otherwise, check them in:
52
+ # Gemfile.lock
53
+ # .ruby-version
54
+ # .ruby-gemset
55
+
56
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
57
+ .rvmrc
@@ -0,0 +1,8 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ Layout/HashAlignment:
4
+ AllowMultipleStyles: true
5
+ EnforcedColonStyle: separator
6
+ AllCops:
7
+ NewCops: enable
8
+ SuggestExtensions: false
@@ -0,0 +1,30 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
+ # on 2020-12-30 22:56:14 UTC using RuboCop version 1.7.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
11
+ # IgnoredMethods: refine
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - '**/*.gemspec'
15
+ - 'spec/flagsmith_spec.rb'
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: AllowedMethods.
19
+ # AllowedMethods: respond_to_missing?
20
+ Style/OptionalBooleanParameter:
21
+ Exclude:
22
+ - 'lib/flagsmith.rb'
23
+
24
+ # Offense count: 1
25
+ # Cop supports --auto-correct.
26
+ # Configuration parameters: AutoCorrect, Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
27
+ # URISchemes: http, https
28
+ Layout/LineLength:
29
+ Exclude:
30
+ - 'flagsmith.gemspec'
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |_repo_name| 'https://github.com/Flagsmith/flagsmith-ruby-client' }
6
+
7
+ # Specify your gem's dependencies in flagsmith.gemspec
8
+ gemspec
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ flagsmith (2.0.0)
5
+ faraday
6
+ faraday_middleware
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.1)
12
+ diff-lcs (1.4.4)
13
+ faraday (1.2.0)
14
+ multipart-post (>= 1.2, < 3)
15
+ ruby2_keywords
16
+ faraday_middleware (1.0.0)
17
+ faraday (~> 1.0)
18
+ gem-release (2.2.0)
19
+ multipart-post (2.1.1)
20
+ parallel (1.20.1)
21
+ parser (3.0.0.0)
22
+ ast (~> 2.4.1)
23
+ rainbow (3.0.0)
24
+ rake (13.0.3)
25
+ regexp_parser (2.0.3)
26
+ rexml (3.2.4)
27
+ rspec (3.10.0)
28
+ rspec-core (~> 3.10.0)
29
+ rspec-expectations (~> 3.10.0)
30
+ rspec-mocks (~> 3.10.0)
31
+ rspec-core (3.10.1)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-expectations (3.10.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-mocks (3.10.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-support (3.10.1)
40
+ rubocop (1.7.0)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.1.5)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml
46
+ rubocop-ast (>= 1.2.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 2.0)
49
+ rubocop-ast (1.3.0)
50
+ parser (>= 2.7.1.5)
51
+ ruby-progressbar (1.10.1)
52
+ ruby2_keywords (0.0.2)
53
+ unicode-display_width (1.7.0)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler
60
+ flagsmith!
61
+ gem-release
62
+ rake
63
+ rspec
64
+ rubocop
65
+
66
+ BUNDLED WITH
67
+ 1.17.2
data/LICENCE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2018 Solid State Technology Ltd (https://www.solidstategroup.com/) and individual contributors.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
10
+ 3. Neither the name of the Sentry nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,83 @@
1
+ <img width="100%" src="https://raw.githubusercontent.com/Flagsmith/flagsmith-frontend/master/hero.png"/>
2
+
3
+ # Flagsmith Client
4
+
5
+ The SDK clients for Ruby [https://flagsmith.com/](https://www.flagsmith.com/). Flagsmith allows you to manage feature flags and remote config across multiple projects, environments and organizations.
6
+
7
+ ## Getting Started
8
+
9
+ These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See running in production for notes on how to deploy the project on a live system.
10
+
11
+ ## Installing
12
+
13
+ ### VIA gem
14
+
15
+ `gem install flagsmith`
16
+
17
+ ## Usage
18
+
19
+ **Retrieving feature flags for your project**
20
+
21
+ **For full documentation visit [https://docs.flagsmith.com/](https://docs.flagsmith.com/)**
22
+
23
+ ```ruby
24
+ require "flagsmith"
25
+
26
+ flagsmith = Flagsmith.new("<<Your API KEY>>")
27
+
28
+ if flagsmith.get_value("font_size")
29
+ # Do something awesome with the font size
30
+ end
31
+
32
+ if flagsmith.feature_enabled?("does_not_exist")
33
+ #do something
34
+ else
35
+ #do nothing, or something else
36
+ end
37
+
38
+ ```
39
+
40
+ **Available Options**
41
+
42
+ | Property | Description | Required | Default Value | Environment Key |
43
+ | --------- | :-----------------------------------------------------------------------------------------------: | -------: | --------------------------------: | ----------------: |
44
+ | `api_key` | Defines which project environment you wish to get flags for. _example ACME Project - Staging._ | **YES** | null | FLAGSMITH_API_KEY |
45
+ | `url` | Use this property to define where you're getting feature flags from, e.g. if you're self hosting. | **NO** | https://api.flagsmith.com/api/v1/ | FLAGSMITH_URL |
46
+
47
+ **Available Functions**
48
+
49
+ | Property | Description |
50
+ | ------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------: |
51
+ | `init` | Initialize the sdk against a particular environment |
52
+ | `feature_enabled?(key)` | Get the value of a particular feature e.g. `flagsmith.feature_enabled?("powerUserFeature") // true` |
53
+ | `feature_enabled?(key, user_id, default = false)` | Get the value of a particular feature for a user e.g. `flagsmith.feature_enabled?("powerUserFeature", 1234) // true` |
54
+ | `get_value(key)` | Get the value of a particular feature e.g. `flagsmith.get_value("font_size") // 10` |
55
+ | `get_value(key, user_id, default = nil)` | Get the value of a particular feature for a specified user e.g. `flagsmith.get_value("font_size", 1234) // 15` |
56
+ | `get_flags()` | Trigger a manual fetch of the environment features, if a user is identified it will fetch their features |
57
+ | `get_flags(user_id)` | Trigger a manual fetch of the environment features with a given user id |
58
+ | `set_trait(user_id, trait, value)` | Set the value of a trait for the given user id |
59
+
60
+ **Identifying Users**
61
+
62
+ Identifying users allows you to target specific users from the [Flagsmith dashboard](https://www.flagsmith.com/).
63
+ You can include an optional user identifier as part of the `feature_enabled?` and `get_value` methods to retrieve unique user flags and variables.
64
+
65
+ ## Contributing
66
+
67
+ Please read [CONTRIBUTING.md](https://gist.github.com/kyle-ssg/c36a03aebe492e45cbd3eefb21cb0486) for details on our code of conduct, and the process for submitting pull requests to us.
68
+
69
+ ## Getting Help
70
+
71
+ If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates.
72
+
73
+ ## Get in touch
74
+
75
+ If you have any questions about our projects you can email <a href="mailto:projects@solidstategroup.com">projects@solidstategroup.com</a>.
76
+
77
+ ## Useful links
78
+
79
+ [Website](https://flagsmith.com)
80
+
81
+ [Documentation](https://docs.flagsmith.com/)
82
+
83
+ [YouTube Tutorials](https://www.youtube.com/channel/UCki7GZrOdZZcsV9rAIRchCw)
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.required_ruby_version = '>= 2.4.0'
5
+ spec.name = 'flagsmith'
6
+ spec.version = '2.0.0'
7
+ spec.authors = ['Tom Stuart', 'Brian Moelk']
8
+ spec.email = ['tom@solidstategroup.com', 'bmoelk@gmail.com']
9
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
10
+
11
+ spec.summary = 'Flagsmith - Ship features with confidence'
12
+ spec.description = 'Ruby Client for Flagsmith. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://flagsmith.com'
13
+ spec.homepage = 'https://flagsmith.com'
14
+
15
+ spec.add_development_dependency 'bundler'
16
+ spec.add_development_dependency 'gem-release'
17
+ spec.add_development_dependency 'rake'
18
+ spec.add_development_dependency 'rspec'
19
+ spec.add_development_dependency 'rubocop'
20
+ spec.add_dependency 'faraday'
21
+ spec.add_dependency 'faraday_middleware'
22
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ # Ruby client for flagsmith.com
7
+ class Flagsmith
8
+ attr_reader :flagsmith_api
9
+
10
+ def initialize(opts = {})
11
+ @opts = determine_opts(opts)
12
+
13
+ @flagsmith_api = Faraday.new(url: @opts[:url]) do |faraday|
14
+ faraday.headers['Accept'] = 'application/json'
15
+ faraday.headers['Content-Type'] = 'application/json'
16
+ faraday.headers['x-environment-key'] = @opts[:api_key]
17
+ faraday.response :json
18
+ # TODO: add timeout adjustment here
19
+ faraday.adapter Faraday.default_adapter
20
+ end
21
+ end
22
+
23
+ def get_flags(user_id = nil)
24
+ if user_id.nil?
25
+ res = @flagsmith_api.get('flags/')
26
+ flags = transform_flags(res.body).select { |flag| flag[:segment].nil? }
27
+ flags_to_hash(flags)
28
+ else
29
+ res = @flagsmith_api.get("identities/?identifier=#{user_id}")
30
+ flags_to_hash(transform_flags(res.body['flags']))
31
+ end
32
+ end
33
+
34
+ def feature_enabled?(feature, user_id = nil, default = false)
35
+ flag = get_flags(user_id)[normalize_key(feature)]
36
+ return default if flag.nil?
37
+
38
+ flag[:enabled]
39
+ end
40
+
41
+ def get_value(key, user_id = nil, default = nil)
42
+ flag = get_flags(user_id)[normalize_key(key)]
43
+ return default if flag.nil?
44
+
45
+ flag[:value]
46
+ end
47
+
48
+ def set_trait(user_id, trait, value)
49
+ raise StandardError, 'user_id cannot be nil' if user_id.nil?
50
+
51
+ trait = {
52
+ identity: { identifier: user_id },
53
+ trait_key: normalize_key(trait),
54
+ trait_value: value
55
+ }
56
+ res = @flagsmith_api.post('traits/', trait.to_json)
57
+ res.body
58
+ end
59
+
60
+ def get_traits(user_id)
61
+ return {} if user_id.nil?
62
+
63
+ res = @flagsmith_api.get("identities/?identifier=#{user_id}")
64
+ traits_to_hash(res.body)
65
+ end
66
+
67
+ # def remove_trait(user_id, trait_id)
68
+ # # Request URL: https://api.flagsmith.com/api/v1/environments/API_KEY/identities/12345/traits/54321/
69
+ # # Request Method: DELETE
70
+ # end
71
+
72
+ def transform_flags(flags)
73
+ flags.map do |flag|
74
+ {
75
+ name: flag['feature']['name'],
76
+ enabled: flag['enabled'],
77
+ value: flag['feature_state_value'],
78
+ segment: flag['feature_segment']
79
+ }
80
+ end
81
+ end
82
+
83
+ def flags_to_hash(flags)
84
+ result = {}
85
+ flags.each do |flag|
86
+ key = normalize_key(flag.delete(:name))
87
+ result[key] = flag
88
+ end
89
+ result
90
+ end
91
+
92
+ def traits_to_hash(user_flags)
93
+ result = {}
94
+ user_flags['traits']&.each do |t|
95
+ key = normalize_key(t['trait_key'])
96
+ result[key] = t['trait_value']
97
+ end
98
+ result
99
+ end
100
+
101
+ def normalize_key(key)
102
+ key.to_s.downcase
103
+ end
104
+
105
+ def determine_opts(opts)
106
+ opts = { api_key: opts } if opts.is_a? String
107
+
108
+ {
109
+ api_key: opts[:api_key] || self.class.api_key,
110
+ url: opts[:url] || self.class.api_url
111
+ }
112
+ end
113
+
114
+ def self.api_key
115
+ ENV['FLAGSMITH_API_KEY']
116
+ end
117
+
118
+ def self.api_url
119
+ ENV.fetch('FLAGSMITH_URL', 'https://api.flagsmith.com/api/v1/')
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flagsmith
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Stuart
8
+ - Brian Moelk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-01-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: gem-release
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: faraday
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: faraday_middleware
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: Ruby Client for Flagsmith. Ship features with confidence using feature
113
+ flags and remote config. Host yourself or use our hosted version at https://flagsmith.com
114
+ email:
115
+ - tom@solidstategroup.com
116
+ - bmoelk@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rubocop.yml"
123
+ - ".rubocop_todo.yml"
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENCE
127
+ - README.md
128
+ - Rakefile
129
+ - flagsmith.gemspec
130
+ - lib/flagsmith.rb
131
+ homepage: https://flagsmith.com
132
+ licenses: []
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 2.4.0
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubygems_version: 3.0.3
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Flagsmith - Ship features with confidence
153
+ test_files: []