class ReamazeAPI::Client

Constants

API_URL

Public: URL for Reamaze API.

Returns a String.

HTTP_METHODS

Public: HTTP methods used by the API

Returns an Array.

Attributes

http[R]

Public: HTTP adapter used for API requests.

Returns a Faraday::Connection.

Public Class Methods

new(brand:, login:, token:) { |builder| ... } click to toggle source

Public: Initialize a new Client instance.

brand - Brand name (subdomain from your Reamaze URL) login - Reamaze login token - Reamaze API token

Yields a Faraday::Connection if a block is given.

Returns nothing.

# File lib/reamaze_api/client.rb, line 47
def initialize(brand:, login:, token:)
  @url = URI.parse(API_URL % { brand: brand })

  @http = Faraday.new(url: @url, ssl: { verify: true }) do |builder|
    builder.request    :json
    builder.response   :json
    builder.use        RaiseErrorMiddleware
    builder.adapter    Faraday.default_adapter
    builder.basic_auth login, token

    yield builder if block_given?
  end
end

Public Instance Methods

articles() click to toggle source

Public: Article resource.

Returns an Article instance.

# File lib/reamaze_api/client.rb, line 64
def articles
  @articles ||= Article.new(self)
end
channels() click to toggle source

Public: Channel resource.

Returns a Channel instance.

# File lib/reamaze_api/client.rb, line 71
def channels
  @channels ||= Channel.new(self)
end
commit(method:, path:, params: {}) click to toggle source

Public: Submits an HTTP request to the upstream API.

method - HTTP method (eg: :get, :post) path - API path (without `/api/v1` prefix, eg: “/messages”) params - Hash of parameters to send with the request (default: {})

Raises a ReamazeAPI::Error for any HTTP response code 400-599 unless `ReamazeAPI.config.exceptions` is false.

Returns a Hash.

# File lib/reamaze_api/client.rb, line 107
def commit(method:, path:, params: {})
  path = "#{@url.path}#{path}"

  response = @http.run_request(method, path, params, {})

  Utils.symbolize_hash(success: response.success?, payload: response.body)
rescue ReamazeAPI::Error => e
  raise if ReamazeAPI.config.exceptions

  Utils.error_hash(e)
end
contacts() click to toggle source

Public: Contact resource.

Returns a Contact instance.

# File lib/reamaze_api/client.rb, line 79
def contacts
  @contacts ||= Contact.new(self)
end
conversations() click to toggle source

Public: Conversation resource.

Returns a Conversation instance.

# File lib/reamaze_api/client.rb, line 86
def conversations
  @conversations ||= Conversation.new(self)
end
messages() click to toggle source

Public: Message resource.

Returns a Message instance.

# File lib/reamaze_api/client.rb, line 93
def messages
  @messages ||= Message.new(self)
end
paginate(path, resource, params = {}) click to toggle source

Public: Performs a GET request on the given path/resource. If results are more than one page, each additional page is fetched and added to the payload. If any page returns an error response, that response is immediately returned and no further requests are performed.

NOTE: Beware of API rate limiting when using this method with large datasets.

path - API path (without `/api/v1` prefix, eg: “/messages”) resource - ReamazeAPI resource name (eg: `:messages`) params - Hash of parameters to send with the request (default: {})

Returns a Hash.

# File lib/reamaze_api/client.rb, line 132
def paginate(path, resource, params = {})
  params        = Utils.symbolize_hash(params)
  auto_paginate = params.delete(:auto_paginate)
  output        = get(path, params)
  page          = params.fetch(:page, 1)
  success       = output[:success]
  payload       = output[:payload]
  page_count    = payload[:page_count]

  if success && auto_paginate && page_count && page_count > page
    more = paginate(path, resource, params.merge(
      page:          page.next,
      auto_paginate: true
    ))

    if more[:success] && more[:payload]
      payload[resource].concat more[:payload][resource]
    else
      output[:success] = false
      output[:payload] = more[:payload]
    end
  end

  output
end