Class: PgRest::Api

Inherits:
Object
  • Object
show all
Defined in:
app/services/pg_rest/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, api_token: PgRest.api_token) ⇒ Api

Returns a new instance of Api.



8
9
10
11
# File 'app/services/pg_rest/api.rb', line 8

def initialize(api_url:, api_token: PgRest.api_token)
  @api_url = api_url 
  @api_token = api_token       
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



6
7
8
# File 'app/services/pg_rest/api.rb', line 6

def api_token
  @api_token
end

#api_urlObject

Returns the value of attribute api_url.



6
7
8
# File 'app/services/pg_rest/api.rb', line 6

def api_url
  @api_url
end

Instance Method Details

#add_column(table_name:, name:, type:, default: nil, array: false, primary_key: false, foreign_key: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/pg_rest/api.rb', line 34

def add_column(
    table_name:,
    name:,
    type:,
    default: nil,
    array: false,
    primary_key: false,
    foreign_key: false
  )      
  url = "#{@api_url}/tables/#{table_name}/columns"          
  params = {
    column: {
      name: name, 
      type: type,
      default: default,
      array: array,
      primary_key: primary_key,
      foreign_key: foreign_key
    }
  }.to_json
  resp = RestClient.post(url, params, headers)      
  result = handle_response(resp)
end

#create_table(table_name:) ⇒ Object



19
20
21
22
23
24
25
26
# File 'app/services/pg_rest/api.rb', line 19

def create_table(table_name:)      
  url = "#{@api_url}/tables"    
  params = {
    name: table_name
  }.to_json
  resp = RestClient.post(url, params, headers)      
  result = handle_response(resp)
end

#drop_table(table_name:) ⇒ Object



28
29
30
31
32
# File 'app/services/pg_rest/api.rb', line 28

def drop_table(table_name:)      
  url = "#{@api_url}/tables/#{table_name}"    
  resp = RestClient.delete(url, headers)      
  result = handle_response(resp)
end

#find_table(table_name:) ⇒ Object



13
14
15
16
17
# File 'app/services/pg_rest/api.rb', line 13

def find_table(table_name:)      
  url = "#{@api_url}/tables/#{table_name}"          
  resp = RestClient.get(url, headers)      
  result = handle_response(resp)
end

#handle_response(resp) ⇒ Object



77
78
79
# File 'app/services/pg_rest/api.rb', line 77

def handle_response(resp)
  JSON.parse(resp)
end

#headersObject



70
71
72
73
74
75
# File 'app/services/pg_rest/api.rb', line 70

def headers 
  {
    "Content-Type": "application/json",
    "Authorization": "Bearer #{@api_token}"
  }
end

#remove_column(table_name:, name:) ⇒ Object



58
59
60
61
62
# File 'app/services/pg_rest/api.rb', line 58

def remove_column(table_name:,name:)
  url = "#{@api_url}/tables/#{table_name}/columns/#{name}"    
  resp = RestClient.delete(url, headers)      
  result = handle_response(resp)
end

#schemaObject



64
65
66
67
68
# File 'app/services/pg_rest/api.rb', line 64

def schema 
  url = "#{@api_url}/schema"    
  resp = RestClient.get(url, headers)
  result = handle_response(resp)        
end