Class: Ghee::ResourceProxy

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(message, *args, &block)
  subject.send(message, *args, &block)
end

Instance Attribute Details

#connectionObject (readonly)

Make connection and path_prefix readable



14
15
16
# File 'lib/ghee/resource_proxy.rb', line 14

def connection
  @connection
end

#current_pageObject (readonly)

Expose pagination data



17
18
19
# File 'lib/ghee/resource_proxy.rb', line 17

def current_page
  @current_page
end

#paginationObject (readonly)

Expose pagination data



17
18
19
# File 'lib/ghee/resource_proxy.rb', line 17

def pagination
  @pagination
end

#paramsObject (readonly)

Make connection and path_prefix readable



14
15
16
# File 'lib/ghee/resource_proxy.rb', line 14

def params
  @params
end

#path_prefixObject (readonly)

Make connection and path_prefix readable



14
15
16
# File 'lib/ghee/resource_proxy.rb', line 14

def path_prefix
  @path_prefix
end

#totalObject (readonly)

Expose pagination data



17
18
19
# File 'lib/ghee/resource_proxy.rb', line 17

def total
  @total
end

Instance Method Details

#allObject



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(options)
  @current_page = options.fetch(:page) {raise ArgumentError, ":page parameter required"}
  per_page = options.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

#subjectObject

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