Class: SearchKit::Clients::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/search_kit/clients/scaffold.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScaffold

Returns a new instance of Scaffold.



9
10
11
12
13
# File 'lib/search_kit/clients/scaffold.rb', line 9

def initialize
  uri = [SearchKit.config.app_uri, "scaffold"].join("/")
  @connection = Faraday.new(uri)
  @token      = SearchKit.config.app_token
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/search_kit/clients/scaffold.rb', line 7

def connection
  @connection
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/search_kit/clients/scaffold.rb', line 7

def token
  @token
end

Instance Method Details

#create(name, documents) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/search_kit/clients/scaffold.rb', line 15

def create(name, documents)
  options = {
    token: token,
    data: {
      type:          'indices',
      attributes:    { name: name },
      relationships: { documents: documents }
    }
  }

  response = connection.post('', options)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::Unauthorized  if response.status == 401
  fail Errors::Unprocessable if response.status == 422

  body
end