Sha256: 1ab4ba5b4fac2e3bd8e84a335c618905c7523b8b0b38ba9198c290ba654e77c2

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

module Reach5
  # Handles all calls to Reach5's API.
  class API
    # Get a new access token.
    #
    # Example:
    #     Reach5::API.new.get_access_token
    #     # => {
    #     #      "auth" => {
    #     #        "accessToken" => "f00b4r",
    #     #        "expires" => 43200
    #     #      },
    #     #      "status" => "success",
    #     #    }
    def get_access_token
      params = {
        client_key: Reach5.configuration.client_key,
        client_secret: Reach5.configuration.client_secret,
      }
      request = HTTP.get("#{host}/api/v1/access_token", params: params)
      request.parse
    end

    # Collects and stores profile data from an Auth provider.
    #
    # Example:
    #     Reach5::API.new.post_login(provider: 'facebook',
    #                                provider_token: 'f00b4r')
    #     # => {
    #     #      "profile" => {
    #     #        …
    #     #      },
    #     #      "status" => "success",
    #     #    }
    def post_login(provider:,
                   provider_token:,
                   provider_secret: nil,
                   user_agent: nil)
      params = {
        provider: provider,
        provider_token: provider_token,
        provider_secret: provider_secret,
        user_agent: user_agent,
      }
      HTTP.post("#{host}/api/v1/login", params: params).parse
    end

    # List profiles
    #
    # Example:
    #     Reach5::API.new.get_profiles
    #
    #     # => {
    #     #      "total" => 42,
    #     #      "items" => […],
    #     #      "status" => "success",
    #     #    }
    def get_profiles(page: 1, count: 10)
      params = {
        access_token: access_token,
        page: page,
        count: count,
      }
      HTTP.get("#{host}/api/v1/profile", params: params).parse
    end

    private

    def host
      "https://#{Reach5.configuration.customer_domain}"
    end

    def access_token
      @access_token ||= get_access_token.fetch("auth").fetch("accessToken")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reach5-0.1.0 lib/reach5/api.rb