Skip to content

Commit

Permalink
Merge branch 'master' into sdkkey
Browse files Browse the repository at this point in the history
  • Loading branch information
sigewuzhere committed Apr 9, 2020
2 parents 53044ee + 2f98c9e commit 99b91a5
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 108 deletions.
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ language: ruby

rvm:
- 2.2
- 2.3
- 2.4
- 2.5
- 2.6
- 2.7

before_install:
- gem install bundler --version=1.17.3

script: bundle exec rake

Expand All @@ -11,4 +19,6 @@ deploy:
gemspec: "configcat.gemspec"
gem: "configcat"
on:
tags: true
tags: true
branch: master
rvm: 2.2
8 changes: 2 additions & 6 deletions DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
bin/rspec --format doc
```
2. Increase the version in the `lib/configcat/version.rb` file.
3. Update Gemfile.lock by running:
```bash
4. bundle install
```
5. Commit & Push
3. Commit & Push
## Publish
Use the **same version** for the git tag as in the `version.rb`.
- Via git tag
Expand All @@ -34,4 +30,4 @@ Make sure the new version is available on [RubyGems](https://rubygems.org/gems/c
Update and test sample apps with the new SDK version.
```bash
gem update configcat
```
```
58 changes: 0 additions & 58 deletions Gemfile.lock

This file was deleted.

13 changes: 7 additions & 6 deletions configcat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Gem::Specification.new do |spec|

spec.files = Dir['lib/*'] + Dir['lib/**/*.rb']
spec.require_paths = ["lib"]
spec.required_ruby_version = "~> 2.2"
spec.required_ruby_version = ">= 2.2"

spec.add_dependency "concurrent-ruby"
spec.add_dependency "semantic"
spec.add_dependency "concurrent-ruby", "~> 1.1"
spec.add_dependency "semantic", "~> 1.6"

spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rake"
spec.add_development_dependency "coveralls"
end
spec.add_development_dependency "rake", "~> 12.3"
spec.add_development_dependency "coveralls", "~> 0.8"
spec.add_development_dependency "webmock", "~> 3.0"
end
31 changes: 17 additions & 14 deletions lib/configcat/autopollingcachepolicy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get()

def force_refresh()
begin
configuration = @_config_fetcher.get_configuration_json()
configuration_response = @_config_fetcher.get_configuration_json()

begin
@_lock.acquire_read_lock()
Expand All @@ -57,20 +57,23 @@ def force_refresh()
@_lock.release_read_lock()
end

if configuration != old_configuration
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_initialized = true
ensure
@_lock.release_write_lock()
end
begin
if !@_on_configuration_changed_callback.equal?(nil)
@_on_configuration_changed_callback.()
if configuration_response.is_fetched()
configuration = configuration_response.json()
if configuration != old_configuration
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_initialized = true
ensure
@_lock.release_write_lock()
end
begin
if !@_on_configuration_changed_callback.equal?(nil)
@_on_configuration_changed_callback.()
end
rescue Exception => e
ConfigCat.logger.error("Exception in on_configuration_changed_callback: #{e.class}:'#{e}'")
end
rescue Exception => e
ConfigCat.logger.error("Exception in on_configuration_changed_callback: #{e.class}:'#{e}'")
end
end

Expand Down
36 changes: 30 additions & 6 deletions lib/configcat/configfetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@ module ConfigCat
BASE_PATH = "configuration-files/"
BASE_EXTENSION = "/config_v4.json"

class FetchResponse
def initialize(response)
@_response = response
end

# Returns the json-encoded content of a response, if any
def json()
return JSON.parse(@_response.body)
end

# Gets whether a new configuration value was fetched or not
def is_fetched()
code = @_response.code.to_i
return 200 <= code && code < 300
end

# Gets whether the fetch resulted a '304 Not Modified' or not
def is_not_modified()
return @_response.code == "304"
end
end

class CacheControlConfigFetcher < ConfigFetcher
def initialize(sdk_key, mode, base_url=nil, proxy_address=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil)
@_sdk_key = sdk_key
@_etag = nil
@_etag = ""
@_headers = {"User-Agent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "X-ConfigCat-UserAgent" => ((("ConfigCat-Ruby/") + mode) + ("-")) + VERSION, "Content-Type" => "application/json"}
if !base_url.equal?(nil)
@_base_url = base_url.chomp("/")
Expand All @@ -26,16 +48,18 @@ def initialize(sdk_key, mode, base_url=nil, proxy_address=nil, proxy_port=nil, p
@_http.read_timeout = 30 # in seconds
end

# Returns the FetchResponse object contains configuration json Dictionary
def get_configuration_json()
ConfigCat.logger.debug "Fetching configuration from ConfigCat"
uri = URI.parse((((@_base_url + ("/")) + BASE_PATH) + @_sdk_key) + BASE_EXTENSION)
@_headers["If-None-Match"] = @_etag unless @_etag.nil?
request = Net::HTTP::Get.new(uri.request_uri, @_headers)
headers = @_headers
headers["If-None-Match"] = @_etag unless @_etag.empty?
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = @_http.request(request)
json = JSON.parse(response.body)
@_etag = response["ETag"]
etag = response["ETag"]
@_etag = etag unless etag.nil? || etag.empty?
ConfigCat.logger.debug "ConfigCat configuration json fetch response code:#{response.code} Cached:#{response['ETag']}"
return json
return FetchResponse.new(response)
end

def close()
Expand Down
17 changes: 10 additions & 7 deletions lib/configcat/lazyloadingcachepolicy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ def get()

def force_refresh()
begin
configuration = @_config_fetcher.get_configuration_json()
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_last_updated = Time.now.utc
ensure
@_lock.release_write_lock()
configuration_response = @_config_fetcher.get_configuration_json()
if configuration_response.is_fetched()
configuration = configuration_response.json()
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
@_last_updated = Time.now.utc
ensure
@_lock.release_write_lock()
end
end
rescue StandardError => e
ConfigCat.logger.error("Double-check your SDK Key at https://app.configcat.com/sdkkey.")
Expand Down
15 changes: 9 additions & 6 deletions lib/configcat/manualpollingcachepolicy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ def get()

def force_refresh()
begin
configuration = @_config_fetcher.get_configuration_json()
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
ensure
@_lock.release_write_lock()
configuration_response = @_config_fetcher.get_configuration_json()
if configuration_response.is_fetched()
configuration = configuration_response.json()
begin
@_lock.acquire_write_lock()
@_config_cache.set(configuration)
ensure
@_lock.release_write_lock()
end
end
rescue StandardError => e
ConfigCat.logger.error("Double-check your SDK Key at https://app.configcat.com/sdkkey.")
Expand Down
2 changes: 1 addition & 1 deletion lib/configcat/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConfigCat
VERSION = "2.0.3"
VERSION = "2.0.5"
end
60 changes: 60 additions & 0 deletions spec/configcat/configfetcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'
require 'configcat/configfetcher'
require_relative 'mocks'

RSpec.describe ConfigCat::CacheControlConfigFetcher do
it "test_simple_fetch_success" do
uri_template = Addressable::Template.new "https://{base_url}/{base_path}/{api_key}/{base_ext}"
WebMock.stub_request(:get, uri_template)
.with(
body: "",
headers: {
'Accept' => '*/*',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
}
)
.to_return(status: 200, body: TEST_JSON, headers: {})

fetcher = ConfigCat::CacheControlConfigFetcher.new("", "m")
fetch_response = fetcher.get_configuration_json()
expect(fetch_response.is_fetched()).to be true
expect(fetch_response.json()).to eq JSON.parse(TEST_JSON)
end

it "test_fetch_not_modified_etag" do
etag = "test"
uri_template = Addressable::Template.new "https://{base_url}/{base_path}/{api_key}/{base_ext}"
WebMock.stub_request(:get, uri_template)
.with(
body: "",
headers: {
'Accept' => '*/*',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'
}
)
.to_return(status: 200, body: TEST_JSON, headers: {"ETag" => etag})
fetcher = ConfigCat::CacheControlConfigFetcher.new("", "m")
fetch_response = fetcher.get_configuration_json()
expect(fetch_response.is_fetched()).to be true
expect(fetch_response.json()).to eq JSON.parse(TEST_JSON)

WebMock.stub_request(:get, uri_template)
.with(
body: "",
headers: {
'Accept' => '*/*',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'If-None-Match' => etag
}
)
.to_return(status: 304, body: "", headers: {})
fetch_response = fetcher.get_configuration_json()
expect(fetch_response.is_fetched()).to be false
expect(fetch_response.is_not_modified()).to be true

WebMock.reset!
end
end
18 changes: 15 additions & 3 deletions spec/configcat/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@

include ConfigCat

class FetchResponseMock
def initialize(json)
@json = json
end
def json()
return @json
end
def is_fetched()
return true
end
end

class ConfigFetcherMock < ConfigFetcher
def initialize()
@_call_count = 0
@_configuration = TEST_JSON
end
def get_configuration_json()
@_call_count = @_call_count + 1
return @_configuration
return FetchResponseMock.new(@_configuration)
end
def set_configuration_json(value)
@_configuration = value
Expand Down Expand Up @@ -43,7 +55,7 @@ def initialize(wait_seconds)
end
def get_configuration_json()
sleep(@_wait_seconds)
return TEST_JSON
return FetchResponseMock.new(TEST_JSON)
end
def close()
end
Expand All @@ -55,7 +67,7 @@ def initialize()
end
def get_configuration_json()
@_value += 10
return @_value
return FetchResponseMock.new(@_value)
end
def close()
end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'configcat'
require 'coveralls'
require 'webmock/rspec'
WebMock.allow_net_connect!
ConfigCat.logger.level = Logger::WARN
Coveralls.wear!

0 comments on commit 99b91a5

Please sign in to comment.