activegist 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ module ActiveGist::ClassMethods
2
+ def create!(options = {})
3
+ new(options).tap { |gist| gist.save! }
4
+ end
5
+
6
+ def create(options = {})
7
+ new(options).tap { |gist| gist.save }
8
+ end
9
+
10
+ def first(type = :all)
11
+ all(type).first
12
+ end
13
+
14
+ def last(type = :all)
15
+ all(type).last
16
+ end
17
+
18
+ def count(type = :all)
19
+ all(type).count
20
+ end
21
+
22
+ def find(id)
23
+ load JSON.parse(api[id].get(:accept => 'application/json'))
24
+ end
25
+
26
+ def load(hash)
27
+ new(hash).tap do |instance|
28
+ instance.changed_attributes.clear
29
+ end
30
+ end
31
+
32
+ def all(type = :all)
33
+ case type
34
+ when :all then JSON.parse api.get(:accept => 'application/json')
35
+ when :public then JSON.parse api['public'].get(:accept => 'application/json')
36
+ when :starred then JSON.parse api['starred'].get(:accept => 'application/json')
37
+ else raise ArgumentError, "Unknown type: #{type.inspect} (expected one of [:all, :public, :starred])"
38
+ end.collect do |hash|
39
+ load hash
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveGist::Errors
2
+ # Raised when a gist raises validation errors during a call to ActiveGist#save!
3
+ class Invalid < StandardError
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ class ActiveGist::Files
2
+ delegate :key?, :has_key?, :[], :[]=, :empty?, :as_json, :to_json, :as_xml, :to_xml, :inspect, :to => :hash
3
+
4
+ def initialize(hash = {})
5
+ @hash = hash
6
+ @changed = false
7
+ end
8
+
9
+ def changed?
10
+ @hash_copy != hash
11
+ end
12
+
13
+ def replace_with(hash)
14
+ return hash if hash == @hash
15
+ @changed = true
16
+ @hash_copy = deep_dup hash
17
+ @hash = hash
18
+ end
19
+
20
+ private
21
+ def hash
22
+ @hash
23
+ end
24
+
25
+ def deep_dup(obj)
26
+ if obj.kind_of?(Array)
27
+ obj.collect { |a| deep_dup a }
28
+ elsif obj.kind_of?(Hash)
29
+ obj.inject(HashWithIndifferentAccess.new) { |h,(k,v)| h[deep_dup(k)] = deep_dup(v); h }
30
+ elsif obj.respond_to?(:dup)
31
+ begin
32
+ obj.dup
33
+ rescue TypeError
34
+ obj
35
+ end
36
+ else
37
+ obj
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ class ActiveGist
2
+ module Version
3
+ MAJOR, MINOR, TINY = 0, 6, 0
4
+ STRING = [MAJOR, MINOR, TINY].join '.'
5
+ end
6
+
7
+ VERSION = ActiveGist::Version::STRING
8
+ end
data/lib/activegist.rb ADDED
@@ -0,0 +1 @@
1
+ require File.expand_path('active_gist', File.dirname(__FILE__))
@@ -0,0 +1,87 @@
1
+ HTTP/1.1 200 OK
2
+ Link: <https://api.github.com/resource?page=2>; rel="next",
3
+ <https://api.github.com/resource?page=5>; rel="last"
4
+ X-RateLimit-Limit: 5000
5
+ X-RateLimit-Remaining: 4999
6
+ Content-type: application/json; charset=utf-8
7
+
8
+ [
9
+ {
10
+ "url": "https://api.github.com/gists/1",
11
+ "id": "1",
12
+ "description": "description of gist",
13
+ "public": true,
14
+ "user": {
15
+ "login": "octocat",
16
+ "id": 1,
17
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
18
+ "gravatar_id": "somehexcode",
19
+ "url": "https://api.github.com/users/octocat"
20
+ },
21
+ "files": {
22
+ "ring.erl": {
23
+ "size": 932,
24
+ "filename": "ring.erl",
25
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
26
+ "content": "contents of gist"
27
+ }
28
+ },
29
+ "comments": 0,
30
+ "html_url": "https://gist.github.com/1",
31
+ "git_pull_url": "git://gist.github.com/1.git",
32
+ "git_push_url": "git@gist.github.com:1.git",
33
+ "created_at": "2010-04-14T02:15:15Z"
34
+ },
35
+ {
36
+ "url": "https://api.github.com/gists/2",
37
+ "id": "2",
38
+ "description": "description of gist",
39
+ "public": true,
40
+ "user": {
41
+ "login": "octocat",
42
+ "id": 1,
43
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
44
+ "gravatar_id": "somehexcode",
45
+ "url": "https://api.github.com/users/octocat"
46
+ },
47
+ "files": {
48
+ "ring.erl": {
49
+ "size": 932,
50
+ "filename": "ring.erl",
51
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
52
+ "content": "contents of gist"
53
+ }
54
+ },
55
+ "comments": 0,
56
+ "html_url": "https://gist.github.com/2",
57
+ "git_pull_url": "git://gist.github.com/2.git",
58
+ "git_push_url": "git@gist.github.com:2.git",
59
+ "created_at": "2010-04-14T02:15:15Z"
60
+ },
61
+ {
62
+ "url": "https://api.github.com/gists/3",
63
+ "id": "3",
64
+ "description": "description of gist",
65
+ "public": false,
66
+ "user": {
67
+ "login": "octocat",
68
+ "id": 1,
69
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
70
+ "gravatar_id": "somehexcode",
71
+ "url": "https://api.github.com/users/octocat"
72
+ },
73
+ "files": {
74
+ "ring.erl": {
75
+ "size": 932,
76
+ "filename": "ring.erl",
77
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
78
+ "content": "contents of gist"
79
+ }
80
+ },
81
+ "comments": 0,
82
+ "html_url": "https://gist.github.com/3",
83
+ "git_pull_url": "git://gist.github.com/3.git",
84
+ "git_push_url": "git@gist.github.com:3.git",
85
+ "created_at": "2010-04-14T02:15:15Z"
86
+ }
87
+ ]
@@ -0,0 +1,78 @@
1
+ HTTP/1.1 200 OK
2
+ Link: <https://api.github.com/resource?page=2>; rel="next",
3
+ <https://api.github.com/resource?page=5>; rel="last"
4
+ X-RateLimit-Limit: 5000
5
+ X-RateLimit-Remaining: 4999
6
+ Content-type: application/json; charset=utf-8
7
+
8
+ {
9
+ "url": "https://api.github.com/gists/1",
10
+ "id": "1",
11
+ "description": "description of gist",
12
+ "public": true,
13
+ "user": {
14
+ "login": "octocat",
15
+ "id": 1,
16
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
17
+ "gravatar_id": "somehexcode",
18
+ "url": "https://api.github.com/users/octocat"
19
+ },
20
+ "files": {
21
+ "file1.txt": {
22
+ "size": 932,
23
+ "filename": "file1.txt",
24
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/file1.txt",
25
+ "content": "to be updated"
26
+ },
27
+ "old_name.txt": {
28
+ "size": 932,
29
+ "filename": "old_name.txt",
30
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/old_name.txt",
31
+ "content": "to be renamed"
32
+ },
33
+ "deleted.txt": {
34
+ "size": 932,
35
+ "filename": "deleted.txt",
36
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/deleted.txt",
37
+ "content": "to be deleted"
38
+ }
39
+ },
40
+ "comments": 0,
41
+ "html_url": "https://gist.github.com/2",
42
+ "git_pull_url": "git://gist.github.com/2.git",
43
+ "git_push_url": "git@gist.github.com:2.git",
44
+ "created_at": "2010-04-14T02:15:15Z",
45
+ "updated_at": "2010-04-14T02:15:15Z",
46
+ "forks": [
47
+ {
48
+ "user": {
49
+ "login": "octocat",
50
+ "id": 1,
51
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
52
+ "gravatar_id": "somehexcode",
53
+ "url": "https://api.github.com/users/octocat"
54
+ },
55
+ "url": "https://api.github.com/gists/5",
56
+ "created_at": "2011-04-14T16:00:49Z"
57
+ }
58
+ ],
59
+ "history": [
60
+ {
61
+ "url": "https://api.github.com/gists/1/57a7f021a713b1c5a6a199b54cc514735d2d462f",
62
+ "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
63
+ "user": {
64
+ "login": "octocat",
65
+ "id": 1,
66
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
67
+ "gravatar_id": "somehexcode",
68
+ "url": "https://api.github.com/users/octocat"
69
+ },
70
+ "change_status": {
71
+ "deletions": 0,
72
+ "additions": 180,
73
+ "total": 180
74
+ },
75
+ "committed_at": "2010-04-14T02:15:15Z"
76
+ }
77
+ ]
78
+ }
@@ -0,0 +1,4 @@
1
+ HTTP/1.1 204 No Content
2
+ X-RateLimit-Limit: 5000
3
+ X-RateLimit-Remaining: 4999
4
+
@@ -0,0 +1,71 @@
1
+ HTTP/1.1 200 OK
2
+ Link: <https://api.github.com/resource?page=2>; rel="next",
3
+ <https://api.github.com/resource?page=5>; rel="last"
4
+ X-RateLimit-Limit: 5000
5
+ X-RateLimit-Remaining: 4999
6
+ Content-type: application/json; charset=utf-8
7
+
8
+ {
9
+ "url": "https://api.github.com/gists/1",
10
+ "id": "1",
11
+ "description": "returned updated description",
12
+ "public": true,
13
+ "user": {
14
+ "login": "octocat",
15
+ "id": 1,
16
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
17
+ "gravatar_id": "somehexcode",
18
+ "url": "https://api.github.com/users/octocat"
19
+ },
20
+ "files": {
21
+ "file1.txt": {
22
+ "size": 932,
23
+ "filename": "file1.txt",
24
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/file1.txt",
25
+ "content": "returned updated file contents"
26
+ },
27
+ "new_name.txt": {
28
+ "size": 932,
29
+ "filename": "new_name.txt",
30
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/old_name.txt",
31
+ "content": "to be renamed"
32
+ }
33
+ },
34
+ "comments": 0,
35
+ "html_url": "https://gist.github.com/2",
36
+ "git_pull_url": "git://gist.github.com/2.git",
37
+ "git_push_url": "git@gist.github.com:2.git",
38
+ "created_at": "2010-04-14T02:15:15Z",
39
+ "forks": [
40
+ {
41
+ "user": {
42
+ "login": "octocat",
43
+ "id": 1,
44
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
45
+ "gravatar_id": "somehexcode",
46
+ "url": "https://api.github.com/users/octocat"
47
+ },
48
+ "url": "https://api.github.com/gists/5",
49
+ "created_at": "2011-04-14T16:00:49Z"
50
+ }
51
+ ],
52
+ "history": [
53
+ {
54
+ "url": "https://api.github.com/gists/1/57a7f021a713b1c5a6a199b54cc514735d2d462f",
55
+ "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
56
+ "user": {
57
+ "login": "octocat",
58
+ "id": 1,
59
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
60
+ "gravatar_id": "somehexcode",
61
+ "url": "https://api.github.com/users/octocat"
62
+ },
63
+ "change_status": {
64
+ "deletions": 0,
65
+ "additions": 180,
66
+ "total": 180
67
+ },
68
+ "committed_at": "2010-04-14T02:15:15Z"
69
+ }
70
+ ]
71
+ }
@@ -0,0 +1,65 @@
1
+ HTTP/1.1 200 OK
2
+ Link: <https://api.github.com/resource?page=2>; rel="next",
3
+ <https://api.github.com/resource?page=5>; rel="last"
4
+ X-RateLimit-Limit: 5000
5
+ X-RateLimit-Remaining: 4999
6
+ Content-type: application/json; charset=utf-8
7
+
8
+ {
9
+ "url": "https://api.github.com/gists/2",
10
+ "id": "2",
11
+ "description": "description of gist",
12
+ "public": true,
13
+ "user": {
14
+ "login": "octocat",
15
+ "id": 1,
16
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
17
+ "gravatar_id": "somehexcode",
18
+ "url": "https://api.github.com/users/octocat"
19
+ },
20
+ "files": {
21
+ "ring.erl": {
22
+ "size": 932,
23
+ "filename": "ring.erl",
24
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
25
+ "content": "contents of gist"
26
+ }
27
+ },
28
+ "comments": 0,
29
+ "html_url": "https://gist.github.com/2",
30
+ "git_pull_url": "git://gist.github.com/2.git",
31
+ "git_push_url": "git@gist.github.com:2.git",
32
+ "created_at": "2010-04-14T02:15:15Z",
33
+ "forks": [
34
+ {
35
+ "user": {
36
+ "login": "octocat",
37
+ "id": 1,
38
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
39
+ "gravatar_id": "somehexcode",
40
+ "url": "https://api.github.com/users/octocat"
41
+ },
42
+ "url": "https://api.github.com/gists/5",
43
+ "created_at": "2011-04-14T16:00:49Z"
44
+ }
45
+ ],
46
+ "history": [
47
+ {
48
+ "url": "https://api.github.com/gists/1/57a7f021a713b1c5a6a199b54cc514735d2d462f",
49
+ "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
50
+ "user": {
51
+ "login": "octocat",
52
+ "id": 1,
53
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
54
+ "gravatar_id": "somehexcode",
55
+ "url": "https://api.github.com/users/octocat"
56
+ },
57
+ "change_status": {
58
+ "deletions": 0,
59
+ "additions": 180,
60
+ "total": 180
61
+ },
62
+ "committed_at": "2010-04-14T02:15:15Z"
63
+ }
64
+ ]
65
+ }
@@ -0,0 +1,4 @@
1
+ HTTP/1.1 404 Not Found
2
+ X-RateLimit-Limit: 5000
3
+ X-RateLimit-Remaining: 4999
4
+
@@ -0,0 +1,4 @@
1
+ HTTP/1.1 204 No Content
2
+ X-RateLimit-Limit: 5000
3
+ X-RateLimit-Remaining: 4999
4
+
@@ -0,0 +1,4 @@
1
+ HTTP/1.1 204 No Content
2
+ X-RateLimit-Limit: 5000
3
+ X-RateLimit-Remaining: 4999
4
+
@@ -0,0 +1,33 @@
1
+ HTTP/1.1 200 OK
2
+ Link: <https://api.github.com/resource?page=2>; rel="next",
3
+ <https://api.github.com/resource?page=5>; rel="last"
4
+ X-RateLimit-Limit: 5000
5
+ X-RateLimit-Remaining: 4999
6
+ Content-type: application/json; charset=utf-8
7
+
8
+ {
9
+ "url": "https://api.github.com/gists/100",
10
+ "id": "100",
11
+ "description": "description of gist",
12
+ "public": true,
13
+ "user": {
14
+ "login": "octocat",
15
+ "id": 1,
16
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
17
+ "gravatar_id": "somehexcode",
18
+ "url": "https://api.github.com/users/octocat"
19
+ },
20
+ "files": {
21
+ "ring.erl": {
22
+ "size": 932,
23
+ "filename": "ring.erl",
24
+ "raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
25
+ "content": "contents of gist"
26
+ }
27
+ },
28
+ "comments": 0,
29
+ "html_url": "https://gist.github.com/100",
30
+ "git_pull_url": "git://gist.github.com/100.git",
31
+ "git_push_url": "git@gist.github.com:100.git",
32
+ "created_at": "2010-04-14T02:15:15Z"
33
+ }