Class: Github::Request

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/github_api/request.rb,
lib/github_api/request/verbs.rb,
lib/github_api/request/oauth2.rb,
lib/github_api/request/basic_auth.rb

Overview

A class responsible for dispatching http requests

Defined Under Namespace

Modules: Verbs Classes: BasicAuth, Jsonize, OAuth2

Constant Summary collapse

HTTP_METHODS =
[:get, :head, :post, :put, :delete, :patch]
METHODS_WITH_BODIES =
[:post, :put, :patch]

Constants included from Connection

Connection::ALLOWED_OPTIONS

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPTED_OAUTH_SCOPES, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OAUTH_SCOPES, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::RATELIMIT_RESET, Constants::SERVER, Constants::USER_AGENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#connection, #default_headers, #default_options, #stack

Constructor Details

#initialize(action, path, api) ⇒ Github::Request

Create a new Request



38
39
40
41
42
# File 'lib/github_api/request.rb', line 38

def initialize(action, path, api)
  @action = action
  @path   = path
  @api    = api
end

Instance Attribute Details

#actionSymbol (readonly)

Return http verb

Returns:

  • (Symbol)


21
22
23
# File 'lib/github_api/request.rb', line 21

def action
  @action
end

#apiGithub::API (readonly)

Return api this request is associated with

Returns:



31
32
33
# File 'lib/github_api/request.rb', line 31

def api
  @api
end

#pathString

Return url

Returns:

  • (String)


26
27
28
# File 'lib/github_api/request.rb', line 26

def path
  @path
end

Instance Method Details

#call(current_options, params) ⇒ Github::ResponseWrapper

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs a request

Parameters:

  • current_options (Hash)
  • params (ParamsHash)
    • ParamsHash to configure the request API

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/github_api/request.rb', line 52

def call(current_options, params)
  unless HTTP_METHODS.include?(action)
    raise ArgumentError, "unknown http method: #{action}"
  end

  puts "EXECUTED: #{action} - #{path} with PARAMS: #{params.request_params}" if ENV['DEBUG']

  request_options    = params.options
  connection_options = current_options.merge(request_options)
  conn               = connection(api, connection_options)

  self.path = Utils::Url.normalize(self.path)
  if conn.path_prefix != '/' && self.path.index(conn.path_prefix) != 0
    self.path = (conn.path_prefix + self.path).gsub(/\/(\/)*/, '/')
  end

  response = conn.send(action) do |request|
    case action.to_sym
    when *(HTTP_METHODS - METHODS_WITH_BODIES)
      request.body = params.data if params.key?('data')
      if params.key?('encoder')
        request.params.params_encoder(params.encoder)
      end
      request.url(self.path, params.request_params)
    when *METHODS_WITH_BODIES
      request.url(self.path, connection_options[:query] || {})
      request.body = params.data unless params.empty?
    end
  end
  ResponseWrapper.new(response, api)
end