Class: AboutYou::SDK::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/client.rb

Overview

This Class manages the API-Calls

Author

Collins GmbH & Co KG

Constant Summary collapse

API_ENDPOINT_STAGE =

url for staging-api-requests

'http://shop-api.staging.aboutyou.de/api'
API_ENDPOINT_SANDBOX =

url for sandbox-api-requests

'http://shop-api.sandbox.aboutyou.de/api'
API_ENDPOINT_LIVE =

url for live-api-requests

'https://shop-api.aboutyou.de/api'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_password, _api_endpoint = 'stage', _logger = nil) ⇒ Client

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 :

    • Instance of AboutYou::SDK::Client



40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#api_endpointObject

the url used for api calls



24
25
26
# File 'lib/AboutYou/client.rb', line 24

def api_endpoint
  @api_endpoint
end

#app_idObject

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



20
21
22
# File 'lib/AboutYou/client.rb', line 20

def app_id
  @app_id
end

#app_passwordObject

the authentifaction token from dev-center for the app id



22
23
24
# File 'lib/AboutYou/client.rb', line 22

def app_password
  @app_password
end

#page_idObject

the page id of the current page visited by an user



26
27
28
# File 'lib/AboutYou/client.rb', line 26

def page_id
  @page_id
end

Instance Method Details

#app_credentials=(app_id, app_password) ⇒ Object

Setter for app-credentials

  • Args :

    • app_id -> The App-Id of the App

    • app_password -> The Auth-Token of the App



57
58
59
60
# 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) ⇒ Object

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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