Class: Junkie::Pyload::Api
- Inherits:
-
Object
- Object
- Junkie::Pyload::Api
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
Instance Method Summary collapse
-
#build_url(path = "") ⇒ String
builds up a URL string.
-
#call(method, params = {}) ⇒ Object
makes a GET request with the given method name and params.
-
#initialize ⇒ Api
constructor
A new instance of Api.
-
#login ⇒ Object
Logs the api user in through sending the credentials to the Api.
Methods included from Config
collect_default_configs, get_config, included
Constructor Details
#initialize ⇒ Api
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/junkie/pyload/api.rb', line 15 def config @config end |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
15 16 17 |
# File 'lib/junkie/pyload/api.rb', line 15 def @cookie end |
Instance Method Details
#build_url(path = "") ⇒ String
builds up a URL 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
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 |
#login ⇒ Object
Note:
sets cookie instance variable
Logs the api user in through sending the credentials to the Api
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 login 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 |