class Starwars::Request

Wrap request attrs

Constants

BASE_URL
FORMAT

Attributes

as[RW]

The format of the HTTP request @return [String] @api private

method[RW]

The http method for the request @return [Symbol] @api private

params[RW]

Extra params we want to send with the http request @return [Hash] @api private

resource[RW]

The resouce object that we going to call to fetch the data @return [Person, Film, Planet, Specie, Starship, Vehicle] @api private

uri[RW]

The remote url for the resource that we want to fetch @return [String] @api private

Public Class Methods

new(attrs) click to toggle source

Initializer @param [Hash] attrs request attributes @option attrs [Starwars::] :resource @option attrs [Symbol] :method @option attrs [String] :uri @option attrs [Hash] :params @option attrs [String] :as @return [Starwars::Request] @example

data = Request.new(resource: Person.new(id: 1), uri: "/something")

@api public

# File lib/starwars/request.rb, line 45
def initialize(attrs)
  self.resource = attrs.fetch(:resource)
  self.method = attrs.fetch(:method) { :get }
  self.uri = attrs.fetch(:uri)
  self.as = attrs.fetch(:as) { FORMAT }
  self.params = attrs.fetch(:params) { {} }
end

Public Instance Methods

perform_request() click to toggle source

Delegate to the Roar client to fetch data from api @return [Person, Film, Planet, Specie, Starship, Vehicle] @example

request.perform_request

@raise [Starwars::Error] @api public

# File lib/starwars/request.rb, line 60
def perform_request
  resource.send(method, uri: uri, as: as)

  rescue Roar::Transport::Error => e
    raise_http_errors(e.response.code.to_i, e.response.msg)
end

Private Instance Methods

raise_http_errors(status, message) click to toggle source

Check the response code and raise exceptions if needed @param status [Integer] @param message [String] @return [void] @api private

# File lib/starwars/request.rb, line 75
def raise_http_errors(status, message)
  error_class = Starwars::Error.errors[status]
  fail(error_class.new(message, status)) if error_class
end