class GiphyRB::Giphy

Constants

API_VERSION
DESCRIPTION
ENDPOINT
VERSION

Public Class Methods

new(api_key:true) click to toggle source

Initialize the client

# File lib/giphyrb.rb, line 19
def initialize(api_key:true)
  @api_key = api_key
end

Public Instance Methods

children_pack_from_id(id) click to toggle source

A Sticker pack is a recursive data structure and so packs may contain other packs. For example, the 'Reactions' pack would have an 'OMG' child pack. This endpoint lists all children packs of a given Sticker pack. @param id [String] Filters results by specified Sticker Pack ID @return [Response]

# File lib/giphyrb.rb, line 164
def children_pack_from_id(id)
  params = {}
  result = request'stickers/packs/' + id.to_s + '/children', params
  Responses::ChildPack.new(result)
end
from_id(id) click to toggle source

Returns a GIF given that GIF's unique ID. @param id [String] Filters results by specified GIF ID @return [Response]

# File lib/giphyrb.rb, line 70
def from_id(id)
  params = {}
  result = request'gifs/' + id.to_s, params
  Response.new(result)
end
from_ids(ids=[]) click to toggle source

A multiget version of the get GIF by ID endpoint @param ids [Array<String>] Filters results by specified GIF IDs @return [Response]

# File lib/giphyrb.rb, line 79
def from_ids(ids=[])
  ids = Array(ids)
  ids = ids.join ','
  params = {:ids => ids}
  result = request'gifs/', params
  Response.new(result)
end
individual_sticker_pack_from_id(id, limit=5, offset=0) click to toggle source

Returns the metadata for any Sticker pack. @param id [String] Filters results by specified Sticker Pack ID @param limit [Int] The maximum number of records to return (default=5) @param offset [Int] An optional results offset (default=0) @return [Responses::StickerPack]

# File lib/giphyrb.rb, line 144
def individual_sticker_pack_from_id(id, limit=5, offset=0)
  params = {:limit => limit.to_i, :offset => offset.to_i}
  result = request'stickers/packs/' + id.to_s, params
  Responses::StickerPack.new(result)
end
list_sticker_pack() click to toggle source

Returns the metadata for any Sticker pack. @return [Responses::ChildPack]

# File lib/giphyrb.rb, line 133
def list_sticker_pack()
  params = {}
  result = request'stickers/packs/', params
  Responses::ChildPack.new(result)
end
random(tag=nil, rating='g') click to toggle source

Returns a random GIF, limited by tag. Excluding the tag parameter will return a random GIF from the GIPHY catalog. @param tag [String] Filters results by specified tag @param rating [String] Filters results by specified rating (default=g) @return [Responses::Random]

# File lib/giphyrb.rb, line 61
def random(tag=nil, rating='g')
  params = {:tag => tag, :rating => rating}
  result = request'gifs/random', params
  Responses::Random.new(result, tag)
end
sticker_pack_from_id(id, limit=5, offset=0) click to toggle source

Returns the stickers within an individual sticker pack. @param id [String] Filters results by specified Sticker Pack ID @param limit [Int] The maximum number of records to return (default=5) @param offset [Int] An optional results offset (default=0) @return [Response]

# File lib/giphyrb.rb, line 155
def sticker_pack_from_id(id, limit=5, offset=0)
  params = {:limit => limit.to_i, :offset => offset.to_i}
  result = request'stickers/packs/' + id.to_s + '/stickers', params
  Response.new(result)
end
sticker_random(tag=nil, rating='g') click to toggle source

Returns a random Sticker, limited by tag. Excluding the tag parameter will return a random Sticker from the GIPHY catalog. @param tag [String] Filters results by specified tag @param rating [String] Filters results by specified rating (default=g) @return [Responses::Random]

# File lib/giphyrb.rb, line 125
def sticker_random(tag=nil, rating='g')
  params = {:tag => tag, :rating => rating}
  result = request'stickers/random', params
  Responses::Random.new(result, tag)
end
sticker_translate(string) click to toggle source

The translate API draws on search, but uses the GIPHY special sauce to handle translating from one vocabulary to another. In this case, words and phrases to GIFs. @param string [String] Search term @return [Responses::Translate]

# File lib/giphyrb.rb, line 115
def sticker_translate(string)
  params = {:s => string}
  result = request'stickers/translate', params
  Responses::Translate.new(result)
end
translate(string) click to toggle source

The translate API draws on search, but uses the GIPHY special sauce to handle translating from one vocabulary to another. In this case, words and phrases to GIFs. @param string [String] Search term @return [Responses::Translate]

# File lib/giphyrb.rb, line 51
def translate(string)
  params = {:s => string}
  result = request'gifs/translate', params
  Responses::Translate.new(result)
end

Private Instance Methods

request(url, params) click to toggle source
# File lib/giphyrb.rb, line 172
def request(url, params)
  params[:api_key] = @api_key
  params[:fmt] = 'json'
  uri = URI "#{ENDPOINT}/#{API_VERSION}/#{url}"
  uri.query = URI.encode_www_form(params)
  resp = Net::HTTP.get_response(uri)
  result = nil
  if resp.is_a?(Net::HTTPSuccess) || resp.is_a?(Net::HTTPNotFound) || resp.is_a?(Net::HTTPBadRequest) || resp.is_a?(Net::HTTPForbidden) || resp.is_a?(Net::HTTPTooManyRequests)
    result = JSON.parse(resp.body)
  end
  result
end