Class: GdsApi::JsonClient

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionHandling

#build_specific_http_error, #error_class_for_code, #ignoring, #ignoring_missing

Constructor Details

#initialize(options = {}) ⇒ JsonClient

Returns a new instance of JsonClient.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gds_api/json_client.rb', line 37

def initialize(options = {})
  if options[:disable_timeout] or options[:timeout].to_i < 0
    raise "It is no longer possible to disable the timeout."
  end

  @logger = options[:logger] || NullLogger.instance

  if options[:disable_cache] || (options[:cache_size] == 0)
    @cache = NullCache.new
  else
    cache_size = options[:cache_size] || DEFAULT_CACHE_SIZE
    cache_ttl = options[:cache_ttl] || DEFAULT_CACHE_TTL
    @cache = JsonClient.cache(cache_size, cache_ttl)
  end
  @options = options
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



35
36
37
# File 'lib/gds_api/json_client.rb', line 35

def cache
  @cache
end

#loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/gds_api/json_client.rb', line 35

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/gds_api/json_client.rb', line 35

def options
  @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.



20
21
22
# File 'lib/gds_api/json_client.rb', line 20

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


31
32
33
# File 'lib/gds_api/json_client.rb', line 31

def self.cache=(c)
  @cache = c
end

.default_request_headersObject



54
55
56
57
58
59
60
61
# File 'lib/gds_api/json_client.rb', line 54

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



107
108
109
# File 'lib/gds_api/json_client.rb', line 107

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



91
92
93
# File 'lib/gds_api/json_client.rb', line 91

def get_json!(url, additional_headers = {}, &create_response)
  do_json_request(:get, url, nil, additional_headers, &create_response)
end

#get_raw(url) ⇒ Object



71
72
73
74
75
# File 'lib/gds_api/json_client.rb', line 71

def get_raw(url)
  ignoring_missing do
    get_raw!(url)
  end
end

#get_raw!(url) ⇒ Object



67
68
69
# File 'lib/gds_api/json_client.rb', line 67

def get_raw!(url)
  do_raw_request(:get, url)
end

#patch_json!(url, params, additional_headers = {}) ⇒ Object



103
104
105
# File 'lib/gds_api/json_client.rb', line 103

def patch_json!(url, params, additional_headers = {})
  do_json_request(:patch, url, params, additional_headers)
end

#post_json!(url, params = {}, additional_headers = {}) ⇒ Object



95
96
97
# File 'lib/gds_api/json_client.rb', line 95

def post_json!(url, params = {}, additional_headers = {})
  do_json_request(:post, url, params, additional_headers)
end

#post_multipart(url, params) ⇒ Object



111
112
113
114
115
116
# File 'lib/gds_api/json_client.rb', line 111

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



99
100
101
# File 'lib/gds_api/json_client.rb', line 99

def put_json!(url, params, additional_headers = {})
  do_json_request(:put, url, params, additional_headers)
end

#put_multipart(url, params) ⇒ Object



118
119
120
121
122
123
# File 'lib/gds_api/json_client.rb', line 118

def put_multipart(url, params)
  r = do_raw_request(:put, url, params.merge({
    :multipart => true
  }))
  Response.new(r)
end