Sha256: 24a1335d0cf9d6dae79c9c6b37bf02df29afd3a93cab553a4c3ed068affa39d9

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require "httparty"
require "json"

module Twtxt
  module Yarn
    def api_endpoint(yarnpod)
      "https://#{yarnpod}/api/v1"
    end

    def get_jwt_token(username, password, endpoint)
      response = HTTParty.post("#{endpoint}/auth", body: { username: username, password: password }.to_json, headers: { "Content-Type" => "application/json" })
      JSON.parse(response.body)["token"]
    end

    def authenticated_headers(token)
      { "Token" => "#{token}", "Content-Type" => "application/json" }
    end

    class Credentials
      attr_reader :username, :password, :yarnpod

      def initialize(username, password, yarnpod)
        @username = username
        @password = password
        @yarnpod = yarnpod
      end
    end

    class Session
      include HTTParty
      include Yarn
      attr_reader :credentials, :endpoint, :token, :headers

      def initialize(credentials)
        @credentials = credentials
        @endpoint = api_endpoint(credentials.yarnpod)
        @token = get_jwt_token(credentials.username, credentials.password, self.endpoint)
        @headers = authenticated_headers(self.token)
      end

      def ping
        self.class.get("#{@endpoint}/ping", headers: @headers)
      end

      def whoami
        self.class.get("#{@endpoint}/whoami", headers: @headers)
      end

      #   def get_profile_twts(username, page)
      #     self.class.get("#{@endpoint}/profile/#{username}/twts?page=#{page}", headers: @headers)
      #   end

      def get_profile(username)
        self.class.get("#{@endpoint}/profile/#{username}", headers: @headers)
      end

      def get_twtxt(profile)
        self.class.get("https://#{credentials.yarnpod}/user/#{profile}/twtxt.txt", headers: @headers)
      end

      def post_twt(twt_text)
        self.class.post("#{@endpoint}/post", body: { text: twt_text, post_as: "#{@credentials.username}" }.to_json, headers: @headers)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twtxt-0.0.1 lib/twtxt/yarn.rb