class Agilix::Buzz::Api
Attributes
domain[RW]
password[RW]
token[RW]
token_expiration[RW]
username[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 19 def initialize(options = {}) @username = options.fetch(:username, default_username) @password = options.fetch(:password, default_password) @domain = options.fetch(:domain, default_domain) @token = options.dig(:token) @token_expiration = options.dig(:token_expiration) end
Public Instance Methods
authenticated_bulk_post(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 42 def authenticated_bulk_post(query = {}) check_authentication bulk_post query end
authenticated_get(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 27 def authenticated_get(query = {}) check_authentication get query end
authenticated_post(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 32 def authenticated_post(query = {}) check_authentication unless query.delete(:bypass_authentication_check) post query end
authenticated_query_post(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 37 def authenticated_query_post(query = {}) check_authentication unless query.delete(:bypass_authentication_check) query_post query end
bulk_post(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 89 def bulk_post(query = {}) cmd = query.delete(:cmd) url = agilix_url_base + "?cmd=#{cmd}&_token=#{token}" query_params = query.delete(:query_params) if query_params url += query_params.map {|k,v| "&#{k}=#{v}" }.join("") end response = self.class.post(url, body: modify_bulk_body(query), timeout: 60, headers: headers) end
check_authentication()
click to toggle source
# File lib/agilix/buzz/api.rb, line 99 def check_authentication if token && token_expiration if token_expiration < Time.now extend_session end else authenticate! end end
get(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 47 def get(query = {}) response = self.class.get(agilix_url_base, query: modify_query(query), timeout: 60, headers: headers) end
post(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 51 def post(query = {}) response = self.class.post(agilix_url_base, body: modify_body(query), timeout: 60, headers: headers) end
query_post(query = {})
click to toggle source
For when the api is super unconventional & you need to modify both query params & body params in a custom fashion, and upload a file even!
# File lib/agilix/buzz/api.rb, line 73 def query_post(query = {}) url = agilix_url_base query_params = query.delete(:query_params) if query_params url += "?&_token=#{token}" + query_params.map {|k,v| "&#{k}=#{v}" }.join("") end file = query.delete(:file) if file new_headers = headers new_headers["Content-Type"] = "multipart/form-data" response = self.class.post(url, multipart: true, body: {file: file}, timeout: 60, headers: new_headers) else response = self.class.post(url, body: query.to_json, timeout: 60, headers: headers) end end
Private Instance Methods
agilix_url_base()
click to toggle source
# File lib/agilix/buzz/api.rb, line 168 def agilix_url_base @agilix_url_base ||= "#{agilix_url_endpoint}/cmd" end
agilix_url_endpoint()
click to toggle source
# File lib/agilix/buzz/api.rb, line 164 def agilix_url_endpoint @agilix_url_endpoint ||= ENV.fetch("AGILIX_BUZZ_URL", "https://api.agilixbuzz.com") end
argument_cleaner(required_params: , optional_params: , options: )
click to toggle source
# File lib/agilix/buzz/api.rb, line 119 def argument_cleaner(required_params: , optional_params: , options: ) missing_required = required_params - options.map {|k,v| k.to_sym } raise ArgumentError.new("Missing Required Arguments: #{missing_required.join(', ')}") if missing_required.any? all_params = (required_params + optional_params).flatten return options.select {|k,v| all_params.include?(k.to_sym)} end
authenticate!()
click to toggle source
# File lib/agilix/buzz/api.rb, line 111 def authenticate! response = login username: @username, password: @password, domain: @domain raise AuthenticationError.new(response.dig("response", "message")) if response.dig("response", "code") == "InvalidCredentials" @token = response.dig("response", "user", "token") @token_expiration = Time.now + (response.dig("response", "user", "authenticationexpirationminutes").to_i * 60 ) if @token response end
default_domain()
click to toggle source
# File lib/agilix/buzz/api.rb, line 160 def default_domain ENV["AGILIX_BUZZ_DEFAULT_DOMAIN"] end
default_password()
click to toggle source
# File lib/agilix/buzz/api.rb, line 156 def default_password ENV["AGILIX_BUZZ_PASSWORD"] end
default_username()
click to toggle source
# File lib/agilix/buzz/api.rb, line 152 def default_username ENV["AGILIX_BUZZ_USERNAME"] end
headers()
click to toggle source
# File lib/agilix/buzz/api.rb, line 145 def headers @headers = { "Accept" => "application/json", "Content-Type" => "application/json", } end
modify_body(body = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 133 def modify_body(body = {}) default_params = { request: {}.merge(body) } default_params[:request]["_token"] = token if token default_params.to_json end
modify_bulk_body(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 139 def modify_bulk_body(query = {}) root_node = query.delete(:root_node) default_params = { requests: { root_node.to_sym => query[:body] } } default_params.to_json end
modify_query(query = {})
click to toggle source
# File lib/agilix/buzz/api.rb, line 126 def modify_query(query = {}) default_params = {} default_params.merge! query default_params["_token"] = token if token default_params end