Skip to content

Instantly share code, notes, and snippets.

@octplane
Forked from joost/pixabay_client.rb
Created January 7, 2016 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octplane/c05859ed765e2f37507c to your computer and use it in GitHub Desktop.
Save octplane/c05859ed765e2f37507c to your computer and use it in GitHub Desktop.
Supersimple http://pixabay.com/api/docs/ Ruby API client.
require 'hashie'
require 'faraday'
require 'faraday_middleware'
# Supersimple http://pixabay.com/api/docs/ client.
# Usage:
# result = PixabayClient.new(key: 'your_key').get(search_term: 'beach')
# result.body # Mashified results
# See: https://gist.github.com/joost/e32daf904ed8bd171974
class PixabayClient
def initialize(options = {})
@key = options[:key]
end
API_URL = "http://pixabay.com/api/"
def connection
@connection ||= Faraday.new(url: API_URL, params: {key: @key}) do |conn|
conn.adapter Faraday.default_adapter
conn.response :mashify
conn.response :json
end
end
def get(params = {})
connection.get('', params)
rescue Faraday::ParsingError
nil
end
end # PixabayClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment