-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrumy.rb
218 lines (187 loc) · 8.33 KB
/
scrumy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# **Scrumy Client** is a Ruby REST client wrapper for [Scrumy](http://apidoc.scrumy.com).
#
# Scrumy Client provides a simple client interface for retrieving Sprints, Stories,
# Tasks, and Scrumers, as well as some tools for generating useful information with
# those objects.
# The [source code](https://github.com/jeffremer/scrumy-client) is available on Github.
# Dependencies
# ============
# We need JSON
# JSON responses are parsed and turned into the core Scrumy objects, key value
# pairs in the JSON hashes become instance variables in the objects.
require 'json'
# Scrumy Client uses [rest-client](https://github.com/archiloque/rest-client)
# for conveniently retrieving REST resources and handling some of the HTTP at a
# higher level.
require 'rest_client'
# We use [ActiveSupport::Inflector](http://as.rubyonrails.org/classes/Inflector.html) to do
# some of the metaprogramming magic and instantiate classes and create methods dynamically.
# `Inflector` helps to pluralize, singularize, (de)modularize symbols.
require 'active_support/inflector'
module Scrumy
class Client
# Every client request sets the `@url` instance variable for easy debugging
# Just call client.url to see that last requested URL.
attr_reader :url
# `Scrumy::Clients` are initialized with a project name and password.
def initialize(project, password)
@project, @password = project, password
end
# This is the heart of the `Scrumy::Client` object. It provides Ghost Methods via the Ruby
# chainsaw, `method_missing`.
def method_missing(id, *args, &block)
# Figure out what kind of resource we're trying to get.
klass = Scrumy::Models.const_get(id.to_s.capitalize.singularize)
# Special case for handling id=:current - this really only applies to Sprint resources
# but if other resources specified the `current` sub-resources then it should still work.
if klass.current_url and args.first==:current
@url = format(klass.current_url, :current)
else
# TODO
# Figure out a better way of determining if the resource is singular or plural
# The only argument that resources ever take is an ID, so pass the first arg as the ID.
@url = format((id.to_s =~ /s$/ ? klass.list_url : klass.show_url), args.first)
end
# Here we request the resource using the singular of the resource name as the root
# to extract from the returned JSON hash.
response = get(@url, id.to_s.singularize)
# Responses are of two types, either arrays of hashes or a single hash
if response.kind_of? Array
# If it's array collect a new array by constructing objects based on the resource
# name capitalized and singularized.
response.collect do |obj|
klass.new(obj, self)
end
else
# Otherwise create a single new object of the correct type.
klass.new(response, self)
end
end
# TODO
# This grammar should be better
# Currently subresources specify special sybmols in their name,
# either :project to get @project off the client, or :id to get
# the argument passed to the client.
def format(url, id=nil)
url = url.gsub(':project', @project)
url = url.gsub(':id', id.to_s) if id
url
end
protected
# `#get` provides the nuts and bolts for retrieving resources. Give it a
# resource URL and a root key and it will return either an array of hashes
# at that root key or a single hash with values found at that key.
#
# For example if the resource returns `{"foo"=>{"id"=>1, "bar"=>"baz"}}`
# then `#get(some_url, "foo")` will return the value of `"foo"` from the hash:
# `{"id"=>1, "bar"=>"baz"}`. This is important because later on in the models
# we assign all the values in the latter hash as instance variables on the
# model objects.
def get(url, root)
begin
# Start by creating a new `RestCLient::Resource` authenticated with
# the `@project` name and `@password`.
resource = RestClient::Resource.new(url, @project, @password)
# `GET` the resource
resource.get {|response, request, result, &block|
case response.code
when 200
# and on success parse the response
json = JSON.parse(response.body)
# If it's `Array` then collect the hashes and flatten them on the `root` key.
if json.kind_of?(Array) && root
json.collect{|item|
item[root]
}
else
# Otherwise just return the `Hash` at the root or the JSON itself directly.
root ? json[root] : json
end
else
response.return!(request, result, &block)
end
}
rescue => e
# Rescue and reraise with the current `@url` for debugging purposes
raise "Problem fetching #{@url} because #{e.message}"
end
end
end
# This is the abstract `Scrumy::Model` class that all resource models inherit from.
module Models
class Model
attr_reader :id
# When passed a hash the constructor will initialize the object with instance variables
# named after the keys in the hash.
def initialize(args, client)
@client = client
args.each do |k,v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end
# This method missing provides a Ghost Method proxy to access or mutate any instance variable.
def method_missing(id, *args, &block)
if id.to_s =~ /=$/
id = id.to_s.gsub(/=$/,'')
instance_variable_set("@#{id}", args.first)
else
instance_variable_get("@#{id}")
end
end
# Adapter methods for the resource DSL, this provides the
# show, list, and current sub-resource defition methods.
class << self
# For each :show, :list, :current
[:show, :list, :current].each{|method|
# Create a method that sets a class instance variable to the URL argument
send :define_method, method do |url|
instance_variable_set "@#{method.to_s}_url", url
end
# And create an accessor for that URL.
send :define_method, "#{method.to_s}_url".to_sym do
instance_variable_get "@#{method.to_s}_url"
end
}
end
# Only current Sprints are complete, so other models need to know how ot lazily load their
# children.
# Specifying a lazy_load key in a subclass defines a new instance method on that class
# that uses the client to fetch the right resource and set the appropriate instance variable
# correctly.
def self.lazy_load(method)
define_method(method) {
client = instance_variable_get("@client")
ivar = instance_variable_get("@#{method}")
clss = Models.send :const_get, method.to_s.singularize.classify
root = method.to_s.singularize
# First check if the instance variable is already set, but perhaps incorrectly as a Hash
# If so, then instantiate the instance variable as the correct type.
if ivar.kind_of? Array
ivar.collect!{|single| clss.new(single[root], client)} if ivar and ivar.first.kind_of?Hash
elsif ivar
ivar = clss.new(ivar, client)
end
# Return if already set, sort of minimal caching.
return ivar if ivar
# Last resort, fetch from the rest client.
ivar = client.send(method, instance_variable_get("@id"))
}
end
def self.helper(name, &block)
self.send :define_method, name do
instance_eval(&block)
end
end
end
end
end
# This is entry point for the DSL that specifies resources
def resource(name, &block)
# It creates a new class based on the resource name scoped tot he Scrumy module
klass = Scrumy::Models.const_set(name.to_s.classify, Class.new(Scrumy::Models::Model))
# Then executes the block on the class. The class provides several class
# methods for making instances behave correctly.
klass.class_exec &block
end
# Loads in the default resources, see `resources.rb`
load('resources.rb')