Sha256: 38370241763a3ebc98bd3eefb5f742f83f080c5b6fb2afb6da8f43e564547f23

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module GroupMe
  class Client
    API_BASE_URL = 'https://api.groupme.com/v3/'
    API_DEFAULT_HEADER = { 'Content-Type': 'application/json' }.freeze

    attr_accessor :access_token

    def initialize(args = {})
      @access_token = args[:access_token] || GroupMe.configuration.access_token
      @client = HTTPClient.new(base_url: API_BASE_URL, default_header: API_DEFAULT_HEADER)
    end

    def get(path, query = {})
      request(:get, path, query: query)
    end

    def post(path, body = {})
      request(:post, path, body: body)
    end

    def delete(path, query = {})
      request(:delete, path, query: query)
    end

    def request(method, path, query: {}, body: nil)
      response = @client.request(method, path, { token: @access_token }.merge(query), body&.to_json)
      [parse_response_body(response), response.status]
    end

    private

    def parse_response_body(response)
      return response.reason unless response.ok?
      return if blank?(response.body)

      JSON.parse(response.body, symbolize_names: true).fetch(:response)
    end

    def blank?(string)
      string =~ /\A[[:space:]]*\z/
    end
  end

  def self.client=(client)
    @client = client
  end

  def self.client
    @client ||= Client.new
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
groupme-api-0.9.8 lib/groupme/client.rb
groupme-api-0.9.7 lib/groupme/client.rb
groupme-api-0.9.6 lib/groupme/client.rb
groupme-api-0.9.5 lib/groupme/client.rb
groupme-api-0.9.4 lib/groupme/client.rb
groupme-api-0.9.3 lib/groupme/client.rb
groupme-api-0.9.1 lib/groupme/client.rb
groupme-api-0.9.0 lib/groupme/client.rb