Class: NRB::HTTPService

Inherits:
Object
  • Object
show all
Defined in:
lib/drink-socially/http_service.rb,
lib/drink-socially/http_service/response.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_MIDDLEWARE =
Proc.new do |faraday| 
  faraday.adapter Faraday.default_adapter
  faraday.request  :url_encoded
  faraday.response :oj
end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, connection_opts = {}) ⇒ HTTPService

Returns a new instance of HTTPService.



32
33
34
35
36
37
38
39
# File 'lib/drink-socially/http_service.rb', line 32

def initialize(args={}, connection_opts={})
  @connection_opts = connection_opts
  @response_class = args.delete(:response_class) || self.class.default_response_class
  @verb = args.delete(:verb)
  @url = args.delete(:url)
  @params = process_args(args)
  @args = args
end

Class Attribute Details

.faraday_middlewareObject

Returns the value of attribute faraday_middleware.



16
17
18
# File 'lib/drink-socially/http_service.rb', line 16

def faraday_middleware
  @faraday_middleware
end

Class Method Details

.default_http_classObject



19
# File 'lib/drink-socially/http_service.rb', line 19

def default_http_class; Faraday; end

.default_middlewareObject



18
# File 'lib/drink-socially/http_service.rb', line 18

def default_middleware; DEFAULT_MIDDLEWARE; end

.default_response_classObject



20
# File 'lib/drink-socially/http_service.rb', line 20

def default_response_class; Response; end

.make_request(args = {}, connection_opts = {}) ⇒ Object



23
24
25
# File 'lib/drink-socially/http_service.rb', line 23

def make_request(args={}, connection_opts={})
  new(args,connection_opts).make_request
end

Instance Method Details

#make_requestObject



42
43
44
45
46
47
48
49
# File 'lib/drink-socially/http_service.rb', line 42

def make_request
  connection = self.class.default_http_class.new @url, @connection_opts, &self.class.faraday_middleware
  response = connection.send @verb, @url, @params
  args = @args.merge( { body: response.body, headers: response.headers, status: response.status.to_i } )
  @response_class.new args
rescue Faraday::Error::ParsingError => e
  self.class.default_response_class.new body: {error: e.message}, status: 500
end