Class: GdsApi::JsonClient

Inherits:
Object
  • Object
show all
Includes:
ExceptionHandling
Defined in:
lib/gds_api/json_client.rb

Constant Summary collapse

DEFAULT_REQUEST_HEADERS =
{
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'User-Agent' => "GDS Api Client v. #{GdsApi::VERSION}"
}
DEFAULT_TIMEOUT_IN_SECONDS =
4
DEFAULT_CACHE_SIZE =
10
DEFAULT_CACHE_TTL =

15 minutes

15 * 60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionHandling

#ignoring

Constructor Details

#initialize(options = {}) ⇒ JsonClient

Returns a new instance of JsonClient.



22
23
24
25
26
27
28
# File 'lib/gds_api/json_client.rb', line 22

def initialize(options = {})
  @logger = options[:logger] || GdsApi::Base.logger
  cache_size = options[:cache_size] || DEFAULT_CACHE_SIZE
  cache_ttl = options[:cache_ttl] || DEFAULT_CACHE_TTL
  @cache = JsonClient.cache(cache_size, cache_ttl)
  @options = options
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.cache(size = DEFAULT_CACHE_SIZE, ttl = DEFAULT_CACHE_TTL) ⇒ Object



12
13
14
# File 'lib/gds_api/json_client.rb', line 12

def self.cache(size=DEFAULT_CACHE_SIZE, ttl=DEFAULT_CACHE_TTL)
  @cache ||= LRUCache.new(max_size: size, ttl: ttl)
end

.cache=(c) ⇒ Object



16
17
18
# File 'lib/gds_api/json_client.rb', line 16

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

Instance Method Details

#delete_json!(url, params = nil) ⇒ Object



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

def delete_json!(url, params = nil)
  do_request(:delete, url, params)
end

#get_json!(url, &create_response) ⇒ Object



59
60
61
# File 'lib/gds_api/json_client.rb', line 59

def get_json!(url, &create_response)
  @cache[url] ||= do_json_request(:get, url, nil, &create_response)
end

#get_raw(url) ⇒ Object



39
40
41
42
43
# File 'lib/gds_api/json_client.rb', line 39

def get_raw(url)
  ignoring GdsApi::HTTPNotFound do
    do_raw_request(:get, url)
  end
end

#post_json!(url, params) ⇒ Object



63
64
65
# File 'lib/gds_api/json_client.rb', line 63

def post_json!(url, params)
  do_json_request(:post, url, params)
end

#put_json!(url, params) ⇒ Object



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

def put_json!(url, params)
  do_json_request(:put, url, params)
end