Module: Matroid

Defined in:
lib/matroid.rb,
lib/matroid/error.rb,
lib/matroid/version.rb,
lib/matroid/detector.rb,
lib/matroid/connection.rb

Defined Under Namespace

Modules: Error Classes: Detector, Token

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.base_api_uriObject (readonly)

Returns the value of attribute base_api_uri


16
17
18
# File 'lib/matroid/connection.rb', line 16

def base_api_uri
  @base_api_uri
end

.clientObject (readonly)

Returns the value of attribute client


16
17
18
# File 'lib/matroid/connection.rb', line 16

def client
  @client
end

.tokenObject (readonly)

Returns the value of attribute token


16
17
18
# File 'lib/matroid/connection.rb', line 16

def token
  @token
end

Class Method Details

.account_infoObject

Retrieves the authenticated user's account information

Examples:

{
  "account" => {
    "credits" => {
      "concurrentTrainLimit" =>1,
      "held" => 3496,
      "plan" => "premium",
      "daily" => {
        "used" => 36842,
        "available" => 100000
      },
      "monthly" => {
        "used" => 36842,
        "available" => 1000000
      }
    }
  }
}

64
65
66
# File 'lib/matroid.rb', line 64

def 
  get('account')
end

.authenticate(client_id = nil, client_secret = nil) ⇒ Boolean

Authenticates access for Matroid API

Examples:

Matroid.authenticate("<your_client_id>", "<your_client_secret>")

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

.get_video_results(video_id, *args) ⇒ Object

Note:

A “video_id” is needed to get the classification results

Retrieves video classification data. Requires a video_id from Matroid::Detector#classify_video_file or Matroid::Detector#classify_video_url format 'json'/'csv'

Examples:

<Detector >.get_video_results(video_id: "23498503uf0dd09", threshold: 30, format: 'json')

77
78
79
# File 'lib/matroid.rb', line 77

def get_video_results(video_id, *args)
  get("videos/#{video_id}", *args)
end

.parse_response(response) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

.set_base_uri(uri) ⇒ Object

Changes the default base api uri. This is used primarily for testing purposes.


20
21
22
# File 'lib/matroid/connection.rb', line 20

def set_base_uri(uri)
  @base_api_uri = uri
end

.show_tokenObject

Calls ::show on the current token (if it exists).


38
39
40
41
42
# File 'lib/matroid.rb', line 38

def show_token
  if @token
    @token.show
  end
end