Class: Ghee::ResourceProxy
- Inherits:
-
Object
- Object
- Ghee::ResourceProxy
- Includes:
- CUD
- 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::Downloads::Proxy, API::Events::Proxy, API::Gists::Comments::Proxy, API::Gists::Proxy, API::Membership::MembershipProxy, API::Orgs::Members::Proxy, API::Orgs::Memberships::MembershipsProxy, API::Orgs::Proxy, API::Orgs::PublicMembers::Proxy, API::Orgs::Teams::Members::Proxy, API::Orgs::Teams::Proxy, API::Proxy, API::Repos::Assignees::Proxy, API::Repos::Collaborators::Proxy, API::Repos::Commits::Comments::Proxy, API::Repos::Commits::Proxy, API::Repos::Commits::Statuses::Proxy, API::Repos::Contents::Proxy, API::Repos::Forks::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::Issues::Assignees::Proxy, API::Repos::Issues::Comments::Proxy, API::Repos::Issues::Events::Proxy, API::Repos::Issues::Labels::Proxy, API::Repos::Issues::Proxy, API::Repos::Keys::Proxy, API::Repos::Labels::Proxy, API::Repos::Milestones::Proxy, API::Repos::Proxy, API::Repos::Pulls::Proxy, API::Search::Issues::Proxy, API::Search::Proxy, API::Users::Emails::Proxy, API::Users::Keys::Proxy, API::Users::Memberships::Proxy, API::Users::Proxy
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Make connection and path_prefix readable.
-
#current_page ⇒ Object
readonly
Expose pagination data.
-
#id ⇒ Object
readonly
Make connection and path_prefix readable.
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
- #all(&block) ⇒ Object
- #all_parallel ⇒ Object
- #build_prefix(first_argument, endpoint) ⇒ Object
-
#initialize(connection, path_prefix, params = {}, &block) ⇒ 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, &block) ⇒ Object
Paginate is a helper method to handle request pagination to the github api.
-
#raw ⇒ Object
Raw is the raw response from the faraday Use this if you need access to status codes or header values.
-
#subject ⇒ Object
Subject is the response body parsed as json.
Methods included from CUD
Constructor Details
#initialize(connection, path_prefix, params = {}, &block) ⇒ ResourceProxy
Instantiates proxy with the connection and path_prefix
connection - Ghee::Connection object path_prefix - String
33 34 35 36 37 38 39 40 41 |
# File 'lib/ghee/resource_proxy.rb', line 33 def initialize(connection, path_prefix, params = {}, &block) if !params.is_a?Hash @id = params params = {} end @connection, @path_prefix, @params = connection, URI.escape(path_prefix), params @block = block if block subject if block 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
50 51 52 |
# File 'lib/ghee/resource_proxy.rb', line 50 def method_missing(, *args, &block) subject.send(, *args, &block) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Make connection and path_prefix readable
16 17 18 |
# File 'lib/ghee/resource_proxy.rb', line 16 def connection @connection end |
#current_page ⇒ Object (readonly)
Expose pagination data
19 20 21 |
# File 'lib/ghee/resource_proxy.rb', line 19 def current_page @current_page end |
#id ⇒ Object (readonly)
Make connection and path_prefix readable
16 17 18 |
# File 'lib/ghee/resource_proxy.rb', line 16 def id @id end |
#pagination ⇒ Object (readonly)
Expose pagination data
19 20 21 |
# File 'lib/ghee/resource_proxy.rb', line 19 def pagination @pagination end |
#params ⇒ Object (readonly)
Make connection and path_prefix readable
16 17 18 |
# File 'lib/ghee/resource_proxy.rb', line 16 def params @params end |
#path_prefix ⇒ Object (readonly)
Make connection and path_prefix readable
16 17 18 |
# File 'lib/ghee/resource_proxy.rb', line 16 def path_prefix @path_prefix end |
#total ⇒ Object (readonly)
Expose pagination data
19 20 21 |
# File 'lib/ghee/resource_proxy.rb', line 19 def total @total end |
Class Method Details
.accept_header(header) ⇒ Object
21 22 23 24 25 |
# File 'lib/ghee/resource_proxy.rb', line 21 def self.accept_header(header) define_method "accept_type" do return @_accept_type ||= "#{header}" end end |
Instance Method Details
#all(&block) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/ghee/resource_proxy.rb', line 104 def all(&block) return self if pagination && next_page.nil? self.paginate :per_page => 100, :page => next_page || 1, &block self.all(&block) end |
#all_parallel ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ghee/resource_proxy.rb', line 112 def all_parallel connection = @connection.parallel_connection headers = connection.head(path_prefix) do |req| req.params.merge! params.merge(:per_page => 100) end pages = pagination_data headers.headers.delete("link") requests = [] connection.in_parallel do pages[:pages].to_i.times do |i| requests << connection.get(path_prefix) do |req| req.params.merge! params.merge(:per_page => 100, :page => i + 1) end end end requests.inject([]) do |results, page| results.concat(page.body) end end |
#build_prefix(first_argument, endpoint) ⇒ Object
138 139 140 141 |
# File 'lib/ghee/resource_proxy.rb', line 138 def build_prefix(first_argument, endpoint) (!first_argument.is_a?(Hash) && !first_argument.nil?) ? File.join(path_prefix, "/#{endpoint}/#{first_argument}") : File.join(path_prefix, "/#{endpoint}") end |
#paginate(options, &block) ⇒ 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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ghee/resource_proxy.rb', line 84 def paginate(, &block) @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 block.call(req) if block end if @subject.nil? @subject = response.body else @subject = @subject.concat response.body end parse_link_header response.headers.delete("link") return self end |
#raw ⇒ Object
Raw is the raw response from the faraday Use this if you need access to status codes or header values
58 59 60 |
# File 'lib/ghee/resource_proxy.rb', line 58 def raw connection.get(path_prefix){|req| req.params.merge!params } end |
#subject ⇒ Object
Subject is the response body parsed as json
Returns json
67 68 69 70 71 72 73 |
# File 'lib/ghee/resource_proxy.rb', line 67 def subject @subject ||= connection.get(path_prefix) do |req| req.params.merge!params req.headers["Accept"] = accept_type if self.respond_to? :accept_type @block.call(req)if @block end.body end |