Class: Memerator

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

Overview

All Memerator functionality, whether extended or just here.

Defined Under Namespace

Modules: Errors Classes: Meme, Notification, Profile, Report, Stats, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil) ⇒ Memerator

Initialize a new Memerator object, via a token.

Parameters:

  • token (String) (defaults to: nil)

    API token, taken from the Memerator Website.


9
10
11
# File 'lib/memerator.rb', line 9

def initialize(token: nil)
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Get the token from instantiation


70
71
72
# File 'lib/memerator.rb', line 70

def token
  @token
end

Instance Method Details

#meme(id) ⇒ Meme

Get a meme by its id

Parameters:

  • id (String)

    the Meme ID

Returns:

  • (Meme)

    the meme

Raises:


36
37
38
39
40
41
42
# File 'lib/memerator.rb', line 36

def meme(id)
  data = JSON.parse(RestClient.get("https://memerator.me/api/v1/meme/#{id}", Authorization: @token))

  Meme.new(data, token: @token)
rescue RestClient::NotFound
  raise Memerator::Errors::InvalidMeme, "This meme doesn't exist!"
end

#notificationsArray<Notification>

Returns your notifications.

Returns:


58
59
60
61
# File 'lib/memerator.rb', line 58

def notifications
  notifications = JSON.parse(RestClient.get('https://memerator.me/api/v1/notifications', Authorization: @token))
  notifications.map { |notification_data| Notification.new(notification_data) }
end

#profileProfile

Returns your profile.

Returns:


27
28
29
30
# File 'lib/memerator.rb', line 27

def profile
  data = JSON.parse(RestClient.get('https://memerator.me/api/v1/profile/me', Authorization: @token))
  Profile.new(data, token: @token)
end

#randommemeMeme

Get a random meme

Returns:

  • (Meme)

    the meme


46
47
48
49
# File 'lib/memerator.rb', line 46

def randommeme
  data = JSON.parse(RestClient.get("https://memerator.me/api/v1/meme/random", Authorization: @token))
  Meme.new(data, token: @token)
end

#reportsArray<Report>

Returns your reports.

Returns:

  • (Array<Report>)

    your reports


64
65
66
67
# File 'lib/memerator.rb', line 64

def reports
  reports = JSON.parse(RestClient.get('https://memerator.me/api/v1/reports', Authorization: @token))
  reports.map { |report_data| Report.new(report_data) }
end

#statsStats

Returns the site's stats.

Returns:

  • (Stats)

    the site's stats


52
53
54
55
# File 'lib/memerator.rb', line 52

def stats
  data = JSON.parse(RestClient.get('https://memerator.me/api/v1/stats', Authorization: @token))
  Stats.new(data)
end

#user(perso) ⇒ User

Returns the user's profile.

Parameters:

  • perso (String, Integer)

    the ID of the user, or their username

Returns:

  • (User)

    the user's profile

Raises:


16
17
18
19
20
21
22
23
24
# File 'lib/memerator.rb', line 16

def user(perso)
  return profile if perso.downcase == 'me'

  data = JSON.parse(RestClient.get("https://memerator.me/api/v1/profile/#{perso}", Authorization: @token))

  User.new(data)
rescue RestClient::NotFound
  raise Memerator::Errors::InvalidUser, "This user doesn't exist!"
end