fcoury-octopi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Felipe Coury
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = octopi
2
+
3
+ Octopi is a Ruby interface to GitHub API v2 (http://developer.github.com). It's under early but active development and already works.
4
+
5
+ == Example
6
+
7
+ require 'octopi'
8
+
9
+ include Octopi
10
+
11
+ # user information
12
+ user = User.find("fcoury")
13
+ puts "#{user.name} is being followed by #{user.followers.join(", ")} and following #{user.following.join(", ")}"
14
+
15
+ # the bang version of followers and following
16
+ # fetches user object for each user, but is
17
+ # a lot more expensive
18
+ user.followers!.each do |u|
19
+ puts " - #{u.name} (#{u.login}) has #{u.public_repo_count} repo(s)"
20
+ end
21
+
22
+ # search user
23
+ users = User.find_all("silva")
24
+ puts "#{users.size} users found for 'silva':"
25
+ users.each do |u|
26
+ puts " - #{u.name}"
27
+ end
28
+
29
+ # repository information
30
+ repo = Repository.find("fcoury", "octopi")
31
+ puts "Repository: #{repo.name} - #{repo.description} (by #{repo.owner}) - #{repo.url}"
32
+
33
+ # repository search
34
+ repos = Repository.find_all("ruby", "git")
35
+ puts "#{repos.size} repository(ies) with 'ruby' and 'git':"
36
+ repos.each do |r|
37
+ puts " - #{r.name}"
38
+ end
39
+
40
+ # connect "user", "<<token>>" do |github|
41
+ # puts github.user.name
42
+ # end
43
+
44
+ == Author
45
+
46
+ Felipe Coury - http://felipecoury.com
47
+ HasMany.info blog - http://hasmany.info
48
+
49
+ == Copyright
50
+
51
+ DISCLAIMER: The name of this library is pronounced _octo-pie_ but no Octocats were harmed during its creation. It's not really an Octo Pie, but a contraction of the words Octocat and API.
52
+
53
+ Copyright (c) 2009 Felipe Coury. See LICENSE for details.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 1
4
+ :major: 0
data/lib/octopi.rb ADDED
@@ -0,0 +1,191 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'pp'
4
+
5
+ module Octopi
6
+ class Api; end
7
+ ANONYMOUS_API = Api.new
8
+
9
+ def connect(login, token, &block)
10
+ yield Api.new(login, token)
11
+ end
12
+
13
+ class Api
14
+ include HTTParty
15
+ base_uri "http://github.com/api/v2"
16
+
17
+ attr_accessor :format
18
+
19
+ def initialize(login = nil, token = nil, format = "xml")
20
+ self.class.default_params(:login => login, :token => token) if login
21
+ @format = format
22
+ end
23
+
24
+ %w[keys emails].each do |action|
25
+ define_method("#{action}") do
26
+ get("/user/#{action}")
27
+ end
28
+ end
29
+
30
+ def user
31
+ user_data = get("/user/show/#{self.class.default_params[:login]}")
32
+ raise "Unexpected response for user command" unless user_data and user_data['user']
33
+ User.new(self, user_data['user'])
34
+ end
35
+
36
+ def save(resource_path, data)
37
+ traslate resource_path, data
38
+ #still can't figure out on what format values are expected
39
+ post("#{resource_path}", { :query => data })
40
+ end
41
+
42
+ def find(path, result_key, resource_id)
43
+ get(path, { :id => resource_id })
44
+ end
45
+
46
+ def find_all(path, result_key, query)
47
+ get(path, { :query => query })[result_key]
48
+ end
49
+
50
+ private
51
+ def get(path, params = {}, format = "yaml")
52
+ params.each_pair do |k,v|
53
+ path = path.gsub(":#{k.to_s}", v)
54
+ end
55
+ # puts "GET: /#{format}#{path}"
56
+ self.class.get("/#{format}#{path}")
57
+ end
58
+ end
59
+
60
+ class Base
61
+ def initialize(api, hash)
62
+ @api = api
63
+ @keys = []
64
+
65
+ raise "Missing data for #{@resource}" unless hash
66
+
67
+ hash.each_pair do |k,v|
68
+ @keys << k
69
+ instance_variable_set("@#{k}", v)
70
+
71
+ self.class.send :define_method, "#{k}=" do |v|
72
+ instance_variable_set("@#{k}", v)
73
+ end
74
+
75
+ self.class.send :define_method, k do
76
+ instance_variable_get("@#{k}")
77
+ end
78
+ end
79
+ end
80
+
81
+ def property(p, v)
82
+ path = "#{self.class.path_for(:resource)}/#{p}"
83
+ @api.find(path, self.class.resource_name(:singular), v)
84
+ end
85
+
86
+ def save
87
+ hash = {}
88
+ @keys.each { |k| hash[k] = send(k) }
89
+ @api.save(self.path_for(:resource), hash)
90
+ end
91
+ end
92
+
93
+ module Resource
94
+ def self.included(base)
95
+ base.extend ClassMethods
96
+ base.set_resource_name(base.name)
97
+ (@@resources||={})[base.resource_name(:singular)] = base
98
+ (@@resources||={})[base.resource_name(:plural)] = base
99
+ end
100
+
101
+ def self.for(name)
102
+ @@resources[name]
103
+ end
104
+
105
+ module ClassMethods
106
+ def set_resource_name(singular, plural = "#{singular}s")
107
+ @resource_name = {:singular => declassify(singular), :plural => declassify(plural)}
108
+ end
109
+
110
+ def resource_name(key)
111
+ @resource_name[key]
112
+ end
113
+
114
+ def declassify(s)
115
+ (s.split('::').last || '').downcase if s
116
+ end
117
+
118
+ def find_path(path)
119
+ (@path_spec||={})[:find] = path
120
+ end
121
+
122
+ def resource_path(path)
123
+ (@path_spec||={})[:resource] = path
124
+ end
125
+
126
+ def find(s)
127
+ result = ANONYMOUS_API.find(path_for(:resource), @resource_name[:singular], s)
128
+ key = result.keys.first
129
+ Resource.for(key).new(ANONYMOUS_API, result[key])
130
+ end
131
+
132
+ def find_all(s)
133
+ all = []
134
+ result = ANONYMOUS_API.find_all(path_for(:find), @resource_name[:plural], s)
135
+ result.each do |item|
136
+ all << new(ANONYMOUS_API, item)
137
+ end
138
+ all
139
+ end
140
+
141
+ def path_for(type)
142
+ @path_spec[type]
143
+ end
144
+ end
145
+ end
146
+
147
+ class User < Base
148
+ include Resource
149
+
150
+ find_path "/user/search/:query"
151
+ resource_path "/user/show/:id"
152
+
153
+ def user_property(property, deep)
154
+ users = []
155
+ property(property, login).each_pair do |k,v|
156
+ return v unless deep
157
+
158
+ v.each { |u| users << User.find(u) }
159
+ end
160
+
161
+ users
162
+ end
163
+
164
+ # takes one param, deep that indicates if returns
165
+ # only the user login or an user object
166
+ %w[followers following].each do |method|
167
+ define_method(method) do
168
+ user_property(method, false)
169
+ end
170
+ define_method("#{method}!") do
171
+ user_property(method, true)
172
+ end
173
+ end
174
+ end
175
+
176
+ class Repository < Base
177
+ include Resource
178
+ set_resource_name "repository", "repositories"
179
+
180
+ def self.find(user, name)
181
+ super "#{user}/#{name}"
182
+ end
183
+
184
+ def self.find_all(*args)
185
+ super args.join("+")
186
+ end
187
+
188
+ find_path "/repos/search/:query"
189
+ resource_path "/repos/show/:id"
190
+ end
191
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class OctopiTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'octopi'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fcoury-octopi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Coury
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: felipe.coury@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - README.rdoc
27
+ - VERSION.yml
28
+ - lib/octopi.rb
29
+ - test/octopi_test.rb
30
+ - test/test_helper.rb
31
+ - LICENSE
32
+ has_rdoc: true
33
+ homepage: http://github.com/fcoury/octopi
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A Ruby interface to GitHub API v2
59
+ test_files: []
60
+