Class: Junkie::Pyload::Api

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/junkie/pyload/api.rb

Overview

Class that abstracts the Pyload Api

Constant Summary collapse

FILE_STATUS =
{
  0  => :done,
  2  => :online,
  3  => :queued,
  4  => :skipped,
  5  => :waiting,
  8  => :failed,
  10 => :decrypting,
  12 => :downloading,
  13 => :extracting,
}
DEFAULT_CONFIG =
{
  protocol: 'http',
  host: 'localhost',
  port: 8000,
  api_user: '',
  api_password: ''
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Config

collect_default_configs, get_config, included

Constructor Details

#initializeApi

Returns a new instance of Api.



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

def initialize()
  @config = Config.get_config(self)

  api_user     = @config[:api_user]
  api_password = @config[:api_password]

  unless api_password && api_user &&
            api_password.match(/\w+/) && api_user.match(/\w+/)
    raise InvalidConfigError, "api_user or api_password are not configured"
  end

end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/junkie/pyload/api.rb', line 15

def config
  @config
end

Returns the value of attribute cookie.



15
16
17
# File 'lib/junkie/pyload/api.rb', line 15

def cookie
  @cookie
end

Instance Method Details

#build_url(path = "") ⇒ String

builds up a URL string

Parameters:

  • path (String) (defaults to: "")

    the method specific part of the url

Returns:

  • (String)

    complete URI String



105
106
107
108
# File 'lib/junkie/pyload/api.rb', line 105

def build_url(path="")
  "%s://%s:%d/api%s" %
      [ @config[:protocol], @config[:host], @config[:port], path ]
end

#call(method, params = {}) ⇒ Object

makes a GET request with the given method name and params

Parameters:

  • API-method (Symbol)

    e.g :getQueue, :getPackageData

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

    that are a passed as query parameters

Returns:

  • (Object)

    parsed JSON Response

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/junkie/pyload/api.rb', line 56

def call(method, params={})
  raise ArgumentError, "`method` is not a Symbol" unless method.is_a? Symbol

  query_parameter = {}
  params.each do |key, value|
    if value.is_a? Array
      query_parameter[key] =
        URI.encode_www_form_component(JSON.dump(value))
    else
      query_parameter[key] =
        URI.encode_www_form_component('"' + value.to_s + '"')
    end
  end

  request("/#{method.to_s}", :get, query_parameter)
end

#loginObject

Note:

sets cookie instance variable

Logs the api user in through sending the credentials to the Api

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/junkie/pyload/api.rb', line 78

def 
  resp = nil

  begin
    resp = request('/login', :post, {
          username: @config[:api_user],
          password: @config[:api_password]
    }, true)

  rescue Junkie::HTTP403Error => e
    raise Junkie::InvalidCredentialsError,
      "the supplied credentials are incorrect"
  end

  # extract Cookie-ID from Login response
  if resp.response_header["SET_COOKIE"]
    header = resp.response_header["SET_COOKIE"]
    if md = header.match(/^(\S+);/)
       @cookie = md[1]
    end
  end
end