Module: ApiFrame::EndpointMethods

Defined in:
lib/api_frame/endpoint_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



48
49
50
# File 'lib/api_frame/endpoint_methods.rb', line 48

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#base_uriObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/api_frame/endpoint_methods.rb', line 10

def base_uri
  raise NotImplementedError
end

#default_content_typeObject



18
19
20
# File 'lib/api_frame/endpoint_methods.rb', line 18

def default_content_type
  'application/json'
end

#default_headersObject



14
15
16
# File 'lib/api_frame/endpoint_methods.rb', line 14

def default_headers
  {}
end

#default_response_parserObject



22
23
24
25
26
# File 'lib/api_frame/endpoint_methods.rb', line 22

def default_response_parser
  proc do |response|
    JSON.parse(response.body)
  end
end

#perform_request(method, api_path, query: nil, body: nil, headers: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/api_frame/endpoint_methods.rb', line 28

def perform_request(method, api_path, query: nil, body: nil, headers: nil)
  uri = self.base_uri + api_path
  uri.query = URI.encode_www_form(query) if query
  
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    ApiFrame::Utils.request_type_from_method_argument(method).new(uri).tap do |request|
      default_headers.merge(headers || {}).each do |name, value|
        request[name] = value
      end
      
      if body
        request['Content-Type'] = default_content_type
        request.body            = body
      end
    end.then do |request|
      http.request(request)
    end
  end
end