Skip to content

Commit

Permalink
Add caching support
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavshatz committed Jun 30, 2020
1 parent 5fe6dbc commit 5a9144c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .semver
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
:major: 2
:major: 3
:minor: 0
:patch: 1
:patch: 0
:special: ''
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.0.0] - 2020-07-10
### Changed
- Updated to Ruby 2.7.1
- Add cache support.

## [2.0.1] - 2018-09-06
### Changed
- Changed `IconRetriever` class' error class from `ArgumentError` to `ServiceError` when responses http code from Noun Project API are other than `200` **BREAKING CHANGE**
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ PATH
remote: .
specs:
noun-project-api (3.0.0)
activesupport
oauth (~> 0.5)

GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
coderay (1.1.3)
concurrent-ruby (1.1.6)
diff-lcs (1.4.2)
Expand All @@ -15,6 +22,7 @@ GEM
i18n (1.8.3)
concurrent-ruby (~> 1.0)
method_source (1.0.0)
minitest (5.14.1)
oauth (0.5.4)
pry (0.13.1)
coderay (~> 1.1)
Expand All @@ -36,6 +44,10 @@ GEM
rspec_junit_formatter (0.4.1)
rspec-core (>= 2, < 4, != 2.12.0)
semver (1.0.1)
thread_safe (0.3.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
zeitwerk (2.3.0)

PLATFORMS
ruby
Expand Down
4 changes: 3 additions & 1 deletion lib/noun-project-api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "oauth"
require "active_support"
require "json"
require "noun-project-api/errors"
require "noun-project-api/connection"
Expand All @@ -24,10 +25,11 @@ def self.configure

# Main configuration class.
class Configuration
attr_accessor :public_domain
attr_accessor :public_domain, :cache

def initialize
@public_domain = false
@cache = ActiveSupport::Cache::NullStore.new
end
end
end
34 changes: 19 additions & 15 deletions lib/noun-project-api/icons_retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ class IconsRetriever < Retriever
# * offset - offset the results
# * page - page number
def find(term, limit = nil, offset = nil, page = nil)
raise ArgumentError.new("Missing search term") unless term
cache_key = Digest::MD5.hexdigest("#{term}+#{limit}+#{offset}+#{page}")

search = OAuth::Helper.escape(term)
search += "?limit_to_public_domain=#{NounProjectApi.configuration.public_domain ? 1 : 0}"
NounProjectApi.configuration.cache.fetch(cache_key) do
raise ArgumentError.new("Missing search term") unless term

args = {
"limit" => limit,
"offset" => offset,
"page" => page
}.reject { |_, v| v.nil? }
args.each { |k, v| search += "&#{k}=#{v}" } if args.size > 0
search = OAuth::Helper.escape(term)
search += "?limit_to_public_domain=#{NounProjectApi.configuration.public_domain ? 1 : 0}"

result = access_token.get("#{API_BASE}#{API_PATH}#{search}")
raise ServiceError.new(result.code, result.body) unless %w(200 404).include? result.code
args = {
"limit" => limit,
"offset" => offset,
"page" => page
}.reject { |_, v| v.nil? }
args.each { |k, v| search += "&#{k}=#{v}" } if args.size > 0

if result.code == "200"
JSON.parse(result.body)["icons"].map { |icon| Icon.new(icon) }
else
[]
result = access_token.get("#{API_BASE}#{API_PATH}#{search}")
raise ServiceError.new(result.code, result.body) unless %w(200 404).include? result.code

if result.code == "200"
JSON.parse(result.body)["icons"].map { |icon| Icon.new(icon) }
else
[]
end
end
end

Expand Down
1 change: 1 addition & 0 deletions noun-project-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.test_files = Dir['spec/**/*']

s.add_runtime_dependency 'oauth', '~> 0.5'
s.add_runtime_dependency 'activesupport'

s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rspec', '~> 3.9'
Expand Down

0 comments on commit 5a9144c

Please sign in to comment.