Sha256: aa4c91952eb7bbbdfe5f944d2d1e59431753cc578b4cad1d9250be712dc40ef3

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module MediumSdk
  class Client
    attr_accessor :connection

    attr_reader :me

    def initialize(opts = {})
      if opts.key? :integration_token
        @connection = MediumSdk::Connection::IntegrationToken.new opts
      elsif opts.key? :client_id
        @connection = MediumSdk::Connection::AuthCode.new opts
      end
    end

    def get_url(url)
      return @connection.http.get do |req|
        req.url url
      end
    end

    def body_key(res, key)
      return res.body.key?(key) ? res.body[key] : nil
    end

    def me()
      @me = body_key get_url('me'), 'data'
      return @me
    end

    def user_publications(user_id)
      res = get_url File.join 'users', user_id, 'publications'
      return body_key(res, 'data')
    end

    def post(post)
      url = ''
      if post.key? :publicationId
        publication_id = post[:publicationId].clone
        post.delete :publicationId
        url = File.join 'publications', publication_id, 'posts'
      else
        me unless @me
        unless @me.is_a?(Hash) && @me.key?('id') && @me['id'].to_s.length>0
          raise 'Authorized User Id is unknown'
        end
        id = @me['id']
        url = File.join 'users', id, 'posts'
      end
      res = @connection.http.post do |req|
        req.url url
        req.body = post
      end
      return body_key(res, 'data')
    end

    def publication_contributors(publication_id)
      res = get_url File.join 'publications', publication_id, 'contributors'
      return body_key(res, 'data')
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
medium_sdk-0.0.1 lib/medium_sdk/client.rb