Class: Ghee::ResourceProxy
- Inherits:
-
Object
- Object
- Ghee::ResourceProxy
- Defined in:
- lib/ghee/resource_proxy.rb
Overview
ResourceProxy lets us create a virtual proxy for any API resource, utilizing method_missing to handle passing messages to the real object
Direct Known Subclasses
API::Authorizations::Proxy, API::Events::Proxy, API::Gists::Proxy, API::Issues::Comments::Proxy, API::Issues::Events::Proxy, API::Issues::Labels::Proxy, API::Issues::Proxy, API::Milestones::Proxy, API::Orgs::Proxy, API::Orgs::Teams::Members::Proxy, API::Orgs::Teams::Proxy, API::Repos::Commits::Proxy, API::Repos::Downloads::Proxy, API::Repos::Git::Blobs::Proxy, API::Repos::Git::Commits::Proxy, API::Repos::Git::Proxy, API::Repos::Git::Refs::Proxy, API::Repos::Git::Tags::Proxy, API::Repos::Git::Trees::Proxy, API::Repos::Hooks::Proxy, API::Repos::Labels::Proxy, API::Repos::Proxy, API::Users::Proxy
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Make connection and path_prefix readable.
-
#current_page ⇒ Object
readonly
Expose pagination data.
-
#pagination ⇒ Object
readonly
Expose pagination data.
-
#params ⇒ Object
readonly
Make connection and path_prefix readable.
-
#path_prefix ⇒ Object
readonly
Make connection and path_prefix readable.
-
#total ⇒ Object
readonly
Expose pagination data.
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(connection, path_prefix, params = {}) ⇒ ResourceProxy
constructor
Instantiates proxy with the connection and path_prefix.
-
#method_missing(message, *args, &block) ⇒ Object
Method_missing takes any message passed to the ResourceProxy and sends it to the real object.
-
#paginate(options) ⇒ Object
Paginate is a helper method to handle request pagination to the github api.
-
#subject ⇒ Object
Subject is the response body parsed as json.
Constructor Details
#initialize(connection, path_prefix, params = {}) ⇒ ResourceProxy
Instantiates proxy with the connection and path_prefix
connection - Ghee::Connection object path_prefix - String
25 26 27 |
# File 'lib/ghee/resource_proxy.rb', line 25 def initialize(connection, path_prefix, params = {}) @connection, @path_prefix, @params = connection, path_prefix, params end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args, &block) ⇒ Object
Method_missing takes any message passed to the ResourceProxy and sends it to the real object
message - Message object args* - Arguements passed
36 37 38 |
# File 'lib/ghee/resource_proxy.rb', line 36 def method_missing(, *args, &block) subject.send(, *args, &block) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Make connection and path_prefix readable
14 15 16 |
# File 'lib/ghee/resource_proxy.rb', line 14 def connection @connection end |
#current_page ⇒ Object (readonly)
Expose pagination data
17 18 19 |
# File 'lib/ghee/resource_proxy.rb', line 17 def current_page @current_page end |
#pagination ⇒ Object (readonly)
Expose pagination data
17 18 19 |
# File 'lib/ghee/resource_proxy.rb', line 17 def pagination @pagination end |
#params ⇒ Object (readonly)
Make connection and path_prefix readable
14 15 16 |
# File 'lib/ghee/resource_proxy.rb', line 14 def params @params end |
#path_prefix ⇒ Object (readonly)
Make connection and path_prefix readable
14 15 16 |
# File 'lib/ghee/resource_proxy.rb', line 14 def path_prefix @path_prefix end |
#total ⇒ Object (readonly)
Expose pagination data
17 18 19 |
# File 'lib/ghee/resource_proxy.rb', line 17 def total @total end |
Instance Method Details
#all ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/ghee/resource_proxy.rb', line 77 def all return self if pagination && next_page.nil? self.paginate :per_page => 100, :page => next_page || 1 self.all end |
#paginate(options) ⇒ Object
Paginate is a helper method to handle request pagination to the github api
options - Hash containing pagination params eg;
:per_page => 100, :page => 1
Returns self
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ghee/resource_proxy.rb', line 58 def paginate() @current_page = .fetch(:page) {raise ArgumentError, ":page parameter required"} per_page = .delete(:per_page) || 30 response = connection.get do |req| req.url path_prefix, :per_page => per_page, :page => current_page req.params.merge! params end if @subject.nil? @subject = response.body else @subject = @subject.concat response.body end parse_link_header response.headers.delete("link") return self end |
#subject ⇒ Object
Subject is the response body parsed as json
Returns json
45 46 47 |
# File 'lib/ghee/resource_proxy.rb', line 45 def subject @subject ||= connection.get(path_prefix){|req| req.params.merge!params }.body end |