Skip to content

Commit b8be6b6

Browse files
authored
Merge pull request #2 from bmoelk/preserve-initialize-compatibility
preserved initialize compatibility, updated README, added rubocop
2 parents 98459bb + e92291f commit b8be6b6

File tree

5 files changed

+144
-26
lines changed

5 files changed

+144
-26
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inherit_from: .rubocop_todo.yml
2+
Layout/HashAlignment:
3+
AllowMultipleStyles: true
4+
EnforcedColonStyle: separator
5+

.rubocop_todo.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2020-01-16 04:57:18 -1000 using RuboCop version 0.79.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: 3
10+
# Cop supports --auto-correct.
11+
# Configuration parameters: EnforcedStyle, IndentationWidth.
12+
# SupportedStyles: special_inside_parentheses, consistent, align_braces
13+
Layout/FirstHashElementIndentation:
14+
Exclude:
15+
- 'lib/bullet_train.rb'
16+
- 'spec/bullet_train_spec.rb'
17+
18+
# Offense count: 8
19+
# Cop supports --auto-correct.
20+
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
21+
# SupportedHashRocketStyles: key, separator, table
22+
# SupportedColonStyles: key, separator, table
23+
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
24+
Layout/HashAlignment:
25+
Exclude:
26+
- 'lib/bullet_train.rb'
27+
- 'spec/bullet_train_spec.rb'
28+
29+
# Offense count: 2
30+
# Cop supports --auto-correct.
31+
Layout/SpaceAroundKeyword:
32+
Exclude:
33+
- 'spec/bullet_train_spec.rb'
34+
35+
# Offense count: 1
36+
# Cop supports --auto-correct.
37+
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
38+
# SupportedStyles: space, no_space
39+
# SupportedStylesForEmptyBraces: space, no_space
40+
Layout/SpaceInsideBlockBraces:
41+
Exclude:
42+
- 'Gemfile'
43+
44+
# Offense count: 1
45+
# Cop supports --auto-correct.
46+
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
47+
Lint/UnusedBlockArgument:
48+
Exclude:
49+
- 'Gemfile'
50+
51+
# Offense count: 1
52+
# Configuration parameters: CountComments, ExcludedMethods.
53+
# ExcludedMethods: refine
54+
Metrics/BlockLength:
55+
Max: 111
56+
57+
# Offense count: 1
58+
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
59+
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
60+
Naming/FileName:
61+
Exclude:
62+
- 'lib/bullet-train.rb'
63+
64+
# Offense count: 3
65+
# Cop supports --auto-correct.
66+
# Configuration parameters: EnforcedStyle.
67+
# SupportedStyles: always, never
68+
Style/FrozenStringLiteralComment:
69+
Exclude:
70+
- 'Gemfile'
71+
- 'Rakefile'
72+
- 'bullet-train-ruby.gemspec'
73+
74+
# Offense count: 1
75+
# Cop supports --auto-correct.
76+
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
77+
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
78+
Style/HashSyntax:
79+
Exclude:
80+
- 'Rakefile'
81+
82+
# Offense count: 13
83+
# Cop supports --auto-correct.
84+
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
85+
# SupportedStyles: single_quotes, double_quotes
86+
Style/StringLiterals:
87+
Exclude:
88+
- 'Gemfile'
89+
- 'Rakefile'
90+
- 'bullet-train-ruby.gemspec'
91+
92+
# Offense count: 11
93+
# Cop supports --auto-correct.
94+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
95+
# URISchemes: http, https
96+
Layout/LineLength:
97+
Max: 188

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ These instructions will get you a copy of the project up and running on your loc
1111

1212
### VIA gem
1313
```gem install bullet-train-client```
14-
14+
1515
## Usage
1616
**Retrieving feature flags for your project**
1717

@@ -36,22 +36,20 @@ end
3636

3737
| Property | Description | Required | Default Value |
3838
| ------------- |:-------------:| -----:| -----:|
39-
| ```environmentID``` | Defines which project environment you wish to get flags for. *example ACME Project - Staging.* | **YES** | null
40-
| ```onError``` | Callback function on failure to retrieve flags. ``` (error)=>{...} ``` | **NO** | null
41-
| ```defaultFlags``` | Defines the default flags if there are any | **NO** | null
42-
| ```api``` | Use this property to define where you're getting feature flags from, e.g. if you're self hosting. | **NO** | https://bullet-train-api.dokku1.solidstategroup.com/api/v1/
39+
| ```api_key``` | Defines which project environment you wish to get flags for. *example ACME Project - Staging.* | **YES** | null
40+
| ```url``` | Use this property to define where you're getting feature flags from, e.g. if you're self hosting. | **NO** | https://bullet-train-api.dokku1.solidstategroup.com/api/v1/
4341

4442
**Available Functions**
4543

46-
| Property | Description |
44+
| Property | Description |
4745
| ------------- |:-------------:|
4846
| ```init``` | Initialise the sdk against a particular environment
49-
| ```hasFeature(key)``` | Get the value of a particular feature e.g. ```bulletTrain.hasFeature("powerUserFeature") // true```
50-
| ```hasFeature(key, userId)``` | Get the value of a particular feature for a user e.g. ```bulletTrain.hasFeature("powerUserFeature", 1234) // true```
51-
| ```getValue(key)``` | Get the value of a particular feature e.g. ```bulletTrain.getValue("font_size") // 10```
52-
| ```getValue(keym userId)``` | Get the value of a particular feature for a specificed user e.g. ```bulletTrain.getValue("font_size", 1234) // 15```
53-
| ```getFlags()``` | Trigger a manual fetch of the environment features, if a user is identified it will fetch their features
54-
| ```getFlagsForUser(1234)``` | Trigger a manual fetch of the environment features with a given user id
47+
| ```feature_enabled?(key)``` | Get the value of a particular feature e.g. ```bulletTrain.feature_enabled?("powerUserFeature") // true```
48+
| ```feature_enabled?(key, user_id, default = false)``` | Get the value of a particular feature for a user e.g. ```bulletTrain.feature_enabled?("powerUserFeature", 1234) // true```
49+
| ```get_value(key)``` | Get the value of a particular feature e.g. ```bulletTrain.get_value("font_size") // 10```
50+
| ```get_value(key, user_id, default = nil)``` | Get the value of a particular feature for a specificed user e.g. ```bulletTrain.get_value("font_size", 1234) // 15```
51+
| ```get_flags()``` | Trigger a manual fetch of the environment features, if a user is identified it will fetch their features
52+
| ```get_flags(user_id)``` | Trigger a manual fetch of the environment features with a given user id
5553

5654

5755
**Identifying users**
@@ -66,7 +64,7 @@ Please read [CONTRIBUTING.md](https://gist.github.com/kyle-ssg/c36a03aebe492e45c
6664

6765
## Getting Help
6866

69-
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.
67+
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.
7068

7169
## Get in touch
7270

bullet-train-ruby.gemspec

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
Gem::Specification.new do |spec|
22
spec.name = "bullet-train-client"
3-
spec.version = "1.0.0"
4-
spec.authors = ["Tom Stuart"]
5-
spec.email = ["[email protected]"]
3+
spec.version = "2.0.0"
4+
spec.authors = ["Tom Stuart", "Brian Moelk"]
5+
66
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
77

88
spec.summary = "Bullet Train - Ship features with confidence"
99
spec.description = "Ruby Client for Bullet-Train. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://bullet-train.io"
1010
spec.homepage = "https://bullet-train.io"
1111

1212
spec.add_development_dependency 'bundler'
13+
spec.add_development_dependency 'gem-release'
1314
spec.add_development_dependency 'rake'
1415
spec.add_development_dependency 'rspec'
16+
spec.add_development_dependency 'rubocop'
1517
spec.add_dependency 'faraday'
1618
end

lib/bullet_train.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# frozen_string_literal: true
2+
23
require 'faraday'
34

5+
# Ruby client for bullet-train.io
46
class BulletTrain
57
attr_reader :bt_api
68

79
def initialize(opts = {})
8-
@opts = {
9-
api_key: opts[:api_key] || self.class.api_key,
10-
url: opts[:url] || self.class.api_url
11-
}
10+
@opts = determine_opts(opts)
1211

1312
@bt_api = Faraday.new(url: @opts[:url]) do |faraday|
1413
faraday.headers['Accept'] = 'application/json'
@@ -48,7 +47,12 @@ def get_value(key, user_id = nil, default = nil)
4847
def set_trait(user_id, trait, value)
4948
raise StandardError, 'user_id cannot be nil' if user_id.nil?
5049

51-
res = @bt_api.post('traits/', { identity: { identifier: user_id }, trait_key: normalize_key(trait), trait_value: value }.to_json)
50+
trait = {
51+
identity: { identifier: user_id },
52+
trait_key: normalize_key(trait),
53+
trait_value: value
54+
}
55+
res = @bt_api.post('traits/', trait.to_json)
5256
res.body
5357
end
5458

@@ -86,18 +90,30 @@ def flags_to_hash(flags)
8690

8791
def traits_to_hash(user_flags)
8892
result = {}
89-
user_flags['traits']&.each { |t| result[normalize_key(t['trait_key'])] = t['trait_value'] }
93+
user_flags['traits']&.each do |t|
94+
key = normalize_key(t['trait_key'])
95+
result[key] = t['trait_value']
96+
end
9097
result
9198
end
9299

93100
def normalize_key(key)
94101
key.to_s.downcase
95102
end
96103

97-
alias :hasFeature :feature_enabled?
98-
alias :getValue :get_value
99-
alias :getFlags :get_flags
100-
alias :getFlagsForUser :get_flags
104+
def determine_opts(opts)
105+
opts = { api_key: opts } if opts.is_a? String
106+
107+
{
108+
api_key: opts[:api_key] || self.class.api_key,
109+
url: opts[:url] || self.class.api_url
110+
}
111+
end
112+
113+
alias hasFeature feature_enabled?
114+
alias getValue get_value
115+
alias getFlags get_flags
116+
alias getFlagsForUser get_flags
101117

102118
def self.api_key
103119
ENV['BULLETTRAIN_API_KEY']

0 commit comments

Comments
 (0)