class Memerator::Meme

A meme on memerator

Public Class Methods

new(data, token: nil) click to toggle source

@!visibility private

# File lib/memerator/meme.rb, line 4
def initialize(data, token: nil)
  @data = data
  @token = token
end

Public Instance Methods

author() click to toggle source

@return [User] the author of this meme.

# File lib/memerator/meme.rb, line 42
def author
  Memerator::User.new(@data['author'])
end
average_rating() click to toggle source

@return [Float] the average rating.

# File lib/memerator/meme.rb, line 32
def average_rating
  @data['rating']['average']
end
caption() click to toggle source

@return [String, nil] the caption, if there is one.

# File lib/memerator/meme.rb, line 17
def caption
  @data['caption']
end
caption=(newcap) click to toggle source

Set the meme caption, only works if you own the meme! @raise [Memerator::Errors::NoPermission] if you don't have access to modify the meme @raise [Memerator::Errors::DisabledMeme] if the meme is disabled @raise [Memerator::Errors::InvalidToken] if your token is bad. @return [true] when successful

# 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!() click to toggle source

Disables the meme, only works if you own the meme! @raise [Memerator::Errors::NoPermission] if you don't have access to disable the meme @raise [Memerator::Errors::NoChange] if the meme is already disabled @raise [Memerator::Errors::InvalidToken] if your token is bad. @return [true] when successful

# 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) click to toggle source

Shorthand for disable! and enable! @see disable! @see enable! @param dis [true, false] should we disable or enable @raise [ArgumentError] if you didn't provide true or false @return [true] if the meme was disabled @return [false] if the meme was enabled

# 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?() click to toggle source

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

# File lib/memerator/meme.rb, line 54
def disabled?
  @data['disabled']
end
enable!() click to toggle source

Enable the meme, only works if you own the meme! @raise [Memerator::Errors::NoPermission] if you don't have access to enable the meme @raise [Memerator::Errors::NoChange] if the meme is already enabled @raise [Memerator::Errors::InvalidToken] if your token is bad. @return [true] when successful

# 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_url() click to toggle source

@return [String] the URL of the image.

# File lib/memerator/meme.rb, line 22
def image_url
  @data['url']
end
meme_url() click to toggle source

@return [String] the URL to the meme

# File lib/memerator/meme.rb, line 47
def meme_url
  @data['permalink']
end
memeid() click to toggle source

@return [String] the meme's ID.

# File lib/memerator/meme.rb, line 12
def memeid
  @data['memeid']
end
rate(rating) click to toggle source

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. @param rating [Integer] the rating to give this meme

# 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
timestamp() click to toggle source

@return [Time] the time this meme was submitted.

# File lib/memerator/meme.rb, line 37
def timestamp
  Time.parse(@data['timestamp'])
end
total_ratings() click to toggle source

@return [Float] the total amount of ratings.

# File lib/memerator/meme.rb, line 27
def total_ratings
  @data['rating']['total']
end