Class: Weasyl::API

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/weasyl/api.rb

Overview

Helper class to abstract actual API communication

Author:

  • Maxine Michalski

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyString

Sets the API key. OAuth isn’t supported yet.

Returns:

  • (String)

    API key for weasyl API

Since:

  • 0.1.0



30
31
32
# File 'lib/weasyl/api.rb', line 30

def key
  @key
end

Instance Method Details

#fetch(endpoint, params = {}) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/weasyl/api.rb', line 31

def fetch(endpoint, params = {})
  params = if params.empty?
             ''
           else
             "?#{params.to_a.map { |a| a.join('=') }.join('&')}"
           end
  raise ArgumentError, 'API key can\'t be empty' if @key.nil?
  JSON.parse(URI.parse("https://weasyl.com/api/#{endpoint}#{params}")
    .read('X-Weasyl-API-Key' => @key, 'User-Agent' => Weasyl::USER_AGENT),
             symbolize_names: true)
end

#versionString

Display version of weasyl API we connected to

Returns:

  • (String)

    String that shows current weasyl API version

Author:

  • Maxine Michalski

Since:

  • 0.1.0



55
56
57
# File 'lib/weasyl/api.rb', line 55

def version
  fetch(:version)[:short_sha]
end

#whoamiHash

A test method to test API connection

Returns:

  • (Hash)

    A Hash object with user information

Author:

  • Maxine Michalski

Since:

  • 0.1.0



47
48
49
# File 'lib/weasyl/api.rb', line 47

def whoami
  fetch(:whoami)
end