Class: Postgrest::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/postgrest/http.rb

Constant Summary collapse

METHODS =
{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  patch: Net::HTTP::Patch,
  put: Net::HTTP::Patch,
  delete: Net::HTTP::Delete
}.freeze
RESPONSES =
{
  get: Responses::GetResponse,
  post: Responses::PostResponse,
  put: Responses::PatchResponse,
  patch: Responses::PatchResponse,
  delete: Responses::DeleteResponse,
  options: Responses::GetResponse # ?
}.freeze
USER_AGENT =
'PostgREST Ruby Client'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, query: {}, body: {}, headers: {}, http_method: :get) ⇒ HTTP

Returns a new instance of HTTP.



26
27
28
29
30
31
32
33
34
# File 'lib/postgrest/http.rb', line 26

def initialize(uri:, query: {}, body: {}, headers: {}, http_method: :get)
  @uri = uri
  @body = body
  @headers = headers
  @http_method = http_method.to_sym
  @response = nil
  @request = nil
  uri.query = decode_query_params(query)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def headers
  @headers
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def http_method
  @http_method
end

#queryObject (readonly)

Returns the value of attribute query.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def query
  @query
end

#requestObject (readonly)

Returns the value of attribute request.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def response
  @response
end

#uriObject (readonly)

Returns the value of attribute uri.



24
25
26
# File 'lib/postgrest/http.rb', line 24

def uri
  @uri
end

Instance Method Details

#callObject Also known as: execute

Raises:



42
43
44
45
46
47
48
49
50
51
# File 'lib/postgrest/http.rb', line 42

def call
  raise InvalidHTTPMethod unless valid_http_method?

  @response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl?) do |http|
    @request = create_request
    http.request(request)
  end

  RESPONSES[http_method].new(request, response)
end

#update_query_params(new_value = {}) ⇒ Object



36
37
38
39
40
# File 'lib/postgrest/http.rb', line 36

def update_query_params(new_value = {})
  @uri.query = decode_query_params(new_value)
rescue NoMethodError
  @uri.query
end