Class: GdsApi::JsonClient
- Inherits:
-
Object
- Object
- GdsApi::JsonClient
- Includes:
- ExceptionHandling
- Defined in:
- lib/gds_api/json_client.rb
Constant Summary collapse
- DEFAULT_TIMEOUT_IN_SECONDS =
4
- DEFAULT_CACHE_SIZE =
100
- DEFAULT_CACHE_TTL =
15 minutes
15 * 60
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.cache(size = DEFAULT_CACHE_SIZE, ttl = DEFAULT_CACHE_TTL) ⇒ Object
Cache TTL will be overridden for a given request/response by the Expires header if it is included in the response.
-
.cache=(c) ⇒ Object
Set the caching implementation.
- .default_request_headers ⇒ Object
Instance Method Summary collapse
- #delete_json!(url, params = nil, additional_headers = {}) ⇒ Object
- #get_json!(url, additional_headers = {}, &create_response) ⇒ Object
- #get_raw(url) ⇒ Object
- #get_raw!(url) ⇒ Object
-
#initialize(options = {}) ⇒ JsonClient
constructor
A new instance of JsonClient.
- #post_json!(url, params = {}, additional_headers = {}) ⇒ Object
- #post_multipart(url, params) ⇒ Object
- #put_json!(url, params, additional_headers = {}) ⇒ Object
- #put_multipart(url, params) ⇒ Object
Methods included from ExceptionHandling
#build_specific_http_error, #ignoring, #ignoring_missing
Constructor Details
#initialize(options = {}) ⇒ JsonClient
Returns a new instance of JsonClient.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gds_api/json_client.rb', line 36 def initialize( = {}) if [:disable_timeout] or [:timeout].to_i < 0 raise "It is no longer possible to disable the timeout." end @logger = [:logger] || GdsApi::Base.logger if [:disable_cache] || ([:cache_size] == 0) @cache = NullCache.new else cache_size = [:cache_size] || DEFAULT_CACHE_SIZE cache_ttl = [:cache_ttl] || DEFAULT_CACHE_TTL @cache = JsonClient.cache(cache_size, cache_ttl) end @options = end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
34 35 36 |
# File 'lib/gds_api/json_client.rb', line 34 def cache @cache end |
#logger ⇒ Object
Returns the value of attribute logger.
34 35 36 |
# File 'lib/gds_api/json_client.rb', line 34 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
34 35 36 |
# File 'lib/gds_api/json_client.rb', line 34 def @options end |
Class Method Details
.cache(size = DEFAULT_CACHE_SIZE, ttl = DEFAULT_CACHE_TTL) ⇒ Object
Cache TTL will be overridden for a given request/response by the Expires header if it is included in the response.
LRUCache doesn’t respect a cache size of 0, and instead effectively creates a cache with a size of 1.
19 20 21 |
# File 'lib/gds_api/json_client.rb', line 19 def self.cache(size=DEFAULT_CACHE_SIZE, ttl=DEFAULT_CACHE_TTL) @cache ||= LRUCache.new(max_size: size, ttl: ttl) end |
.cache=(c) ⇒ Object
Set the caching implementation. Default is LRUCache. Can be Anything which responds to:
[](key)
[]=(key, value)
store(key, value, expiry_time=nil) - or a Ruby Time object
30 31 32 |
# File 'lib/gds_api/json_client.rb', line 30 def self.cache=(c) @cache = c end |
.default_request_headers ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/gds_api/json_client.rb', line 53 def self.default_request_headers { 'Accept' => 'application/json', 'Content-Type' => 'application/json', # GOVUK_APP_NAME is set for all apps by Puppet 'User-Agent' => "gds-api-adapters/#{GdsApi::VERSION} (#{ENV["GOVUK_APP_NAME"]})" } end |
Instance Method Details
#delete_json!(url, params = nil, additional_headers = {}) ⇒ Object
102 103 104 |
# File 'lib/gds_api/json_client.rb', line 102 def delete_json!(url, params = nil, additional_headers = {}) do_json_request(:delete, url, params, additional_headers) end |
#get_json!(url, additional_headers = {}, &create_response) ⇒ Object
90 91 92 |
# File 'lib/gds_api/json_client.rb', line 90 def get_json!(url, additional_headers = {}, &create_response) do_json_request(:get, url, nil, additional_headers, &create_response) end |
#get_raw(url) ⇒ Object
70 71 72 73 74 |
# File 'lib/gds_api/json_client.rb', line 70 def get_raw(url) ignoring_missing do get_raw!(url) end end |
#get_raw!(url) ⇒ Object
66 67 68 |
# File 'lib/gds_api/json_client.rb', line 66 def get_raw!(url) do_raw_request(:get, url) end |
#post_json!(url, params = {}, additional_headers = {}) ⇒ Object
94 95 96 |
# File 'lib/gds_api/json_client.rb', line 94 def post_json!(url, params = {}, additional_headers = {}) do_json_request(:post, url, params, additional_headers) end |
#post_multipart(url, params) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/gds_api/json_client.rb', line 106 def post_multipart(url, params) r = do_raw_request(:post, url, params.merge({ :multipart => true })) Response.new(r) end |
#put_json!(url, params, additional_headers = {}) ⇒ Object
98 99 100 |
# File 'lib/gds_api/json_client.rb', line 98 def put_json!(url, params, additional_headers = {}) do_json_request(:put, url, params, additional_headers) end |