module Matroid
Constants
- VERSION
Attributes
Public Class Methods
Retrieves the authenticated user's account information @return The account info as a parsed JSON @example
{ "account" => { "credits" => { "concurrentTrainLimit" =>1, "held" => 3496, "plan" => "premium", "daily" => { "used" => 36842, "available" => 100000 }, "monthly" => { "used" => 36842, "available" => 1000000 } } } }
# File lib/matroid.rb, line 64 def account_info get('account') end
Authenticates access for Matroid
API @example
Matroid.authenticate("<your_client_id>", "<your_client_secret>")
@param client_id [String] @param client_secret [String] @return [Boolean] If the the access token is successfully created.
# File lib/matroid.rb, line 19 def authenticate(client_id = nil, client_secret = nil) return true unless @token.nil? || @token.expired? if client_id && client_secret err_msg = 'problem using environment variables "MATROID_CLIENT_ID" and "MATROID_CLIENT_SECRET"' new_token = get_token(client_id, client_secret) raise Error::AuthorizationError.new(err_msg) if new_token.nil? @client_id, @client_secret = client_id, client_secret elsif (@client_id.nil? || @client_secret.nil?) && !environment_variables? err_msg = '"MATROID_CLIENT_ID" and "MATROID_CLIENT_SECRET" not found in environment' raise Error::AuthorizationError.new(err_msg) else err_msg = 'Problem using client variables provided' raise Error::AuthorizationError.new(err_msg) if get_token(@client_id, @client_secret).nil? end true end
Retrieves video classification data. Requires a video_id from {Detector#classify_video_file} or {Detector#classify_video_url} format 'json'/'csv' @note A “video_id” is needed to get the classification results @example
<Detector >.get_video_results(video_id: "23498503uf0dd09", threshold: 30, format: 'json')
@param video_id [String] @param threshold [Numeric, nil] @param
# File lib/matroid.rb, line 77 def get_video_results(video_id, *args) get("videos/#{video_id}", *args) end
# File lib/matroid/connection.rb, line 30 def parse_response(response) if valid_json?(response.body) status = response.status_code parsed_response = JSON.parse(response.body) if status != 200 err_msg = JSON.pretty_generate(parsed_response) raise Error::RateLimitError.new(err_msg) if status == 429 raise Error::InvalidQueryError.new(err_msg) if status == 422 raise Error::PaymentError.new(err_msg) if status == 402 raise Error::ServerError.new(err_msg) if status / 100 == 5 raise Error::APIError.new(err_msg) end parsed_response else p response raise Error::APIError.new(response.body) end end
Registers a new stream on Matroid
@param stream_url [String] the url for the stream @param stream_name [String] the name of the stream @return Hash containing the id of the stream { “stream_id” => “123” }
# File lib/matroid.rb, line 85 def register_stream(stream_url, stream_name) register_err = "Must include a url and name for stream" raise Error::InvalidQueryError.new(register_err) unless stream_url && stream_name Matroid.post("feeds", name: stream_name, url: stream_url) end
Changes the default base api uri. This is used primarily for testing purposes. @param uri [String]
# File lib/matroid/connection.rb, line 20 def set_base_uri(uri) @base_api_uri = uri end
Calls ::show on the current token (if it exists).
# File lib/matroid.rb, line 38 def show_token if @token @token.show end end
Private Class Methods
# File lib/matroid/connection.rb, line 89 def auth_params(client_id, client_secret) { 'client_id' => client_id, 'client_secret' => client_secret, 'grant_type' => DEFAULT_GRANT_TYPE } end
# File lib/matroid/connection.rb, line 97 def environment_variables? !ENV['MATROID_CLIENT_ID'].nil? && !ENV['MATROID_CLIENT_SECRET'].nil? end
# File lib/matroid/connection.rb, line 52 def get_token(client_id, client_secret) @base_api_uri = BASE_API_URI if @base_api_uri.nil? url = @base_api_uri + TOKEN_RESOURCE params = auth_params(client_id, client_secret) @client = HTTPClient.new response = @client.post(url, body: params) return false unless response.status_code == 200 @token = Token.new(JSON.parse(response.body)) @client.base_url = @base_api_uri @client.default_header = { 'Authorization' => @token.authorization_header } end
# File lib/matroid/connection.rb, line 64 def send_request(verb, path, params = {}) path = URI.escape(path) # refreshes token with each call authenticate case verb when 'get' response = @client.get(path, body: params) when 'post' response = @client.post(path, body: params) end parse_response(response) end
# File lib/matroid/connection.rb, line 80 def valid_json?(json) begin JSON.parse(json) return true rescue JSON::ParserError => e return false end end