Medium SDK for Ruby
Description
A Ruby SDK for the Medium.com API including:
- Auth via OAuth 2.0 with token refresh and demo app
- Auth via integration token
- Get and Post APIs
Installation
Via Bundler
Add medium_sdk to Gemfile and then run bundle:
$ echo "gem 'medium_sdk'" >> Gemfile
$ bundle
Via RubyGems
$ gem install medium_sdk
Usage
Authorization
Authorization Code Grant
The OAuth 2.0 authorization code grant is designed for where authorization needs to be granted by a 3rd party resource owner.
Using the default authorization URL:
require 'medium_sdk'
# Initialize SDK with OAuth redirect URI
client = MediumSdk.new(
client_id: 'my_client_id',
client_secret: 'my_client_secret',
redirect_uri: 'https://example.com/callback/medium'
)
# Retrieve OAuth authorize url using default redirect URL
auth_url = client.connection.(
scope: 'basicProfile,publishPost',
state: 'myState'
)
On your redirect page, you can exchange your authorization code for an access token using the following:
code = params['code'] # e.g. using Sinatra to retrieve code param in Redirect URI
client.connection.(code)
Integration Token
require 'medium_sdk'
client = MediumSdk.new integration_token: token
API Requests
# Getting the authenticated user’s details
data = client.me
# Listing the user’s publications
data = client.user_publications 'user_id'
# Fetching contributors for a publication
data = client.publication_contributors 'publication_id'
# Creating a user post
data = client.post, {
title: "Hard things in software development",
contentFormat: "html",
content: "<p>Cache invalidation</p><p>Naming things</p>",
tags: ["development", "design"],
publishStatus: "draft"
}
# Creating a publication post
data = client.post, {
title: "Hard things in software development",
contentFormat: "html",
content: "<p>Cache invalidation</p><p>Naming things</p>",
tags: ["development", "design"],
publishStatus: "draft",
publicationId: "deadbeef"
}
Demos
Demos are in the scripts directory and use .env files for configuration.
Integration Token Demo
$ cd scripts
$ cp example.env .env
$ vi .env
$ ruby me_token.rb
OAuth 2.0 Demo
Execute the following and then go to the URL in your browser after launching the Sinatra app.
$ cd scripts/sinatra
$ bundle
$ cp example.env .env
$ vi .env
$ ruby app.rb
Change Log
See CHANGELOG.md
Links
Project Repo
Medium API Docs
Contributing
- Fork it ( http://github.com/grokify/medium-sdk-ruby/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
License
Medium SDK for Ruby is available under an MIT-style license. See LICENSE.txt for details.
Medium SDK for Ruby © 2016 by John Wang
