Skip to content

Commit fb7ba88

Browse files
authored
Merge pull request #2 from bmoelk/fix-faraday-json-middleware
Fix faraday json middleware
2 parents ef56f8f + fbd59be commit fb7ba88

File tree

5 files changed

+147
-27
lines changed

5 files changed

+147
-27
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: 13 additions & 15 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

@@ -21,11 +21,11 @@ require "bullet-train-ruby-client"
2121

2222
bt = BulletTrain.new("<<Your API KEY>>")
2323

24-
if bt.getValue("font_size")
24+
if bt.get_value("font_size")
2525
# Do something awesome with the font size
2626
end
2727

28-
if bt.hasFeature("does_not_exist")
28+
if bt.feature_enabled?("does_not_exist")
2929
#do something
3030
else
3131
#do nothing, or something else
@@ -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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
Gem::Specification.new do |spec|
22
spec.name = "bullet-train-client"
33
spec.version = "1.1.0"
4-
spec.authors = ["Tom Stuart"]
5-
spec.email = ["[email protected]"]
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'
18+
spec.add_dependency 'faraday_middleware'
1619
end

lib/bullet_train.rb

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
2+
23
require 'faraday'
4+
require 'faraday_middleware'
35

6+
# Ruby client for bullet-train.io
47
class BulletTrain
58
attr_reader :bt_api
69

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

1313
@bt_api = Faraday.new(url: @opts[:url]) do |faraday|
1414
faraday.headers['Accept'] = 'application/json'
@@ -48,7 +48,12 @@ def get_value(key, user_id = nil, default = nil)
4848
def set_trait(user_id, trait, value)
4949
raise StandardError, 'user_id cannot be nil' if user_id.nil?
5050

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

@@ -86,18 +91,30 @@ def flags_to_hash(flags)
8691

8792
def traits_to_hash(user_flags)
8893
result = {}
89-
user_flags['traits']&.each { |t| result[normalize_key(t['trait_key'])] = t['trait_value'] }
94+
user_flags['traits']&.each do |t|
95+
key = normalize_key(t['trait_key'])
96+
result[key] = t['trait_value']
97+
end
9098
result
9199
end
92100

93101
def normalize_key(key)
94102
key.to_s.downcase
95103
end
96104

97-
alias :hasFeature :feature_enabled?
98-
alias :getValue :get_value
99-
alias :getFlags :get_flags
100-
alias :getFlagsForUser :get_flags
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+
alias hasFeature feature_enabled?
115+
alias getValue get_value
116+
alias getFlags get_flags
117+
alias getFlagsForUser get_flags
101118

102119
def self.api_key
103120
ENV['BULLETTRAIN_API_KEY']

0 commit comments

Comments
 (0)