-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsucker.rb
39 lines (33 loc) · 1016 Bytes
/
sucker.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
################################## LIB ########################################
require 'json'
class Resource
attr_accessor :path
def initialize(path)
@path = path
end
def raw
`curl -s #{@path}`
end
def obj
JSON.restore(raw) if raw.chomp!
end
def collection # Specify a collection URI, such as http://example.com/resources/
Resource.new(@path + '/')
end
def method_missing(method, *args, &block)
return obj.send(method, *args, &block) if Hash.method_defined?(method) || Array.method_defined?(method)
Resource.new(@path + '/' + method.to_s)
end
end
class Sucker
attr_accessor :base_endpoint
def initialize(base_endpoint)
@base_endpoint = base_endpoint
end
def method_missing(method, *args, &block)
Resource.new(@base_endpoint + method.to_s)
end
end
################################## DEMO #######################################
github = Sucker.new('https://api.github.com/');
puts github.repos.secreek.hackatus.commits[0]['commit']['author']