Medium SDK for Ruby

Gem Version Build Status Coverage Status Dependency Status Code Climate Scrutinizer Code Quality Downloads Docs License

Description

A Ruby SDK for the Medium.com API including:

  1. Auth via OAuth 2.0 with token refresh and demo app
  2. Auth via integration token
  3. 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.authorize_uri(
  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.authorize_code(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

Project Repo

Medium API Docs

Contributing

  1. Fork it ( http://github.com/grokify/medium-sdk-ruby/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. 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