class AboutYou::SDK::Client

This Class manages the API-Calls

Author

Collins GmbH & Co KG

Constants

API_ENDPOINT_LIVE

url for live-api-requests

API_ENDPOINT_SANDBOX

url for sandbox-api-requests

API_ENDPOINT_STAGE

url for staging-api-requests

Attributes

api_endpoint[RW]

the url used for api calls

app_id[RW]

the app id of the app which should be represented by an instance of AY

app_password[RW]

the authentifaction token from dev-center for the app id

page_id[RW]

the page id of the current page visited by an user

Public Class Methods

new( app_id, app_password, _api_endpoint = 'stage', _logger = nil ) click to toggle source

the Constructor for the Client class

  • Args :

    • app_id -> The App-Id of the App

    • app_password -> The Auth-Token of the App

    • api_endpoint -> Can be either live or staging

    • logger -> Logger-Template

  • Returns :

# File lib/AboutYou/client.rb, line 40
def initialize(
  app_id,
  app_password,
  _api_endpoint = 'stage',
  _logger = nil
)
  self.app_id = app_id
  self.app_password = app_password
end

Public Instance Methods

api_endpoint=(api_endpoint) click to toggle source

Setter for the api endpoint

  • Args :

    • api_endpoint -> Can be either live or staging

# File lib/AboutYou/client.rb, line 68
def api_endpoint=(api_endpoint)
  case
  when api_endpoint == 'stage'
    self.api_endpoint = API_ENDPOINT_STAGE
  when api_endpoint == 'sandbox'
    self.api_endpoint = API_ENDPOINT_SANDBOX
  when api_endpoint == 'live'
    self.api_endpoint = API_ENDPOINT_LIVE
  else
    self.api_endpoint = api_endpoint
  end
end
app_credentials=(app_id, app_password) click to toggle source

Setter for app-credentials

  • Args :

    • app_id -> The App-Id of the App

    • app_password -> The Auth-Token of the App

# File lib/AboutYou/client.rb, line 57
def app_credentials=(app_id, app_password)
  self.app_id = app_id
  self.app_password = app_password
end
request(body) click to toggle source

Builds a JSON string representing the request-data Executes the API request builds a JSON string representing the response-data

  • Args :

    • body -> the body of the api-call, containing all request data

  • Fails :

    • if the http response code is not between 200 and 300 or 304

    • if the body of the http response is not an array

  • Returns :

    • Json-String containing the response-body

# File lib/AboutYou/client.rb, line 96
def request(body)
  if page_id
    request = {
      body: body,
      basic_auth: {
        username: app_id,
        password: app_password
      },
      headers: {
        'Content-Type' => 'application/json',
        'Accept-Encoding' => 'gzip,deflate',
        'X-Page-ID' => page_id
      }
    }
  else
    request = {
      body: body,
      basic_auth: {
        username: app_id,
        password: app_password
      },
      headers: {
        'Content-Type' => 'application/json',
        'Accept-Encoding' => 'gzip,deflate'
      }
    }
  end

  response = HTTParty.post('https://shop-api.aboutyou.de/api', request)
  code = response.code

  fail String(response.code) unless
  code >= 200 && code < 300 || code == 304
  fail 'result is not array' unless JSON.parse(response.body).is_a?(Array)

  JSON.parse(response.body)
end