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



69
70
71
# File 'lib/gds_api/json_client.rb', line 69

def delete_json!(url, params = nil)
  do_request(Net::HTTP::Delete, url, params)
end

#get_json!(url, &create_response) ⇒ Object



57
58
59
# File 'lib/gds_api/json_client.rb', line 57

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

#get_raw(url) ⇒ Object



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

def get_raw(url)
  do_raw_request(Net::HTTP::Get, url)
end

#post_json!(url, params) ⇒ Object



61
62
63
# File 'lib/gds_api/json_client.rb', line 61

def post_json!(url, params)
  do_json_request(Net::HTTP::Post, url, params)
end

#put_json!(url, params) ⇒ Object



65
66
67
# File 'lib/gds_api/json_client.rb', line 65

def put_json!(url, params)
  do_json_request(Net::HTTP::Put, url, params)
end