Class: Gifster::Client

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

Constant Summary collapse

BASE_URL =
"http://api.giphy.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/gifster/client.rb', line 10

def initialize(api_key)
  @connection = Faraday.new(BASE_URL)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/gifster/client.rb', line 8

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/gifster/client.rb', line 8

def connection
  @connection
end

Instance Method Details

#search(query, limit, offset) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gifster/client.rb', line 15

def search(query, limit, offset)
  response = connection.get do |req|
    req.url "/v1/gifs/search"
    req.headers["Accepts"] = "application/json"
    req.params['api_key'] = api_key
    req.params['q'] = query
    req.params['limit'] = limit
    req.params['offset'] = offset
  end
  JSON.parse(response.body)["data"]
end


27
28
29
30
31
32
33
34
35
# File 'lib/gifster/client.rb', line 27

def trending(limit, offset)
  response = connection.get do |req|
    req.url "/v1/gifs/trending"
    req.headers["Accepts"] = "application/json"
    req.params['api_key'] = api_key
    req.params['offset'] = offset
  end
  JSON.parse(response.body)["data"]
end