capacity 0.0.1 → 0.0.2

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.tar.gz.sig CHANGED
Binary file
data/Manifest CHANGED
@@ -1,3 +1,5 @@
1
+ Manifest
1
2
  Rakefile
2
3
  lib/capacity.rb
3
- Manifest
4
+ lib/capacity/api.rb
5
+ lib/capacity/response.rb
data/Rakefile CHANGED
@@ -2,8 +2,9 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('capacity', '0.0.1') do |p|
6
- p.description = 'A handy class for interacting with the Capacity API'
5
+ Echoe.new('capacity', '0.0.2') do |p|
6
+ p.summary = "Driver for the Capacity API"
7
+ p.description = 'Not even alpha, not for production use, and not much use to anyone but us at the moment!'
7
8
  p.url = 'http://getcapacity.com/'
8
9
  p.author = 'Peat Bakke'
9
10
  p.email = 'peat@getcapacity.com'
data/capacity.gemspec CHANGED
@@ -2,23 +2,23 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{capacity}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Peat Bakke"]
9
9
  s.cert_chain = ["/Users/peat/.ssh/gem-public_cert.pem"]
10
10
  s.date = %q{2010-10-20}
11
- s.description = %q{A handy class for interacting with the Capacity API}
11
+ s.description = %q{Not even alpha, not for production use, and not much use to anyone but us at the moment!}
12
12
  s.email = %q{peat@getcapacity.com}
13
- s.extra_rdoc_files = ["lib/capacity.rb"]
14
- s.files = ["Rakefile", "lib/capacity.rb", "Manifest", "capacity.gemspec"]
13
+ s.extra_rdoc_files = ["lib/capacity.rb", "lib/capacity/api.rb", "lib/capacity/response.rb"]
14
+ s.files = ["Manifest", "Rakefile", "lib/capacity.rb", "lib/capacity/api.rb", "lib/capacity/response.rb", "capacity.gemspec"]
15
15
  s.homepage = %q{http://getcapacity.com/}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Capacity"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{capacity}
19
19
  s.rubygems_version = %q{1.3.7}
20
20
  s.signing_key = %q{/Users/peat/.ssh/gem-private_key.pem}
21
- s.summary = %q{A handy class for interacting with the Capacity API}
21
+ s.summary = %q{Driver for the Capacity API}
22
22
 
23
23
  if s.respond_to? :specification_version then
24
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
data/lib/capacity.rb CHANGED
@@ -1,75 +1,18 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+
3
+ # general requirements
1
4
  require 'rubygems'
2
5
  require 'uri'
3
6
  require 'net/http'
4
7
  require 'json'
5
8
 
6
- # talks with the Capcity API
7
- class Capacity
9
+ # local requirements
10
+ require 'capacity/response'
11
+ require 'capacity/api'
12
+
13
+ module Capacity
8
14
 
9
15
  SERVER = 'http://localhost:8080/'
10
16
  VERSION = 'v1'
11
-
12
- @@token = nil
13
-
14
- def self.token=( token )
15
- @@token = token
16
- end
17
-
18
- def self.get( resource, query = {} )
19
- request( :get, resource, query )
20
- end
21
-
22
- def self.post( resource, query = {} )
23
- request( :post, resource, query )
24
- end
25
-
26
- def self.put( resource, query = {} )
27
- request( :put, resource, query )
28
- end
29
-
30
- def self.delete( resource, query = {} )
31
- request( :delete, resource, query )
32
- end
33
-
34
- protected
35
-
36
- def self.request( http_method, resource, query = {} )
37
- query.merge!( { :token => @@token } ) if @@token
38
-
39
- url = URI.parse("#{SERVER}#{VERSION}/#{resource}")
40
-
41
- # hack for query parameter in GETs
42
- if http_method == :get
43
- if query.empty?
44
- get_path = url.path
45
- else
46
- query_array = []
47
- query.each do |k,v|
48
- if v.is_a?(Hash) # sub-key/value pairs, eg: query[foo]=abcdef query[bar]=zed
49
- v.each do |subKey, subVal|
50
- query_array << URI.escape( "#{k.to_s}[" + subKey.to_s + "]" ) + "=" + URI.escape( subVal.to_s )
51
- end
52
- else
53
- query_array << "#{URI.escape( k.to_s )}=#{URI.escape( v.to_s )}"
54
- end
55
- end
56
- get_path = url.path + "?" + query_array.join("&");
57
- end
58
- end
59
-
60
- req = case http_method
61
- when :get then Net::HTTP::Get.new( get_path )
62
- when :put then Net::HTTP::Put.new( url.path )
63
- when :post then Net::HTTP::Post.new( url.path )
64
- when :delete then Net::HTTP::Delete.new( url.path )
65
- end
66
-
67
- res = Net::HTTP.start( url.host, url.port ) { |http|
68
- req.set_form_data( query ) if http_method != :get
69
- http.request( req )
70
- }
71
-
72
- JSON.parse( res.body )
73
- end
74
17
 
75
18
  end
@@ -0,0 +1,72 @@
1
+ require 'rubygems'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ # talks with the Capcity API
7
+ module Capacity
8
+
9
+ class API
10
+
11
+ attr_accessor :token
12
+
13
+ def get( resource, query = {} )
14
+ request( :get, resource, query )
15
+ end
16
+
17
+ def post( resource, query = {} )
18
+ request( :post, resource, query )
19
+ end
20
+
21
+ def put( resource, query = {} )
22
+ request( :put, resource, query )
23
+ end
24
+
25
+ def delete( resource, query = {} )
26
+ request( :delete, resource, query )
27
+ end
28
+
29
+ protected
30
+
31
+ def request( http_method, resource, query = {} )
32
+ query.merge!( { :token => token } ) if token
33
+
34
+ url = URI.parse("#{Capacity::SERVER}#{Capacity::VERSION}/#{resource}")
35
+
36
+ # hack for query parameter in GETs
37
+ if http_method == :get
38
+ if query.empty?
39
+ get_path = url.path
40
+ else
41
+ query_array = []
42
+ query.each do |k,v|
43
+ if v.is_a?(Hash) # sub-key/value pairs, eg: query[foo]=abcdef query[bar]=zed
44
+ v.each do |subKey, subVal|
45
+ query_array << URI.escape( "#{k.to_s}[" + subKey.to_s + "]" ) + "=" + URI.escape( subVal.to_s )
46
+ end
47
+ else
48
+ query_array << "#{URI.escape( k.to_s )}=#{URI.escape( v.to_s )}"
49
+ end
50
+ end
51
+ get_path = url.path + "?" + query_array.join("&");
52
+ end
53
+ end
54
+
55
+ req = case http_method
56
+ when :get then Net::HTTP::Get.new( get_path )
57
+ when :put then Net::HTTP::Put.new( url.path )
58
+ when :post then Net::HTTP::Post.new( url.path )
59
+ when :delete then Net::HTTP::Delete.new( url.path )
60
+ end
61
+
62
+ res = Net::HTTP.start( url.host, url.port ) { |http|
63
+ req.set_form_data( query ) if http_method != :get
64
+ http.request( req )
65
+ }
66
+
67
+ Response.new( JSON.parse( res.body ) )
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,50 @@
1
+ module Capacity
2
+ class Response
3
+
4
+ include Enumerable
5
+
6
+ # Expects 'resp' to be a hash, created from the API's JSON response
7
+ def initialize( resp )
8
+ # setup the local key/value store
9
+ @data = {}
10
+
11
+ if resp['status'] != 'OK'
12
+ # see if there's an error
13
+ @error = resp['error']
14
+ else
15
+ @error = nil
16
+ # find the embedded hash that will contain the response data
17
+ resp.each do |k,v|
18
+ if v.is_a?( Hash ) # there it is!
19
+ v.each { |kp,vp| @data[kp.to_sym] = vp } # symbolize keys
20
+ break # stop searching
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # returns boolean for whether or not there was an error
27
+ def error?
28
+ !@error.to_s.empty?
29
+ end
30
+
31
+ # returns the error message
32
+ def error
33
+ @error
34
+ end
35
+
36
+ # satisfies Enumerable requirements
37
+ def each
38
+ @data.each do |k,v|
39
+ yield k,v
40
+ end
41
+ end
42
+
43
+ # provides string/symbol agnostic, hash-like accessor
44
+ def []( key )
45
+ @data[ key.to_sym ]
46
+ end
47
+
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Peat Bakke
@@ -39,7 +39,7 @@ date: 2010-10-20 00:00:00 -07:00
39
39
  default_executable:
40
40
  dependencies: []
41
41
 
42
- description: A handy class for interacting with the Capacity API
42
+ description: Not even alpha, not for production use, and not much use to anyone but us at the moment!
43
43
  email: peat@getcapacity.com
44
44
  executables: []
45
45
 
@@ -47,10 +47,14 @@ extensions: []
47
47
 
48
48
  extra_rdoc_files:
49
49
  - lib/capacity.rb
50
+ - lib/capacity/api.rb
51
+ - lib/capacity/response.rb
50
52
  files:
53
+ - Manifest
51
54
  - Rakefile
52
55
  - lib/capacity.rb
53
- - Manifest
56
+ - lib/capacity/api.rb
57
+ - lib/capacity/response.rb
54
58
  - capacity.gemspec
55
59
  has_rdoc: true
56
60
  homepage: http://getcapacity.com/
@@ -87,6 +91,6 @@ rubyforge_project: capacity
87
91
  rubygems_version: 1.3.7
88
92
  signing_key:
89
93
  specification_version: 3
90
- summary: A handy class for interacting with the Capacity API
94
+ summary: Driver for the Capacity API
91
95
  test_files: []
92
96
 
metadata.gz.sig CHANGED
Binary file