Class: Ghee::ResourceProxy

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CUD

#create, #destroy, #patch

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

Instance Attribute Details

#connectionObject (readonly)

Make connection and path_prefix readable



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

def connection
  @connection
end

#current_pageObject (readonly)

Expose pagination data



19
20
21
# File 'lib/ghee/resource_proxy.rb', line 19

def current_page
  @current_page
end

#idObject (readonly)

Make connection and path_prefix readable



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

def id
  @id
end

#paginationObject (readonly)

Expose pagination data



19
20
21
# File 'lib/ghee/resource_proxy.rb', line 19

def pagination
  @pagination
end

#paramsObject (readonly)

Make connection and path_prefix readable



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

def params
  @params
end

#path_prefixObject (readonly)

Make connection and path_prefix readable



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

def path_prefix
  @path_prefix
end

#totalObject (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_parallelObject



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(options, &block)
  @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
    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

#rawObject

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

#subjectObject

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