Giter Site home page Giter Site logo

imgur's Introduction

Imgur

This gem allows you to interact with Imgur's authenticated API, version 3.

It doesn't implement all the available endpoints. So far:

  • image upload, find and delete
  • account image count, account details, account image list

Imgur set-up

In order to upload images to Imgur you are going to need an Imgur application. Using an application is the preferred way, because you get twice the credits (100 per hour) than uploading as an anonymous user. Of course, if you already have an Imgur application, you can skip this section.

To create an application visit https://imgur.com/register/api_oauth and fill the form. Pay attention to select these options:

  • Application Type: Browser
  • Access Type: Read & Write

Check your inbox, you will receive an email with your application Client Id and Client Secret. We are going to need them later on.

Installation

Add this line to your application's Gemfile:

gem 'imgurapi'

And then run:

$ bundle install

Authorize your Ruby application

We now need to grant access to our Imgur user to Imgur the application, so we can upload images in his behalf. Type this and follow the instructions:

$ rake imgur:authorize CLIENT_ID='CLIENT_ID' CLIENT_SECRET='CLIENT_SECRET'
Visit this URL: http://api.imgur.com/oauth/authorize?oauth_token=xxx
And after you approved the authorization please enter your verification code: yyy

Authorization was successful. Use these credentials to initialize the library:

access_token: ACCESS_TOKEN
refresh_token: REFRESH_TOKEN

Usage and overview

Create a session object to communicate to Imgur.

imgur_session = Imgurapi::Session.new(client_id: 'CLIENT_ID', client_secret: 'CLIENT_SECRET', refresh_token: 'REFRESH_TOKEN')

Your account:

account = imgur_session.account.account
=> #<Imgurapi::Account:0x007fd399b6b678 @id=123, @url="my_account", @bio=nil, @reputation=7, @created=1352279501, @pro_expiration=false>

How many images you have:

puts imgur_session.account.image_count

Upload your first image. Argument can be either a String or a File:

image = imgur_session.image.image_upload('portrait.jpg')

image is now an instance of Imgurapi::Image, a convenient way to manage all the attributes of your image (at least more convenient than a multilevel dictionary):

name = nil
title = nil
caption = nil
hash = "xyzzy"
deletehash = "abcdef"
datetime = "2012-11-18 16:22:00"
type = "image/jpeg"
animated = "false"
width = 654
height = 273
size = 47584
views = 0
bandwidth = 0
link = "http://imgur.com/xyzzy"

Do you need to retrieve a previously uploaded image?

my_image = imgur_session.image.image('hash_code')

Granted, another Image object. It will return nil if it could not find an image matching your hash.

Feel free to use it in your Rails views:

<%= image_tag my_image.link %>

Alternatively, you can use url instead of link because it provides image resizing.

<%= image_tag my_image.url %>
<%= image_tag my_image.url(:small) %>

With no arguments, it's the same as link. Available sizes are small (:small_square, :small or :s) and large (:large_thumbnail, :large or :l)

How many images do you have at the moment?

puts imgur_session.account.image_count

We want to delete some images. Argument can be either a String or an Image:

imgur_session.image.image_delete(image)
imgur_session.image.image_delete('xyzzy')

It will return true if succeeded, false otherwise.

Available endpoints

Not all the endpoints available at https://api.imgur.com/ have been implemented. Feel free to suggest or pull request your needs.

The API methods have been named following Imgur's endpoints, for easy mental mapping. Although I consider this is clearer, in the future it may change to follow the naming conventions of the official Python API.

Type API method Imgur doc URL
Account account.account https://api.imgur.com/endpoints/account#account
Account account.images https://api.imgur.com/endpoints/account#images
Account account.image_count https://api.imgur.com/endpoints/account#image-count
Image image.image https://api.imgur.com/endpoints/image#image
Image image.image_upload https://api.imgur.com/endpoints/image#image-upload
Image image.image_delete https://api.imgur.com/endpoints/image#image-delete

imgur's People

Contributors

dncrht avatar themcny avatar

Watchers

James Cloos avatar Chris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.