github_api 0.8.11 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/README.md +21 -14
  2. data/features/activity/notifications.feature +15 -0
  3. data/features/cassettes/activity/notifications/list_user.yml +54 -0
  4. data/features/step_definitions/common_steps.rb +8 -9
  5. data/lib/github_api.rb +3 -4
  6. data/lib/github_api/activity.rb +8 -13
  7. data/lib/github_api/activity/events.rb +0 -5
  8. data/lib/github_api/activity/notifications.rb +0 -5
  9. data/lib/github_api/api.rb +27 -22
  10. data/lib/github_api/api_factory.rb +13 -12
  11. data/lib/github_api/authorizations.rb +0 -5
  12. data/lib/github_api/client.rb +33 -33
  13. data/lib/github_api/configuration.rb +10 -2
  14. data/lib/github_api/connection.rb +2 -3
  15. data/lib/github_api/constants.rb +0 -3
  16. data/lib/github_api/gists.rb +2 -7
  17. data/lib/github_api/git_data.rb +10 -15
  18. data/lib/github_api/git_data/blobs.rb +0 -5
  19. data/lib/github_api/git_data/commits.rb +0 -5
  20. data/lib/github_api/git_data/references.rb +3 -7
  21. data/lib/github_api/git_data/tags.rb +0 -5
  22. data/lib/github_api/git_data/trees.rb +0 -5
  23. data/lib/github_api/gitignore.rb +0 -5
  24. data/lib/github_api/issues.rb +10 -15
  25. data/lib/github_api/issues/comments.rb +0 -5
  26. data/lib/github_api/issues/events.rb +0 -5
  27. data/lib/github_api/issues/labels.rb +0 -5
  28. data/lib/github_api/issues/milestones.rb +0 -5
  29. data/lib/github_api/markdown.rb +0 -5
  30. data/lib/github_api/meta.rb +0 -5
  31. data/lib/github_api/orgs.rb +4 -9
  32. data/lib/github_api/page_iterator.rb +44 -29
  33. data/lib/github_api/paged_request.rb +12 -12
  34. data/lib/github_api/{result.rb → pagination.rb} +19 -86
  35. data/lib/github_api/params_hash.rb +1 -1
  36. data/lib/github_api/pull_requests.rb +2 -7
  37. data/lib/github_api/repos.rb +22 -27
  38. data/lib/github_api/request.rb +7 -4
  39. data/lib/github_api/response/header.rb +76 -0
  40. data/lib/github_api/response_wrapper.rb +126 -0
  41. data/lib/github_api/scopes.rb +1 -1
  42. data/lib/github_api/search.rb +0 -5
  43. data/lib/github_api/users.rb +6 -11
  44. data/lib/github_api/version.rb +2 -2
  45. data/spec/github/activity/notifications/get_spec.rb +1 -1
  46. data/spec/github/activity/starring/starred_spec.rb +1 -1
  47. data/spec/github/activity/watching/watched_spec.rb +1 -1
  48. data/spec/github/api_factory_spec.rb +7 -8
  49. data/spec/github/api_spec.rb +0 -7
  50. data/spec/github/authorization_spec.rb +22 -32
  51. data/spec/github/authorizations/create_spec.rb +49 -0
  52. data/spec/github/authorizations/delete_spec.rb +39 -0
  53. data/spec/github/authorizations/get_spec.rb +49 -0
  54. data/spec/github/authorizations/list_spec.rb +55 -0
  55. data/spec/github/authorizations/update_spec.rb +50 -0
  56. data/spec/github/authorizations_spec.rb +2 -236
  57. data/spec/github/gists/comments/create_spec.rb +1 -1
  58. data/spec/github/gists/comments/edit_spec.rb +1 -1
  59. data/spec/github/gists/comments/get_spec.rb +1 -1
  60. data/spec/github/gists/create_spec.rb +1 -1
  61. data/spec/github/gists/edit_spec.rb +1 -1
  62. data/spec/github/gists/fork_spec.rb +1 -1
  63. data/spec/github/gists/get_spec.rb +1 -1
  64. data/spec/github/git_data/blobs/create_spec.rb +1 -1
  65. data/spec/github/git_data/blobs/get_spec.rb +1 -1
  66. data/spec/github/git_data/commits/create_spec.rb +1 -1
  67. data/spec/github/git_data/commits/get_spec.rb +1 -1
  68. data/spec/github/git_data/references/create_spec.rb +2 -2
  69. data/spec/github/git_data/references/get_spec.rb +1 -1
  70. data/spec/github/git_data/references/list_spec.rb +16 -6
  71. data/spec/github/git_data/tags/create_spec.rb +1 -1
  72. data/spec/github/git_data/tags/get_spec.rb +1 -1
  73. data/spec/github/git_data/trees/create_spec.rb +1 -1
  74. data/spec/github/git_data/trees/get_spec.rb +4 -4
  75. data/spec/github/issues/comments/create_spec.rb +1 -1
  76. data/spec/github/issues/comments/edit_spec.rb +1 -1
  77. data/spec/github/issues/comments/get_spec.rb +1 -1
  78. data/spec/github/issues/create_spec.rb +1 -1
  79. data/spec/github/issues/edit_spec.rb +1 -1
  80. data/spec/github/issues/events/get_spec.rb +1 -1
  81. data/spec/github/issues/get_spec.rb +1 -1
  82. data/spec/github/issues/labels/create_spec.rb +1 -1
  83. data/spec/github/issues/labels/get_spec.rb +1 -1
  84. data/spec/github/issues/labels/update_spec.rb +1 -1
  85. data/spec/github/issues/milestones/create_spec.rb +1 -1
  86. data/spec/github/issues/milestones/delete_spec.rb +1 -1
  87. data/spec/github/issues/milestones/get_spec.rb +1 -1
  88. data/spec/github/issues/milestones/update_spec.rb +1 -1
  89. data/spec/github/orgs/edit_spec.rb +1 -1
  90. data/spec/github/orgs/get_spec.rb +1 -1
  91. data/spec/github/orgs/teams/create_spec.rb +1 -1
  92. data/spec/github/orgs/teams/edit_spec.rb +1 -1
  93. data/spec/github/orgs/teams/get_spec.rb +1 -1
  94. data/spec/github/paged_request_spec.rb +34 -29
  95. data/spec/github/pagination/iterator/number_spec.rb +118 -0
  96. data/spec/github/pagination/iterator/sha_spec.rb +67 -0
  97. data/spec/github/pull_requests/comments/create_spec.rb +1 -1
  98. data/spec/github/pull_requests/comments/edit_spec.rb +1 -1
  99. data/spec/github/pull_requests/comments/get_spec.rb +1 -1
  100. data/spec/github/pull_requests/create_spec.rb +1 -1
  101. data/spec/github/pull_requests/get_spec.rb +1 -1
  102. data/spec/github/pull_requests/update_spec.rb +1 -1
  103. data/spec/github/repos/branch_spec.rb +1 -1
  104. data/spec/github/repos/comments/create_spec.rb +1 -1
  105. data/spec/github/repos/comments/get_spec.rb +1 -1
  106. data/spec/github/repos/comments/update_spec.rb +1 -1
  107. data/spec/github/repos/commits/get_spec.rb +1 -1
  108. data/spec/github/repos/create_spec.rb +1 -1
  109. data/spec/github/repos/downloads/create_spec.rb +1 -1
  110. data/spec/github/repos/downloads/get_spec.rb +1 -1
  111. data/spec/github/repos/edit_spec.rb +1 -1
  112. data/spec/github/repos/forks/create_spec.rb +1 -1
  113. data/spec/github/repos/get_spec.rb +1 -1
  114. data/spec/github/repos/hooks/create_spec.rb +1 -1
  115. data/spec/github/repos/hooks/edit_spec.rb +1 -1
  116. data/spec/github/repos/hooks/get_spec.rb +1 -1
  117. data/spec/github/repos/languages_spec.rb +1 -1
  118. data/spec/github/repos/merging/merge_spec.rb +1 -1
  119. data/spec/github/repos/statuses/create_spec.rb +1 -1
  120. data/spec/github/repos/tags_spec.rb +1 -1
  121. data/spec/github/repos/teams_spec.rb +1 -1
  122. data/spec/github/request/endpoint_spec.rb +24 -0
  123. data/spec/github/request/request_spec.rb +38 -0
  124. data/spec/github/{result_spec.rb → response_wrapper_spec.rb} +14 -13
  125. data/spec/github/users/emails/list_spec.rb +1 -1
  126. data/spec/github/users/get_spec.rb +2 -7
  127. data/spec/github/users/keys/create_spec.rb +1 -1
  128. data/spec/github/users/keys/get_spec.rb +1 -1
  129. data/spec/github/users/keys/update_spec.rb +1 -1
  130. data/spec/github/users/update_spec.rb +1 -1
  131. data/spec/github_spec.rb +1 -6
  132. data/spec/{github/activity → integration}/activity_spec.rb +0 -0
  133. data/spec/{github/gists → integration}/gists_spec.rb +2 -0
  134. data/spec/{github/git_data → integration}/git_data_spec.rb +0 -0
  135. data/spec/{github/issues → integration}/issues_spec.rb +2 -0
  136. data/spec/integration/options_spec.rb +103 -23
  137. data/spec/{github/orgs → integration}/orgs_spec.rb +0 -0
  138. data/spec/{github/pull_requests → integration}/pull_requests_spec.rb +0 -0
  139. data/spec/{github → integration}/repos_spec.rb +2 -0
  140. data/spec/{github/users → integration}/users_spec.rb +0 -0
  141. data/spec/shared/array_of_resources_behaviour.rb +1 -1
  142. metadata +55 -46
  143. data/lib/github_api/page_uri_processor.rb +0 -25
  144. data/lib/github_api/response/helpers.rb +0 -21
  145. data/spec/github/page_iterator_spec.rb +0 -260
  146. data/spec/github/response/helpers_spec.rb +0 -16
@@ -1,27 +1,30 @@
1
+ # encoding: utf-8
2
+
1
3
  module Github
4
+
5
+ # A module that adds http get request to response pagination
2
6
  module PagedRequest
3
7
  include Github::Constants
4
8
 
5
- extend self
6
-
7
9
  FIRST_PAGE = 1 # Default request page if none provided
8
10
 
9
11
  PER_PAGE = 30 # Default number of items as specified by API
10
12
 
11
13
  NOT_FOUND = -1 # Either page or per_page parameter not present
12
14
 
13
- class << self
14
- attr_accessor :page, :per_page
15
- end
16
-
15
+ # Check if current api instance has default per_page param set,
16
+ # otherwise use global default.
17
+ #
17
18
  def default_page_size
18
- Github.api_client.per_page ? Github.api_client.per_page : PER_PAGE
19
+ current_api.per_page ? current_api.per_page : PER_PAGE
19
20
  end
20
21
 
21
22
  def default_page
22
- Github.api_client.page ? Github.api_client.page : FIRST_PAGE
23
+ current_api.page ? current_api.page : FIRST_PAGE
23
24
  end
24
25
 
26
+ # Perform http get request with paginatoin parameters
27
+ #
25
28
  def page_request(path, params={})
26
29
  if params[PARAM_PER_PAGE] == NOT_FOUND
27
30
  params[PARAM_PER_PAGE] = default_page_size
@@ -30,10 +33,7 @@ module Github
30
33
  params[PARAM_PAGE] = default_page
31
34
  end
32
35
 
33
- Github::PagedRequest.page = params[PARAM_PAGE]
34
- Github::PagedRequest.per_page = params[PARAM_PER_PAGE]
35
-
36
- Github.api_client.get_request path, params
36
+ current_api.get_request(path, params)
37
37
  end
38
38
 
39
39
  end # PagedRequest
@@ -1,87 +1,25 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Github
4
- module Result
5
- include Github::Constants
6
-
7
- # TODO Add result counts method to check total items looking at result links
8
-
9
- # All request headers
10
- def headers
11
- loaded? ? @env[:response_headers] : nil
12
- end
13
-
14
- def oauth_scopes
15
- loaded? ? @env[:response_headers][OAUTH_SCOPES] : nil
16
- end
17
-
18
- def accepted_oauth_scopes
19
- loaded? ? @env[:response_headers][ACCEPTED_OAUTH_SCOPES] : nil
20
- end
21
-
22
- # Requests are limited to API v3 to 5000 per hour.
23
- def ratelimit_limit
24
- loaded? ? @env[:response_headers][RATELIMIT_LIMIT] : nil
25
- end
26
-
27
- def ratelimit_remaining
28
- loaded? ? @env[:response_headers][RATELIMIT_REMAINING] : nil
29
- end
30
-
31
- def cache_control
32
- loaded? ? @env[:response_headers][CACHE_CONTROL] : nil
33
- end
34
-
35
- def content_type
36
- loaded? ? @env[:response_headers][CONTENT_TYPE] : nil
37
- end
38
-
39
- def content_length
40
- loaded? ? @env[:response_headers][CONTENT_LENGTH] : nil
41
- end
42
-
43
- def etag
44
- loaded? ? @env[:response_headers][ETAG] : nil
45
- end
46
-
47
- def date
48
- loaded? ? @env[:response_headers][DATE] : nil
49
- end
50
-
51
- def location
52
- loaded? ? @env[:response_headers][LOCATION] : nil
53
- end
54
4
 
55
- def server
56
- loaded? ? @env[:response_headers][SERVER] : nil
57
- end
58
-
59
- def status
60
- loaded? ? @env[:status] : nil
61
- end
62
-
63
- def success?
64
- (200..299).include? status
65
- end
66
-
67
- # Returns raw body
68
- def body
69
- loaded? ? @env[:body] : nil
70
- end
71
-
72
- def loaded?
73
- !!@env
74
- end
5
+ # A module that decorates response with pagination helpers
6
+ module Pagination
7
+ include Github::Constants
75
8
 
76
9
  # Return page links
77
10
  def links
78
- @@links = Github::PageLinks.new(@env[:response_headers])
11
+ @links = Github::PageLinks.new(env[:response_headers])
12
+ end
13
+
14
+ # Retrive number of total pages base on current :per_page parameter
15
+ def count_pages
16
+ page_iterator.count.to_i
79
17
  end
80
18
 
81
19
  # Iterator like each for response pages. If there are no pages to
82
- # iterate over this method will return nothing.
20
+ # iterate over this method will return current page.
83
21
  def each_page
84
- yield self.body
22
+ yield self
85
23
  while page_iterator.has_next?
86
24
  yield next_page
87
25
  end
@@ -93,7 +31,7 @@ module Github
93
31
  def first_page
94
32
  first_request = page_iterator.first
95
33
  self.instance_eval { @env = first_request.env } if first_request
96
- self.body
34
+ first_request
97
35
  end
98
36
 
99
37
  # Retrives the result of the next page. Returns <tt>nil</tt> if there is
@@ -101,7 +39,7 @@ module Github
101
39
  def next_page
102
40
  next_request = page_iterator.next
103
41
  self.instance_eval { @env = next_request.env } if next_request
104
- self.body
42
+ next_request
105
43
  end
106
44
 
107
45
  # Retrives the result of the previous page. Returns <tt>nil</tt> if there is
@@ -109,7 +47,7 @@ module Github
109
47
  def prev_page
110
48
  prev_request = page_iterator.prev
111
49
  self.instance_eval { @env = prev_request.env } if prev_request
112
- self.body
50
+ prev_request
113
51
  end
114
52
  alias :previous_page :prev_page
115
53
 
@@ -119,7 +57,7 @@ module Github
119
57
  def last_page
120
58
  last_request = page_iterator.last
121
59
  self.instance_eval { @env = last_request.env } if last_request
122
- self.body
60
+ last_request
123
61
  end
124
62
 
125
63
  # Retrives a specific result for a page given page number.
@@ -129,7 +67,7 @@ module Github
129
67
  def page(page_number)
130
68
  request = page_iterator.get_page(page_number)
131
69
  self.instance_eval { @env = request.env } if request
132
- self.body
70
+ request
133
71
  end
134
72
 
135
73
  # Returns <tt>true</tt> if there is another page in the result set,
@@ -138,17 +76,12 @@ module Github
138
76
  page_iterator.has_next?
139
77
  end
140
78
 
141
- # Repopulates objects for new values
142
- def reset
143
- nil
144
- end
145
-
146
- private
79
+ private
147
80
 
148
81
  # Internally used page iterator
149
82
  def page_iterator # :nodoc:
150
- @@page_iterator = Github::PageIterator.new(@env)
83
+ @page_iterator = Github::PageIterator.new(links, current_api)
151
84
  end
152
85
 
153
- end # Result
86
+ end # Pagination
154
87
  end # Github
@@ -3,7 +3,7 @@ module Github
3
3
 
4
4
  def initialize(*args, &block)
5
5
  hash = args.extract_options!
6
- debugger
6
+ # debugger
7
7
  # debugger
8
8
  # normalize_keys!(hash)
9
9
  # debugger
@@ -23,14 +23,9 @@ module Github
23
23
  'state' => %w[ open closed ]
24
24
  }
25
25
 
26
- # Creates new Gists API
27
- def initialize(options = {})
28
- super(options)
29
- end
30
-
31
26
  # Access to PullRequests::Comments API
32
- def comments
33
- @comments ||= ApiFactory.new 'PullRequests::Comments'
27
+ def comments(options={}, &block)
28
+ @comments ||= ApiFactory.new('PullRequests::Comments', current_options.merge(options), &block)
34
29
  end
35
30
 
36
31
  # List pull requests
@@ -41,64 +41,59 @@ module Github
41
41
 
42
42
  VALID_REPO_TYPES = %w[ all public private member ].freeze
43
43
 
44
- # Creates new Repositories API
45
- def initialize(options = {})
46
- super(options)
47
- end
48
-
49
44
  # Access to Repos::Collaborators API
50
- def collaborators
51
- @collaborators ||= ApiFactory.new 'Repos::Collaborators'
45
+ def collaborators(options={}, &block)
46
+ @collaborators ||= ApiFactory.new('Repos::Collaborators', current_options.merge(options), &block)
52
47
  end
53
48
 
54
49
  # Access to Repos::Comments API
55
- def comments
56
- @commits ||= ApiFactory.new 'Repos::Comments'
50
+ def comments(options={}, &block)
51
+ @commits ||= ApiFactory.new('Repos::Comments', current_options.merge(options), &block)
57
52
  end
58
53
 
59
54
  # Access to Repos::Commits API
60
- def commits
61
- @commits ||= ApiFactory.new 'Repos::Commits'
55
+ def commits(options={}, &block)
56
+ @commits ||= ApiFactory.new('Repos::Commits', current_options.merge(options), &block)
62
57
  end
63
58
 
64
59
  # Access to Repos::Contents API
65
- def contents
66
- @contents ||= ApiFactory.new 'Repos::Contents'
60
+ def contents(options={}, &block)
61
+ @contents ||= ApiFactory.new('Repos::Contents', current_options.merge(options), &block)
67
62
  end
68
63
 
69
64
  # Access to Repos::Downloads API
70
- def downloads
71
- @downloads ||= ApiFactory.new 'Repos::Downloads'
65
+ def downloads(options={}, &block)
66
+ @downloads ||= ApiFactory.new('Repos::Downloads', current_options.merge(options), &block)
72
67
  end
73
68
 
74
69
  # Access to Repos::Forks API
75
- def forks
76
- @forks ||= ApiFactory.new 'Repos::Forks'
70
+ def forks(options={}, &block)
71
+ @forks ||= ApiFactory.new('Repos::Forks', current_options.merge(options), &block)
77
72
  end
78
73
 
79
74
  # Access to Repos::Hooks API
80
- def hooks
81
- @hooks ||= ApiFactory.new 'Repos::Hooks'
75
+ def hooks(options={}, &block)
76
+ @hooks ||= ApiFactory.new('Repos::Hooks', current_options.merge(options), &block)
82
77
  end
83
78
 
84
79
  # Access to Repos::Keys API
85
- def keys
86
- @keys ||= ApiFactory.new 'Repos::Keys'
80
+ def keys(options={}, &block)
81
+ @keys ||= ApiFactory.new('Repos::Keys', current_options.merge(options), &block)
87
82
  end
88
83
 
89
84
  # Access to Repos::Merging API
90
- def merging
91
- @mergin ||= ApiFactory.new 'Repos::Merging'
85
+ def merging(options={}, &block)
86
+ @mergin ||= ApiFactory.new('Repos::Merging', current_options.merge(options), &block)
92
87
  end
93
88
 
94
89
  # Access to Repos::Watchin API
95
- def pubsubhubbub
96
- @pubsubhubbub ||= ApiFactory.new 'Repos::PubSubHubbub'
90
+ def pubsubhubbub(options={}, &block)
91
+ @pubsubhubbub ||= ApiFactory.new('Repos::PubSubHubbub', current_options.merge(options), &block)
97
92
  end
98
93
 
99
94
  # Access to Repos::Statuses API
100
- def statuses
101
- @statuses ||= ApiFactory.new 'Repos::Statuses'
95
+ def statuses(options={}, &block)
96
+ @statuses ||= ApiFactory.new('Repos::Statuses', current_options.merge(options), &block)
102
97
  end
103
98
 
104
99
  # List branches
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Github
4
+
4
5
  # Defines HTTP verbs
5
6
  module Request
6
7
 
@@ -27,7 +28,7 @@ module Github
27
28
  request(:delete, path, params, options)
28
29
  end
29
30
 
30
- def request(method, path, params, options)
31
+ def request(method, path, params, options) # :nodoc:
31
32
  if !METHODS.include?(method)
32
33
  raise ArgumentError, "unkown http method: #{method}"
33
34
  end
@@ -35,8 +36,10 @@ module Github
35
36
 
36
37
  puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']
37
38
 
38
- conn = connection(options)
39
- path = (conn.path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'
39
+ conn = connection(options.merge(current_options))
40
+ if conn.path_prefix != '/' && path.index(conn.path_prefix) != 0
41
+ path = (conn.path_prefix + path).gsub(/\/(\/)*/, '/')
42
+ end
40
43
 
41
44
  response = conn.send(method) do |request|
42
45
  case method.to_sym
@@ -48,7 +51,7 @@ module Github
48
51
  request.body = extract_data_from_params(params) unless params.empty?
49
52
  end
50
53
  end
51
- response.body
54
+ ResponseWrapper.new(response, self)
52
55
  end
53
56
 
54
57
  private
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Response
5
+
6
+ # Represents http response header
7
+ class Header < Struct.new(:env)
8
+ include Github::Constants
9
+
10
+ SUCCESSFUL_STATUSES = 200..299
11
+
12
+ def loaded?
13
+ !!env
14
+ end
15
+
16
+ def oauth_scopes
17
+ loaded? ? env[:response_headers][OAUTH_SCOPES] : nil
18
+ end
19
+
20
+ def accepted_oauth_scopes
21
+ loaded? ? env[:response_headers][ACCEPTED_OAUTH_SCOPES] : nil
22
+ end
23
+
24
+ # Requests are limited to API v3 to 5000 per hour.
25
+ def ratelimit_limit
26
+ loaded? ? env[:response_headers][RATELIMIT_LIMIT] : nil
27
+ end
28
+
29
+ def ratelimit_remaining
30
+ loaded? ? env[:response_headers][RATELIMIT_REMAINING] : nil
31
+ end
32
+
33
+ def cache_control
34
+ loaded? ? env[:response_headers][CACHE_CONTROL] : nil
35
+ end
36
+
37
+ def content_type
38
+ loaded? ? env[:response_headers][CONTENT_TYPE] : nil
39
+ end
40
+
41
+ def content_length
42
+ loaded? ? env[:response_headers][CONTENT_LENGTH] : nil
43
+ end
44
+
45
+ def etag
46
+ loaded? ? env[:response_headers][ETAG] : nil
47
+ end
48
+
49
+ def date
50
+ loaded? ? env[:response_headers][DATE] : nil
51
+ end
52
+
53
+ def location
54
+ loaded? ? env[:response_headers][LOCATION] : nil
55
+ end
56
+
57
+ def server
58
+ loaded? ? env[:response_headers][SERVER] : nil
59
+ end
60
+
61
+ def status
62
+ loaded? ? env[:status] : nil
63
+ end
64
+
65
+ def success?
66
+ SUCCESSFUL_STATUSES.include? status
67
+ end
68
+
69
+ # Returns raw body
70
+ def body
71
+ loaded? ? env[:body] : nil
72
+ end
73
+
74
+ end # Header
75
+ end # Response
76
+ end # Github
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+
5
+ # A class responsible for proxing to faraday response
6
+ class ResponseWrapper
7
+ extend Forwardable
8
+ include Pagination
9
+ include Enumerable
10
+
11
+ attr_reader :response
12
+
13
+ attr_reader :current_api
14
+
15
+ attr_reader :env
16
+
17
+ def_delegators :body, :empty?, :size, :include?, :length, :to_a, :first, :flatten, :include?, :keys, :[]
18
+
19
+ def initialize(response, current_api)
20
+ @response = response
21
+ @current_api = current_api
22
+ @env = response.env
23
+ end
24
+
25
+ # Request url
26
+ #
27
+ def url
28
+ response.env[:url].to_s
29
+ end
30
+
31
+ # Response raw body
32
+ #
33
+ def body
34
+ response.body
35
+ end
36
+
37
+ # Response status
38
+ #
39
+ def status
40
+ response.status
41
+ end
42
+
43
+ def success?
44
+ response.success?
45
+ end
46
+
47
+ # Return response headers
48
+ #
49
+ def headers
50
+ Github::Response::Header.new(env)
51
+ end
52
+
53
+ # Lookup an attribute from the body.
54
+ # Convert any key to string before calling.
55
+ #
56
+ def [](key)
57
+ self.body.send(:"#{key}")
58
+ end
59
+
60
+ # Return response body as string
61
+ #
62
+ def to_s
63
+ body.to_s
64
+ end
65
+
66
+ # Convert the ResponseWrapper into a Hash
67
+ #
68
+ def to_hash
69
+ body.to_hash
70
+ end
71
+
72
+ # Convert the ResponseWrapper into an Array
73
+ #
74
+ def to_ary
75
+ body.to_ary
76
+ end
77
+
78
+ # Iterate over each resource inside the body
79
+ #
80
+ def each
81
+ body_parts = self.body.respond_to?(:each) ? self.body : [self.body]
82
+ return body_parts.to_enum unless block_given?
83
+ body_parts.each { |part| yield(part) }
84
+ end
85
+
86
+ # Check if body has an attribute
87
+ #
88
+ def has_key?(key)
89
+ !self.body.is_a?(Array) && self.body.has_key?(key)
90
+ end
91
+
92
+ # Coerce any method calls for body attributes
93
+ #
94
+ def method_missing(method_name, *args, &block)
95
+ if self.has_key?(method_name.to_s)
96
+ self.[](method_name, &block)
97
+ else
98
+ super
99
+ end
100
+ end
101
+
102
+ # Check if method is defined on the body
103
+ #
104
+ def respond_to?(method_name)
105
+ if self.has_key?(method_name.to_s)
106
+ true
107
+ else
108
+ super
109
+ end
110
+ end
111
+
112
+ # Print only response body
113
+ #
114
+ def inspect
115
+ "#<#{self.class.name} @body=\"#{self.body}\">"
116
+ end
117
+
118
+ # Compare the wrapper with other wrapper for equality
119
+ #
120
+ def ==(other)
121
+ self.env == other.env &&
122
+ self.body == other.body
123
+ end
124
+
125
+ end # ResponseWrapper
126
+ end # Github