Class: GdsApi::JsonClient
- Inherits:
-
Object
- Object
- GdsApi::JsonClient
- 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
-
#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
Instance Method Summary collapse
- #delete_json!(url, params = nil) ⇒ Object
- #get_json(url) ⇒ Object
- #get_json!(url) ⇒ Object
- #get_raw(url) ⇒ Object
-
#initialize(options = {}) ⇒ JsonClient
constructor
A new instance of JsonClient.
- #post_json(url, params) ⇒ Object
- #post_json!(url, params) ⇒ Object
- #put_json(url, params) ⇒ Object
- #put_json!(url, params) ⇒ Object
Methods included from ExceptionHandling
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( = {}) @logger = [:logger] || GdsApi::Base.logger cache_size = [:cache_size] || DEFAULT_CACHE_SIZE cache_ttl = [:cache_ttl] || DEFAULT_CACHE_TTL @cache = JsonClient.cache(cache_size, cache_ttl) @options = end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
20 21 22 |
# File 'lib/gds_api/json_client.rb', line 20 def cache @cache end |
#logger ⇒ Object
Returns the value of attribute logger.
20 21 22 |
# File 'lib/gds_api/json_client.rb', line 20 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/gds_api/json_client.rb', line 20 def @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 |