Class: CurseClient::API

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

Defined Under Namespace

Classes: UnauthorizedError

Constant Summary collapse

DEFAULT_URL =
"https://curse-rest-proxy.azurewebsites.net/api"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = DEFAULT_URL) ⇒ API

Returns a new instance of API.



10
11
12
# File 'lib/curse_client/api.rb', line 10

def initialize(url = DEFAULT_URL)
  @url = url
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/curse_client/api.rb', line 8

def token
  @token
end

Instance Method Details

#addon(id) ⇒ Object



34
35
36
# File 'lib/curse_client/api.rb', line 34

def addon(id)
  get_json("addon/#{id}")
end

#addon_file(addon_id, file_id) ⇒ Object



42
43
44
# File 'lib/curse_client/api.rb', line 42

def addon_file(addon_id, file_id)
  get_json("addon/#{addon_id}/file/#{file_id}")
end

#addon_files(id) ⇒ Object



38
39
40
# File 'lib/curse_client/api.rb', line 38

def addon_files(id)
  get_json("addon/#{id}/files")
end

#authenticate(username, password) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/curse_client/api.rb', line 18

def authenticate(username, password)
  response = HTTP.post("#{url}/authenticate",
                        { username: username, password: password },
                        headers: { "Content-Type" => "application/json" },
                        format: :json)
  case response
  when Net::HTTPUnauthorized
    raise UnauthorizedError, "Invalid username or password"
  when Net::HTTPSuccess
    data = JSON.parse(response.body, symbolize_names: true)
    @token = "#{data[:session][:user_id]}:#{data[:session][:token]}"
  else
    response.error!
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/curse_client/api.rb', line 14

def authenticated?
  !token.nil?
end