Class: Pexels::Client

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

Defined Under Namespace

Classes: Photos, Videos

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = ENV['PEXELS_API_KEY']) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_key = ENV['PEXELS_API_KEY'])
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



4
5
6
# File 'lib/pexels/client.rb', line 4

def api_key
  @api_key
end

#ratelimit_remainingObject (readonly)

Returns the value of attribute ratelimit_remaining.



4
5
6
# File 'lib/pexels/client.rb', line 4

def ratelimit_remaining
  @ratelimit_remaining
end

Instance Method Details

#photosObject



12
13
14
# File 'lib/pexels/client.rb', line 12

def photos
  @photos ||= Pexels::Client::Photos.new(self)
end

#request(path, method: 'GET', params: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pexels/client.rb', line 20

def request(path, method: 'GET', params: {})
  results = Requests.request(
    method,
    "#{Pexels.api_base_url}#{path}",
    params: params,
    headers: {
      'Authorization' => api_key
    }
  )

  @ratelimit_remaining = results.headers['x-ratelimit-remaining'].first.to_i

  return JSON.parse(results.body)
rescue StandardError => exception
  raise Pexels::APIError.new(exception)
end

#videosObject



16
17
18
# File 'lib/pexels/client.rb', line 16

def videos
  @videos ||= Pexels::Client::Videos.new(self)
end