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



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

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

#get_json(url) ⇒ Object



43
44
45
46
47
# File 'lib/gds_api/json_client.rb', line 43

def get_json(url)
  ignoring GdsApi::HTTPNotFound do
    get_json! url
  end
end

#get_json!(url) ⇒ Object



49
50
51
# File 'lib/gds_api/json_client.rb', line 49

def get_json!(url)
  @cache[url] ||= do_json_request(Net::HTTP::Get, url)
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



53
54
55
56
57
# File 'lib/gds_api/json_client.rb', line 53

def post_json(url, params)
  ignoring GdsApi::HTTPNotFound do
    post_json! url, params
  end
end

#post_json!(url, params) ⇒ Object



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

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

#put_json(url, params) ⇒ Object



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

def put_json(url, params)
  ignoring GdsApi::HTTPNotFound do
    put_json! url, params
  end
end

#put_json!(url, params) ⇒ Object



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

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