Class: Github::ResponseWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Pagination
Defined in:
lib/github_api2/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.



22
23
24
25
26
27
# File 'lib/github_api2/response_wrapper.rb', line 22

def initialize(response, current_api)
  @response    = response
  @current_api = current_api
  @env         = response.env
  @body        = nil
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



127
128
129
130
131
132
133
# File 'lib/github_api2/response_wrapper.rb', line 127

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.



16
17
18
# File 'lib/github_api2/response_wrapper.rb', line 16

def current_api
  @current_api
end

#envObject (readonly)

Returns the value of attribute env.



18
19
20
# File 'lib/github_api2/response_wrapper.rb', line 18

def env
  @env
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

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

Compare the wrapper with other wrapper for equality



153
154
155
156
157
# File 'lib/github_api2/response_wrapper.rb', line 153

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.



85
86
87
88
89
90
91
# File 'lib/github_api2/response_wrapper.rb', line 85

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

#bodyObject

Response raw body



50
51
52
# File 'lib/github_api2/response_wrapper.rb', line 50

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

#body=(value) ⇒ Object



43
44
45
46
# File 'lib/github_api2/response_wrapper.rb', line 43

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

#client_error?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/github_api2/response_wrapper.rb', line 68

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

#eachObject

Iterate over each resource inside the body



113
114
115
116
117
# File 'lib/github_api2/response_wrapper.rb', line 113

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)


121
122
123
# File 'lib/github_api2/response_wrapper.rb', line 121

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

#headersObject

Return response headers



78
79
80
# File 'lib/github_api2/response_wrapper.rb', line 78

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

#inspectObject

Print only response body



147
148
149
# File 'lib/github_api2/response_wrapper.rb', line 147

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

#redirect?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/github_api2/response_wrapper.rb', line 64

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)


137
138
139
140
141
142
143
# File 'lib/github_api2/response_wrapper.rb', line 137

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)


72
73
74
# File 'lib/github_api2/response_wrapper.rb', line 72

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

#statusObject

Response status



56
57
58
# File 'lib/github_api2/response_wrapper.rb', line 56

def status
  response.status
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/github_api2/response_wrapper.rb', line 60

def success?
  response.success?
end

#to_aryObject

Convert the ResponseWrapper into an Array



107
108
109
# File 'lib/github_api2/response_wrapper.rb', line 107

def to_ary
  body.to_ary
end

#to_hashObject

Convert the ResponseWrapper into a Hash



101
102
103
# File 'lib/github_api2/response_wrapper.rb', line 101

def to_hash
  body.to_hash
end

#to_sObject

Return response body as string



95
96
97
# File 'lib/github_api2/response_wrapper.rb', line 95

def to_s
  body.to_s
end

#urlObject

Request url



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

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