Sha256: 119645b0e937e16e4880aa8ecd1846639f46cbf1a960b6d547c5767a60eca866

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require "httparty"
require "json"

# Submodules (hold the api method calls)
require_relative "client/links"

module BranchIO

  class Client
    class ErrorMissingBranchKey < StandardError; end

    # Submodules
    include Links

    # Include HTTParty for requests
    include HTTParty
    base_uri "https://api.branch.io"

    # Basic Api client
    attr_reader :branch_key, :branch_secret
    def initialize(branch_key=ENV["BRANCH_KEY"], branch_secret=ENV["BRANCH_SECRET"])
      @branch_key = branch_key
      @branch_secret = branch_secret

      unless branch_key
        raise ErrorMissingBranchKey.new("No Branch Key found: please provided one such key to BranchIO::Client#initialize or by setting the BRANCH_KEY environment variable")
      end
    end

    def get(url)
      self.class.get(url, headers: default_headers)
    end

    def post(url, data={})
      body = data.to_json
      self.class.post(url, body: body, headers: default_headers)
    end

    def put(url, data={})
      body = data.to_json
      self.class.put(url, body: body, headers: default_headers)
    end

    private

    def ensure_branch_secret_defined!
      unless branch_secret
        raise ErrorMissingBranchKey.new("No Branch Secret found: please provided one such key to BranchIO::Client#initialize or by setting the BRANCH_SECRET environment variable")
      end
    end

    def default_headers
      {
        "Content-Type" => "application/json",
        "Accept" => "application/json"
      }
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
branch_io-0.1.0 lib/branch_io/client.rb