Sha256: 697ef82549620ce308795d752a9505682ec95dda4afc462b97354eacf0faac8d

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "net/http"
require "json"
require "action_view"
require_relative "../utils"

module DevOrbit
  module Interactions
    class Follower
      def initialize(id:, name:, username:, url:, workspace_id:, api_key:)
        @id = id
        @name = name
        @username = username
        @url = url
        @workspace_id = workspace_id
        @api_key = api_key

        after_initialize!
      end

      def after_initialize!
        url = URI("https://app.orbit.love/api/v1/#{@workspace_id}/members")

        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true
        req = Net::HTTP::Post.new(url)
        req["Accept"] = "application/json"
        req["Content-Type"] = "application/json"
        req["Authorization"] = "Bearer #{@api_key}"

        req.body = construct_body

        req.body = req.body.to_json

        response = http.request(req)

        JSON.parse(response.body) if DevOrbit::Utils.valid_json?(response.body)
      end

      def construct_body
        {
          member: {
            name: @name.include?("_") ? @name.split("_").map(&:capitalize).join(" ") : @name,
            devto: @username,
            url: "https://dev.to#{@url}"
          }
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dev_orbit-0.1.2 lib/dev_orbit/interactions/follower.rb
dev_orbit-0.1.1 lib/dev_orbit/interactions/follower.rb
dev_orbit-0.1.0 lib/dev_orbit/interactions/follower.rb
dev_orbit-0.0.10 lib/dev_orbit/interactions/follower.rb
dev_orbit-0.0.9 lib/dev_orbit/interactions/follower.rb