Class: Jahuty::Client
- Inherits:
-
Object
- Object
- Jahuty::Client
- Defined in:
- lib/jahuty/client.rb
Overview
Executes requests against Jahuty’s API and returns resources.
Instance Method Summary collapse
-
#initialize(api_key:, cache: nil, expires_in: nil, prefer_latest: false) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(name, *args, &block) ⇒ Object
Allows services to be accessed as properties (e.g., jahuty.snippets).
- #request(action) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(api_key:, cache: nil, expires_in: nil, prefer_latest: false) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 |
# File 'lib/jahuty/client.rb', line 8 def initialize(api_key:, cache: nil, expires_in: nil, prefer_latest: false) @api_key = api_key @cache = Cache::Facade.new(cache || ::MiniCache::Store.new) @expires_in = expires_in @services = {} @prefer_latest = prefer_latest end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Allows services to be accessed as properties (e.g., jahuty.snippets).
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jahuty/client.rb', line 17 def method_missing(name, *args, &block) if args.empty? && name == :snippets unless @services.key?(name) @services[name] = Service::Snippet.new( client: self, cache: @cache, expires_in: @expires_in, prefer_latest: @prefer_latest ) end @services[name] else super end end |
Instance Method Details
#request(action) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jahuty/client.rb', line 30 def request(action) @requests ||= Request::Factory.new request = @requests.call(action) @client ||= Api::Client.new(api_key: @api_key) response = @client.send(request) @responses ||= Response::Handler.new result = @responses.call(action, response) raise Exception::Error.new(result), 'API problem' if result.is_a?(Resource::Problem) result end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
48 49 50 |
# File 'lib/jahuty/client.rb', line 48 def respond_to_missing?(name, include_private = false) name == :snippets || super end |