Class: Github::ResponseWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Pagination
Defined in:
lib/github_api/response_wrapper.rb

Overview

A class responsible for proxing to faraday response

Constant Summary

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 Pagination

#auto_paginate, #count_pages, #each_page, #first_page, #has_next_page?, #last_page, #links, #next_page, #page, per_page_as_param, #prev_page

Constructor Details

#initialize(response, current_api) ⇒ ResponseWrapper

Returns a new instance of ResponseWrapper.


18
19
20
21
22
# File 'lib/github_api/response_wrapper.rb', line 18

def initialize(response, current_api)
  @response    = response
  @current_api = current_api
  @env         = response.env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Coerce any method calls for body attributes


122
123
124
125
126
127
128
# File 'lib/github_api/response_wrapper.rb', line 122

def method_missing(method_name, *args, &block)
  if self.has_key?(method_name.to_s)
    self.[](method_name, &block)
  else
    super
  end
end

Instance Attribute Details

#current_apiObject (readonly)

Returns the value of attribute current_api


12
13
14
# File 'lib/github_api/response_wrapper.rb', line 12

def current_api
  @current_api
end

#envObject (readonly)

Returns the value of attribute env


14
15
16
# File 'lib/github_api/response_wrapper.rb', line 14

def env
  @env
end

#responseObject (readonly)

Returns the value of attribute response


10
11
12
# File 'lib/github_api/response_wrapper.rb', line 10

def response
  @response
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Compare the wrapper with other wrapper for equality


148
149
150
151
152
# File 'lib/github_api/response_wrapper.rb', line 148

def ==(other)
  return false unless other.is_a?(self.class)
  return false unless (other.respond_to?(:env) && other.respond_to?(:body))
  self.env == other.env && self.body == other.body
end

#[](key) ⇒ Object

Lookup an attribute from the body if hash, otherwise behave like array index. Convert any key to string before calling.


80
81
82
83
84
85
86
# File 'lib/github_api/response_wrapper.rb', line 80

def [](key)
  if self.body.is_a?(Array)
    self.body[key]
  else
    self.body.send(:"#{key}")
  end
end

#bodyObject

Response raw body


45
46
47
# File 'lib/github_api/response_wrapper.rb', line 45

def body
  @body ? @body : response.body
end

#body=(value) ⇒ Object


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

def body=(value)
  @body = value
  @env[:body] = value
end

#client_error?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/github_api/response_wrapper.rb', line 63

def client_error?
  status.to_i >= 400 && status.to_i < 500
end

#eachObject

Iterate over each resource inside the body


108
109
110
111
112
# File 'lib/github_api/response_wrapper.rb', line 108

def each
  body_parts = self.body.respond_to?(:each) ? self.body : [self.body]
  return body_parts.to_enum unless block_given?
  body_parts.each { |part| yield(part) }
end

#has_key?(key) ⇒ Boolean

Check if body has an attribute

Returns:

  • (Boolean)

116
117
118
# File 'lib/github_api/response_wrapper.rb', line 116

def has_key?(key)
  self.body.is_a?(Hash) && self.body.has_key?(key)
end

#headersObject

Return response headers


73
74
75
# File 'lib/github_api/response_wrapper.rb', line 73

def headers
  Github::Response::Header.new(env)
end

#inspectObject

Print only response body


142
143
144
# File 'lib/github_api/response_wrapper.rb', line 142

def inspect
  "#<#{self.class.name} @body=\"#{self.body}\">"
end

#redirect?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'lib/github_api/response_wrapper.rb', line 59

def redirect?
  status.to_i >= 300 && status.to_i < 400
end

#respond_to?(method_name, include_all = false) ⇒ Boolean

Check if method is defined on the body

Returns:

  • (Boolean)

132
133
134
135
136
137
138
# File 'lib/github_api/response_wrapper.rb', line 132

def respond_to?(method_name, include_all = false)
  if self.has_key?(method_name.to_s)
    true
  else
    super
  end
end

#server_error?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/github_api/response_wrapper.rb', line 67

def server_error?
  status.to_i >= 500 && status.to_i < 600
end

#statusObject

Response status


51
52
53
# File 'lib/github_api/response_wrapper.rb', line 51

def status
  response.status
end

#success?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/github_api/response_wrapper.rb', line 55

def success?
  response.success?
end

#to_aryObject

Convert the ResponseWrapper into an Array


102
103
104
# File 'lib/github_api/response_wrapper.rb', line 102

def to_ary
  body.to_ary
end

#to_hashObject

Convert the ResponseWrapper into a Hash


96
97
98
# File 'lib/github_api/response_wrapper.rb', line 96

def to_hash
  body.to_hash
end

#to_sObject

Return response body as string


90
91
92
# File 'lib/github_api/response_wrapper.rb', line 90

def to_s
  body.to_s
end

#urlObject

Request url


34
35
36
# File 'lib/github_api/response_wrapper.rb', line 34

def url
  response.env[:url].to_s
end