cocupu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.sw[opn]
2
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cocupu (0.0.1)
5
+ httmultiparty (= 0.3.8)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ fakeweb (1.3.0)
11
+ httmultiparty (0.3.8)
12
+ httparty (>= 0.7.3)
13
+ multipart-post
14
+ httparty (0.9.0)
15
+ multi_json (~> 1.0)
16
+ multi_xml
17
+ multi_json (1.3.7)
18
+ multi_xml (0.5.1)
19
+ multipart-post (1.1.5)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ cocupu!
26
+ fakeweb (= 1.3.0)
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ client
2
+ ======
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/cocupu.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ version = File.read(File.expand_path("../VERSION",__FILE__)).strip
2
+
3
+ Gem::Specification.new do |s|
4
+ s.platform = Gem::Platform::RUBY
5
+ s.name = 'cocupu'
6
+ s.version = version
7
+ s.summary = 'A client for the Cocupu server'
8
+ s.description = 'Cocupu is a platform for collecting, curating and publishing your data. This client allows you to interface with that data.'
9
+
10
+ s.required_ruby_version = '>= 1.9.3'
11
+ s.required_rubygems_version = ">= 1.8.11"
12
+
13
+ s.author = 'Justin Coyne'
14
+ s.email = 'justin@cocupu.com'
15
+ s.homepage = 'http://cocupu.com'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('httmultiparty', '0.3.8')
23
+ s.add_development_dependency('fakeweb', '1.3.0')
24
+ end
data/lib/cocupu.rb ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'httmultiparty'
4
+ require 'cocupu/identity'
5
+ require 'cocupu/model'
6
+ require 'cocupu/node'
7
+ require 'cocupu/file'
8
+
9
+ module Cocupu
10
+ class Connection
11
+ include HTTMultiParty
12
+ attr_accessor :token, :host, :port
13
+
14
+ def initialize(email, password, port=80, host='localhost')
15
+ self.host = host
16
+ self.port = port
17
+ response = self.class.post("http://#{host}:#{port}/api/v1/tokens", body: {email: email, password: password})
18
+ raise "Error logging in: #{response}" unless response.code == 200
19
+ self.token = response["token"]
20
+ end
21
+
22
+ def get(path)
23
+ self.class.get(request_url(path))
24
+ end
25
+
26
+ def put(path, args={})
27
+ self.class.put(request_url(path), args)
28
+ end
29
+
30
+ def post(path, args={})
31
+ self.class.post(request_url(path), args)
32
+ end
33
+
34
+ def request_url(path)
35
+ "http://#{host}:#{port}#{path}?auth_token=#{token}"
36
+ end
37
+
38
+ def identities
39
+ return @identities if @identities
40
+ response = self.class.get("http://#{host}:#{port}/identities?auth_token=#{token}")
41
+ raise "Error getting identities: #{response}" unless response.code == 200
42
+ @identities = response.map {|val| Identity.new(val, self)}
43
+ end
44
+
45
+ def identity(short_name)
46
+ identities.find{|i| i.short_name == short_name}
47
+ end
48
+
49
+ end
50
+
51
+ def self.start(email, password, port=80, host='localhost')
52
+ Thread.current[:cocupu_connection] = Connection.new(email, password, port, host)
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ module Cocupu
2
+ class File
3
+ attr_accessor :node, :file_name, :file
4
+ attr_reader :conn
5
+ def initialize(node, file_name, file)
6
+ self.node = node
7
+ self.file_name = file_name
8
+ self.file = file
9
+ @conn = Thread.current[:cocupu_connection]
10
+ end
11
+
12
+ def url
13
+ node.url + "/files"
14
+ end
15
+
16
+ def save
17
+ response = conn.post("#{url}.json")#, query: {file_name: file_name, file: file})
18
+ raise "Error saving file: #{response.inspect}" unless response.code >= 200 and response.code < 300
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module Cocupu
2
+ class Identity
3
+ attr_accessor :values, :conn
4
+ def initialize(values)
5
+ self.conn = Thread.current[:cocupu_connection]
6
+ self.values = values
7
+ end
8
+
9
+ def short_name
10
+ values["short_name"]
11
+ end
12
+
13
+ def url
14
+ values["url"]
15
+ end
16
+
17
+ def pools
18
+ return @pools if @pools
19
+ response = conn.get(url+'.json')
20
+ raise "Error getting pools: #{response}" unless response.code == 200
21
+ @pools = response.map {|val| Pool.new(val, conn)}
22
+ end
23
+
24
+ def pool(short_name)
25
+ pools.find{|i| i.short_name == short_name}
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,65 @@
1
+ module Cocupu
2
+ class Model
3
+ attr_accessor :values, :conn
4
+ def initialize(values)
5
+ self.conn = Thread.current[:cocupu_connection]
6
+ self.values = values
7
+ end
8
+
9
+ def name
10
+ values['name']
11
+ end
12
+
13
+ def label=(label)
14
+ values['label'] = label
15
+ end
16
+
17
+ def identity
18
+ values['identity']
19
+ end
20
+
21
+ def pool
22
+ values['pool']
23
+ end
24
+
25
+ def url
26
+ values['url'] || "/#{identity}/#{pool}/models"
27
+ end
28
+
29
+ def url=(url)
30
+ values['url'] = url
31
+ end
32
+
33
+ def id=(id)
34
+ values['id'] = id
35
+ end
36
+
37
+ def fields=(fields)
38
+ values['fields'] = fields
39
+ end
40
+
41
+ def associations=(fields)
42
+ values['associations'] = fields
43
+ end
44
+
45
+ def id
46
+ values['id']
47
+ end
48
+
49
+ def save
50
+ #req_url = "http://#{host}:#{port}#{url}.json?auth_token=#{token}"
51
+ response = if id
52
+ conn.put("#{url}.json", body: {model: values})
53
+ else
54
+ conn.post("#{url}.json", body: {model: values})
55
+ end
56
+ raise "Error saving models: #{response.inspect}" unless response.code >= 200 and response.code < 300
57
+ if (response['id'])
58
+ self.id = response['id']
59
+ self.url = response['url']
60
+ end
61
+ values
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,64 @@
1
+ module Cocupu
2
+ class Node
3
+ attr_accessor :values
4
+ attr_reader :conn
5
+ def initialize(values)
6
+ @conn = Thread.current[:cocupu_connection]
7
+ self.values = values
8
+ end
9
+
10
+ def model_id
11
+ values['model_id']
12
+ end
13
+
14
+ def identity
15
+ values['identity']
16
+ end
17
+
18
+ def pool
19
+ values['pool']
20
+ end
21
+
22
+ def url
23
+ values['url'] || "/#{identity}/#{pool}/nodes"
24
+ end
25
+
26
+ def url=(url)
27
+ values['url'] = url
28
+ end
29
+
30
+ def persistent_id=(id)
31
+ values['persistent_id'] = id
32
+ end
33
+
34
+ def associations=(associations)
35
+ values['associations'] = associations
36
+ end
37
+
38
+ def persistent_id
39
+ values['persistent_id']
40
+ end
41
+
42
+ def save
43
+ response = if persistent_id
44
+ conn.put("#{url}.json", body: {node: values})
45
+ else
46
+ conn.post("#{url}.json", body: {node: values})
47
+ end
48
+ raise "Error saving models: #{response.inspect}" unless response.code >= 200 and response.code < 300
49
+ if (response['persistent_id'])
50
+ self.persistent_id = response['persistent_id']
51
+ self.url = response['url']
52
+ end
53
+ values
54
+ end
55
+
56
+ def attach_file(file_name, file)
57
+ raise RuntimeError "You can't attach a file to an object that hasn't been persisted" unless persistent_id
58
+ Cocupu::File.new(self, file_name, file)
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,26 @@
1
+ module Cocupu
2
+ class Pool
3
+ attr_accessor :values, :conn
4
+ def initialize(values, conn)
5
+ self.conn = conn
6
+ self.values = values
7
+ end
8
+ def short_name
9
+ values["short_name"]
10
+ end
11
+
12
+ def url
13
+ values["url"]
14
+ end
15
+
16
+ def models
17
+ return @models if @models
18
+ # req_url = "http://#{host}:#{port}#{url}/models.json?auth_token=#{token}"
19
+ # puts "Calling #{req_url}"
20
+ response = conn.get("#{url}/models.json")
21
+ #puts "RESP: #{response}"
22
+ raise "Error getting models: #{response}" unless response.code == 200
23
+ @pools = response.map {|val| Model.new(val, conn)}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require 'cocupu'
2
+
3
+ require 'fakeweb'
4
+
5
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
6
+
7
+ RSpec.configure do |config|
8
+
9
+ config.before(:suite) do
10
+ FakeWeb.allow_net_connect = false
11
+ end
12
+
13
+ config.after(:suite) do
14
+ FakeWeb.allow_net_connect = true
15
+ end
16
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cocupu do
4
+
5
+ before do
6
+ FakeWeb.register_uri(:post, "http://localhost:3001/api/v1/tokens", :body => "{\"token\":\"112312\"}", :content_type => "text/json")
7
+ Cocupu.start('justin@cocupu.com', 'password', 3001, 'localhost')
8
+ @identity = 'my_id'
9
+ @pool = 'my_pool'
10
+ end
11
+
12
+ describe "creating a new model" do
13
+ before do
14
+ FakeWeb.register_uri(:post, 'http://localhost:3001/my_id/my_pool/models.json?auth_token=112312', :body=>'{"id":"99"}', :content_type=>"text/json")
15
+ end
16
+ it "should be successful" do
17
+ talk = Cocupu::Model.new({'identity' =>@identity, 'pool'=>@pool, 'name'=>"Talk"})
18
+ talk.fields = [
19
+ {"name"=>"File Name", "type"=>"text", "uri"=>"", "code"=>"file_name"},
20
+ {"name"=>"Tibetan Title", "type"=>"text", "uri"=>"", "code"=>"tibetan_title"},
21
+ {"name"=>"English Title", "type"=>"text", "uri"=>"", "code"=>"english_title"},
22
+ {"name"=>"Author", "type"=>"text", "uri"=>"", "code"=>"author"},
23
+ {"name"=>"Date", "type"=>"text", "uri"=>"", "code"=>"date"},
24
+ {"name"=>"Time", "type"=>"text", "uri"=>"", "code"=>"time"},
25
+ {"name"=>"Size", "type"=>"text", "uri"=>"", "code"=>"size"},
26
+ {"name"=>"Location", "type"=>"text", "uri"=>"", "code"=>"location"},
27
+ {"name"=>"Access", "type"=>"text", "uri"=>"", "code"=>"access"},
28
+ {"name"=>"Originals", "type"=>"text", "uri"=>"", "code"=>"originals"},
29
+ {"name"=>"Master", "type"=>"text", "uri"=>"", "code"=>"master"},
30
+ {"name"=>"Notes", "type"=>"text", "uri"=>"", "code"=>"notes"},
31
+ {"name"=>"Notes (cont)", "type"=>"text", "uri"=>"", "code"=>"notes2"}
32
+ ]
33
+ talk.label = 'file_name'
34
+ talk.save
35
+ talk.id.should == '99'
36
+ end
37
+ end
38
+
39
+ describe "creating a new node" do
40
+ before do
41
+ FakeWeb.register_uri(:post, 'http://localhost:3001/my_id/my_pool/nodes.json?auth_token=112312', :body=>"{\"persistent_id\":\"909877\",\"url\":\"http://foo.bar/\"}", :content_type => "text/json")
42
+ end
43
+ it "should be successful" do
44
+ node = Cocupu::Node.new({'identity'=>@identity, 'pool'=>@pool, 'model_id' => 22, 'data' => {'file_name'=>'my file.xls'}})
45
+ node.save
46
+ node.persistent_id.should == '909877'
47
+ node.url.should == 'http://foo.bar/'
48
+ end
49
+ end
50
+
51
+ describe "attach a file to the node" do
52
+ before do
53
+ FakeWeb.register_uri(:post, 'http://localhost:3001/my_id/my_pool/nodes/909877/files.json?auth_token=112312', :body=>"{\"persistent_id\":\"909877\",\"url\":\"http://foo.bar/\"}", :content_type => "text/json")
54
+ @node = Cocupu::Node.new({'identity'=>@identity, 'pool'=>@pool, 'model_id' => 22, 'data' => {'file_name'=>'my file.xls'}})
55
+ @node.persistent_id = '909877'
56
+ @node.url = "/#{@identity}/#{@pool}/nodes/909877"
57
+ end
58
+ it "should be successful" do
59
+ file = @node.attach_file('my_file_name', File.open('./spec/unit/cocupu_spec.rb'))
60
+ file.save
61
+ end
62
+
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocupu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Coyne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httmultiparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakeweb
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.0
46
+ description: Cocupu is a platform for collecting, curating and publishing your data. This
47
+ client allows you to interface with that data.
48
+ email: justin@cocupu.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - README.md
57
+ - Rakefile
58
+ - VERSION
59
+ - cocupu.gemspec
60
+ - lib/cocupu.rb
61
+ - lib/cocupu/file.rb
62
+ - lib/cocupu/identity.rb
63
+ - lib/cocupu/model.rb
64
+ - lib/cocupu/node.rb
65
+ - lib/cocupu/pool.rb
66
+ - spec/spec_helper.rb
67
+ - spec/unit/cocupu_spec.rb
68
+ homepage: http://cocupu.com
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 1.9.3
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.11
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A client for the Cocupu server
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/unit/cocupu_spec.rb
95
+ has_rdoc: