Class: Memerator::Meme

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

Overview

A meme on memerator

Meme Information Methods collapse

Meme Modification Methods collapse

Instance Method Details

#authorUser

Returns the author of this meme.

Returns:

  • (User)

    the author of this meme.


42
43
44
# File 'lib/memerator/meme.rb', line 42

def author
  Memerator::User.new(@data['author'])
end

#average_ratingFloat

Returns the average rating.

Returns:

  • (Float)

    the average rating.


32
33
34
# File 'lib/memerator/meme.rb', line 32

def average_rating
  @data['rating']['average']
end

#captionString?

Returns the caption, if there is one.

Returns:

  • (String, nil)

    the caption, if there is one.


17
18
19
# File 'lib/memerator/meme.rb', line 17

def caption
  @data['caption']
end

#caption=(newcap) ⇒ true

Set the meme caption, only works if you own the meme!

Returns:

  • (true)

    when successful

Raises:


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/memerator/meme.rb', line 101

def caption=(newcap)
  raise Memerator::Errors::DisabledMeme, "This meme is disabled and can't be interacted with" if disabled?

  response = JSON.parse(RestClient.put("https://api.memerator.me/v1/meme/#{memeid}/caption", {"caption" => newcap}.to_json, Authorization: @token, 'Content-Type': :json))
  @data['caption'] = newcap
  response['success']
rescue RestClient::NotFound
  raise Memerator::Errors::NoPermission, "your token doesn't grant you access to this meme"
rescue RestClient::Unauthorized
  raise Memerator::Errors::InvalidToken, "Your token expired!"
end

#disable!true

Disables the meme, only works if you own the meme!

Returns:

  • (true)

    when successful

Raises:


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/memerator/meme.rb', line 67

def disable!
  response = JSON.parse(RestClient.put("https://api.memerator.me/v1/meme/#{memeid}/disable", {}, Authorization: @token))
  @data['disabled'] = true
  response['success']
rescue RestClient::NotFound
  raise Memerator::Errors::NoPermission, "your token doesn't grant you access to this meme"
rescue RestClient::BadRequest
  raise Memerator::Errors::NoChange, "This Meme is already disabled!"
rescue RestClient::Unauthorized
  raise Memerator::Errors::InvalidToken, "Your token expired!"
end

#disabled=(dis) ⇒ true, false

Shorthand for #disable! and #enable!

Parameters:

  • dis (true, false)

    should we disable or enable

Returns:

  • (true)

    if the meme was disabled

  • (false)

    if the meme was enabled

Raises:

  • (ArgumentError)

    if you didn't provide true or false

See Also:


120
121
122
123
124
125
126
127
128
129
# File 'lib/memerator/meme.rb', line 120

def disabled=(dis)
  case dis
  when true
    disable!
  when false
    enable!
  else
    raise ArgumentError, "please provide true or false"
  end
end

#disabled?true, false

Usually, the only people who can see disabled memes are Staff and the owners of the meme. This will most likely always be false.

Returns:

  • (true, false)

    the meme disabled status.


54
55
56
# File 'lib/memerator/meme.rb', line 54

def disabled?
  @data['disabled']
end

#enable!true

Enable the meme, only works if you own the meme!

Returns:

  • (true)

    when successful

Raises:


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/memerator/meme.rb', line 84

def enable!
  response = JSON.parse(RestClient.put("https://api.memerator.me/v1/meme/#{memeid}/enable", {}, Authorization: @token))
  @data['disabled'] = false
  response['success']
rescue RestClient::NotFound
  raise Memerator::Errors::NoPermission, "your token doesn't grant you access to this meme"
rescue RestClient::BadRequest
  raise Memerator::Errors::NoChange, "This Meme is already disabled!"
rescue RestClient::Unauthorized
  raise Memerator::Errors::InvalidToken, "Your token expired!"
end

#image_urlString

Returns the URL of the image.

Returns:

  • (String)

    the URL of the image.


22
23
24
# File 'lib/memerator/meme.rb', line 22

def image_url
  @data['url']
end

#meme_urlString

Returns the URL to the meme.

Returns:

  • (String)

    the URL to the meme


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

def meme_url
  @data['permalink']
end

#memeidString

Returns the meme's ID.

Returns:

  • (String)

    the meme's ID.


12
13
14
# File 'lib/memerator/meme.rb', line 12

def memeid
  @data['memeid']
end

#rate(rating) ⇒ Object

Rate a meme This was a long running joke, in fact, until recently, you couldn't even rate a meme from the API! You're welcome.

Parameters:

  • rating (Integer)

    the rating to give this meme


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/memerator/meme.rb', line 134

def rate(rating)
  raise Memerator::Errors::DisabledMeme, "This meme is disabled and can't be interacted with" if disabled?
  raise ArgumentError, "Your rating is bad mate!" if rating > 5 || rating < 1 || rating.to_i != rating

  response = JSON.parse(RestClient.post("https://api.memerator.me/v1/meme/#{memeid}/rate", {"rating" => rating}.to_json, Authorization: @token, 'Content-Type': :json))
  response['success']
rescue RestClient::NotFound
  raise Memerator::Errors::NoPermission, "your token doesn't grant you access to this meme"
rescue RestClient::Unauthorized
  raise Memerator::Errors::InvalidToken, "Your token expired!"
end

#timestampTime

Returns the time this meme was submitted.

Returns:

  • (Time)

    the time this meme was submitted.


37
38
39
# File 'lib/memerator/meme.rb', line 37

def timestamp
  Time.parse(@data['timestamp'])
end

#total_ratingsFloat

Returns the total amount of ratings.

Returns:

  • (Float)

    the total amount of ratings.


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

def total_ratings
  @data['rating']['total']
end