class Medium::Client

The Client class is used to interact with the resources the Medium API exposes.

Attributes

posts[R]

:users is the User resource sub-client :posts is the Post resource sub-client

users[R]

:users is the User resource sub-client :posts is the Post resource sub-client

Public Class Methods

new(auth_creds) click to toggle source

Creates a new instance of Medium::Client.

@param auth_creds [Hash] The credentials to use for authentication with

the Medium API. This can be a self provided :integration_token. OAuth
support to be added in the future.
# File lib/medium/client.rb, line 15
def initialize(auth_creds)
  @client = Hurley::Client.new 'https://api.medium.com/v1/'
  @client.connection = Hurley::HttpCache.new
  auth_headers_with auth_creds

  true
end

Private Class Methods

validate(response) click to toggle source

A class method to validate responses from the Medium API.

@param response [#status_type and body] The response object from the

remote server. The body is the actual response from the server. The
status_type exposes whether the request was successful or if it failed
with an error

@return [Hash] Returns the response body if the request is successful.

Otherwise, an error is thrown.
# File lib/medium/client.rb, line 60
def self.validate(response)
  response_body = JSON.parse response.body
  if response.success?
    response_body
  else
    fail "Failed with #{response.status_type} error from server. Received error: #{response_body['errors'][0]['message']}"
  end
end

Private Instance Methods

auth_headers_with(creds) click to toggle source

Add authorization headers to the Hurley client. This allows us to authenticate and communicate with the Medium API.

@param creds [Hash] The credentials / key to use when authenticating with

the Medium API.

@return [TrueClass] Returns true if the headers are set successfully.

Otherwise, an error is thrown.
# File lib/medium/client.rb, line 46
def auth_headers_with(creds)
  token = creds[:integration_token] || creds['integration_token']
  @client.header[:Authorization] = "Bearer #{token}"
  true
end