Skip to content

Commit

Permalink
simplify adapter call method
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHENG-JING-JZ committed Nov 23, 2018
1 parent 96f8e99 commit 8e6de83
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
26 changes: 8 additions & 18 deletions lib/quick_travel/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ def self.check_id!(id)
end

def self.find_all!(request_path, opts = {})
response = if opts.key? :cache
QuickTravel::Cache.cache(opts[:cache], opts[:cache_options]) {
get_and_validate(request_path, opts.except(:cache, :cache_options), return_response_object: true)
}
else
get_and_validate(request_path, opts, return_response_object: true)
end
response = QuickTravel::Cache.cache(opts[:cache], opts[:cache_options]) {
get_and_validate(request_path, opts.except(:cache, :cache_options), return_response_object: true)
}

deserializer = Deserializer.new(response[:parsed_response])
objects = Array.wrap(deserializer.extract_under_root(self))
Expand Down Expand Up @@ -200,17 +196,11 @@ def self.delete_and_validate(path, query = {}, opts = {})
end

def self.call_and_validate(http_method, path, query = {}, opts = {})
response = if opts.key? :cache
QuickTravel::Cache.cache(opts[:cache], opts[:cache_options]) {
Api.call_and_validate(http_method, path, query, opts.except(:cache, :cache_options))
}
else
if opts[:return_response_object]
Api.call_and_validate(http_method, path, query, opts)
else
Api.call_and_validate(http_method, path, query, opts)[:parsed_response]
end
end
response = QuickTravel::Cache.cache(opts[:cache], opts[:cache_options]) {
response_object = Api.call_and_validate(http_method, path, query, opts.except(:cache, :cache_options))
response_object = response_object[:parsed_response] if !opts[:cache] and !opts[:return_response_object]
response_object
}
end

def self.base_uri(uri = nil)
Expand Down
1 change: 1 addition & 0 deletions lib/quick_travel/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def cache(key, cache_options = {}, &block)
end

def self.cache(key, cache_options = {})
return yield unless key.present?
cached_value = cache_store.read(key)
return cached_value unless cached_value.nil?
return nil unless block_given?
Expand Down

0 comments on commit 8e6de83

Please sign in to comment.