class SkullIsland::APIClientBase
The API Client Base class
Attributes
base_uri[R]
password[RW]
server[R]
username[RW]
Public Instance Methods
api_uri()
click to toggle source
# File lib/skull_island/api_client_base.rb, line 12 def api_uri @api_uri ||= URI.parse(server) @api_uri.path = base_uri if base_uri @api_uri end
authenticated?()
click to toggle source
# File lib/skull_island/api_client_base.rb, line 18 def authenticated? raise Exceptions::APIClientNotConfigured unless configured? @username && @password ? true : false end
configured?()
click to toggle source
# File lib/skull_island/api_client_base.rb, line 24 def configured? @configured ? true : false end
delete(uri)
click to toggle source
# File lib/skull_island/api_client_base.rb, line 83 def delete(uri) client_action do |client| client[uri].delete(json_headers) end end
get(uri, data = nil)
click to toggle source
# File lib/skull_island/api_client_base.rb, line 32 def get(uri, data = nil) client_action do |client| results = nil params = {} params.merge(data) if data # OPTIMIZE: Move to an Enumerable loop do follow_up = JSON.parse client[uri].get(json_headers.merge(params: params)) if results results['data'] += follow_up['data'] else results = follow_up.dup results.delete('offset') results.delete('next') end params = params.merge('offset' => follow_up['offset']) if follow_up.key?('offset') raise StopIteration unless follow_up.key?('offset') end results end end
json_headers()
click to toggle source
# File lib/skull_island/api_client_base.rb, line 28 def json_headers { content_type: :json, accept: :json } end
patch(uri, data)
click to toggle source
# File lib/skull_island/api_client_base.rb, line 66 def patch(uri, data) client_action do |client| response = client[uri].patch(json_escape(data.to_json), json_headers) if response && !response.empty? JSON.parse(response) else true end end end
post(uri, data = nil)
click to toggle source
# File lib/skull_island/api_client_base.rb, line 56 def post(uri, data = nil) client_action do |client| if data JSON.parse client[uri].post(json_escape(data.to_json), json_headers) else JSON.parse client[uri].post(nil, json_headers) end end end
put(uri, data)
click to toggle source
# File lib/skull_island/api_client_base.rb, line 77 def put(uri, data) client_action do |client| client[uri].put(json_escape(data.to_json), json_headers) end end
Private Instance Methods
client_action() { |connection| ... }
click to toggle source
# File lib/skull_island/api_client_base.rb, line 91 def client_action raise Exceptions::APIClientNotConfigured unless configured? yield connection end
connection()
click to toggle source
Don't bother creating a connection until we need one
# File lib/skull_island/api_client_base.rb, line 98 def connection @connection ||= if authenticated? RestClient::Resource.new(api_uri.to_s, @username, @password) else RestClient::Resource.new(api_uri.to_s) end end