Sha256: 95d99a37d7e105e6e8934570e1aaeaf44770a36956ec4834f78494ac9bf69103

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require 'rest_client'
require 'json'

module Lolcommits
  module Flowdock
    class RequestFailed < StandardError; end

    class Client
      attr_accessor :access_token, :organization, :flow

      API_HOST = 'api.flowdock.com'

      def initialize(access_token, organization: nil, flow: nil)
        @access_token = access_token
        @organization = organization
        @flow         = flow
      end

      # GET /organizations
      def organizations
        get(organizations_url)
      end

      # GET /flows
      def flows
        get(flows_url)
      end

      # POST /flows/:organization/:flow/messages
      def create_message(organization: nil, flow: nil, params: {})
        post(messages_url(organization, flow), params)
      end

      private

      def post(*args)
        request(*args, :post)
      end

      def get(*args)
        request(*args)
      end

      def request(url, params = {}, method = :get)
        response = RestClient.send(method, url, params)
        if response.code.to_s =~ /^2/
          JSON.parse(response)
        else
          raise RestClient::RequestFailed.new(response)
        end
      rescue RestClient::RequestFailed, JSON::ParserError => e
        raise Flowdock::RequestFailed.new(e.message)
      end

      def base_url
        "https://#{access_token}@#{API_HOST}"
      end

      def organizations_url
        "#{base_url}/organizations"
      end

      def flows_url
        "#{base_url}/flows"
      end

      def messages_url(organization, flow)
        "#{base_url}/flows/#{organization}/#{flow}/messages"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lolcommits-flowdock-0.5.0 lib/lolcommits/flowdock/client.rb
lolcommits-flowdock-0.4.0 lib/lolcommits/flowdock/client.rb
lolcommits-flowdock-0.3.3 lib/lolcommits/flowdock/client.rb
lolcommits-flowdock-0.3.2 lib/lolcommits/flowdock/client.rb
lolcommits-flowdock-0.3.1 lib/lolcommits/flowdock/client.rb
lolcommits-flowdock-0.2.0 lib/lolcommits/flowdock/client.rb