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

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::SERVER, Constants::USER_AGENT

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Connection

#caching?, #clear_cache, #connection, #default_options, #stack

Constructor Details

- (Github::Request) initialize(action, path, api)

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

- (Symbol) action (readonly)

Return http verb

Returns:

  • (Symbol)


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

def action
  @action
end

- (Github::API) 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

- (String) path

Return url

Returns:

  • (String)


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

def path
  @path
end

Instance Method Details

- (Github::ResponseWrapper) call(current_options, params)

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:

  • method (Symbol)
    • The Symbol the HTTP verb

  • path (String)
    • String relative URL to access

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

Returns:



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 53

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

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

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

  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.has_key?('data')
      if params.has_key?('encoder')
        request.params.params_encoder(params.encoder)
      end
      request.url(self.path, params.to_hash)
    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