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
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 |
# File 'lib/gds_api/json_client.rb', line 22 def initialize( = {}) @logger = [:logger] || GdsApi::Base.logger cache_size = [:cache_size] || DEFAULT_CACHE_SIZE @cache = JsonClient.cache(cache_size) @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) ⇒ Object
12 13 14 |
# File 'lib/gds_api/json_client.rb', line 12 def self.cache(size=DEFAULT_CACHE_SIZE) @cache ||= LRUCache.new(max_size: size) 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(Net::HTTP::Delete, url, params) end |
#get_json(url) ⇒ Object
41 42 43 44 45 |
# File 'lib/gds_api/json_client.rb', line 41 def get_json(url) ignoring GdsApi::HTTPNotFound do get_json! url end end |
#get_json!(url) ⇒ Object
47 48 49 |
# File 'lib/gds_api/json_client.rb', line 47 def get_json!(url) @cache[url] ||= do_json_request(Net::HTTP::Get, url) end |
#get_raw(url) ⇒ Object
37 38 39 |
# File 'lib/gds_api/json_client.rb', line 37 def get_raw(url) do_raw_request(Net::HTTP::Get, url) end |
#post_json(url, params) ⇒ Object
51 52 53 54 55 |
# File 'lib/gds_api/json_client.rb', line 51 def post_json(url, params) ignoring GdsApi::HTTPNotFound do post_json! url, params end end |
#post_json!(url, params) ⇒ Object
57 58 59 |
# File 'lib/gds_api/json_client.rb', line 57 def post_json!(url, params) do_json_request(Net::HTTP::Post, url, params) end |
#put_json(url, params) ⇒ Object
61 62 63 64 65 |
# File 'lib/gds_api/json_client.rb', line 61 def put_json(url, params) ignoring GdsApi::HTTPNotFound do put_json! url, params end 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(Net::HTTP::Put, url, params) end |