Class: RARBG::API

Inherits:
Object
  • Object
show all
Defined in:
lib/rarbg/api.rb

Overview

Base class for RARBG API.

Constant Summary collapse

API_ENDPOINT =

RARBG API endpoint.

'https://torrentapi.org/pubapi_v2.php'
APP_ID =

App name identifier.

'rarbg-rubygem'
TOKEN_EXPIRATION =

Default token expiration time.

800

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPI

Initialize a new istance of RARBG::API.

Examples:

rarbg = RARBG::API.new


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rarbg/api.rb', line 38

def initialize
  @conn = Faraday.new(url: API_ENDPOINT) do |conn|
    conn.request  :json
    conn.response :json
    conn.adapter  Faraday.default_adapter

    conn.options.timeout = 90
    conn.options.open_timeout = 10

    conn.params[:app_id] = APP_ID
  end
end

Instance Attribute Details

#connFaraday::Connection (readonly)

Returns the Faraday connection object.

Returns:

  • (Faraday::Connection)

    the Faraday connection object.



23
24
25
# File 'lib/rarbg/api.rb', line 23

def conn
  @conn
end

#last_requestInteger (readonly)

Returns the monotonic timestamp of the last request performed.

Returns:

  • (Integer)

    the monotonic timestamp of the last request performed.



32
33
34
# File 'lib/rarbg/api.rb', line 32

def last_request
  @last_request
end

#tokenString (readonly)

Returns the token used for authentication.

Returns:

  • (String)

    the token used for authentication.



26
27
28
# File 'lib/rarbg/api.rb', line 26

def token
  @token
end

#token_timeInteger (readonly)

Returns the monotonic timestamp of the token request.

Returns:

  • (Integer)

    the monotonic timestamp of the token request.



29
30
31
# File 'lib/rarbg/api.rb', line 29

def token_time
  @token_time
end

Instance Method Details

#list(params = {}) ⇒ Array<Hash>

List torrents.

Examples:

List last 100 ranked torrents in Movies/x264/1080

rarbg = RARBG::API.new
rarbg.list(limit: 100, ranked: true, category: [44])

List all torrent with minimum 50 seeders

rarbg = RARBG::API.new
rarbg.list(min_seeders: 50)

Parameters:

  • params (Hash) (defaults to: {})

    A customizable set of parameters.

Options Hash (params):

  • :category (Array<Integer>)

    Filter results by category.

  • :format (Symbol)

    Format results. Valid values: :json, :json_extended. Default: :json.

  • :limit (Integer)

    Limit results number. Valid values: 25, 50, 100. Default: 25.

  • :min_seeders (Integer)

    Filter results by minimum seeders.

  • :min_leechers (Integer)

    Filter results by minimum leechers.

  • :ranked (Boolean)

    Include/exclude unranked results.

  • :sort (Symbol)

    Sort results. Valid values: :last, :seeders, :leechers. Default: :last.

Returns:

  • (Array<Hash>)

    Return torrents that match the specified parameters.

Raises:

  • (ArgumentError)

    Exception raised if params is not an Hash.

  • (RARBG::APIError)

    Exception raised when request fails or endpoint responds with an error.



80
81
82
83
84
85
86
87
88
# File 'lib/rarbg/api.rb', line 80

def list(params = {})
  raise ArgumentError, 'Expected params hash' unless params.is_a?(Hash)

  params.update(
    mode:   'list',
    token:  token?
  )
  call(params)
end

#search(params = {}) ⇒ Array<Hash>

Search torrents.

Examples:

Search by IMDb ID, sorted by leechers and in extended format.

rarbg = RARBG::API.new
rarbg.search(imdb: 'tt012831', sort: :leechers, format: :json_extended)

Search unranked torrents by string, with at least 2 seeders.

rarbg = RARBG::API.new
rarbg.search(string: 'Star Wars', ranked: false, min_seeders: 2)

Parameters:

  • params (Hash) (defaults to: {})

    A customizable set of parameters.

Options Hash (params):

  • :string (String)

    Search results by string.

  • :imdb (String)

    Search results by IMDb id.

  • :tvdb (String)

    Search results by TVDB id.

  • :themoviedb (String)

    Search results by The Movie DB id.

  • :category (Array<Integer>)

    Filter results by category.

  • :format (Symbol)

    Format results. Valid values: :json, :json_extended. Default: :json

  • :limit (Integer)

    Limit results number. Valid values: 25, 50, 100. Default: 25.

  • :min_seeders (Integer)

    Filter results by minimum seeders.

  • :min_leechers (Integer)

    Filter results by minimum leechers.

  • :ranked (Boolean)

    Include/exclude unranked results.

  • :sort (Symbol)

    Sort results. Valid values: :last, :seeders, :leechers. Default: :last.

Returns:

  • (Array<Hash>)

    Return torrents that match the specified parameters.

Raises:

  • (ArgumentError)

    Exception raised if params is not an Hash.

  • (ArgumentError)

    Exception raised if no search type param is passed (among string, imdb, tvdb, themoviedb).

  • (RARBG::APIError)

    Exception raised when request fails or endpoint responds with an error.



126
127
128
129
130
131
132
133
134
# File 'lib/rarbg/api.rb', line 126

def search(params = {})
  raise ArgumentError, 'Expected params hash' unless params.is_a?(Hash)

  params.update(
    mode:   'search',
    token:  token?
  )
  call(params)
end