cdd 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,7 @@ require 'cdd'
25
25
  cdd = CDD::Client.new(YOUR_TOKEN)
26
26
  ```
27
27
 
28
+ ## Vaults
28
29
  Retrieve your vaults
29
30
 
30
31
  ```ruby
@@ -36,7 +37,8 @@ Returns a list of vault objects that look like:
36
37
  [{"name"=>"Your Vault Name", "id"=>1234}]
37
38
  ```
38
39
 
39
- With a vault object, you can retrieve it's project list:
40
+ ## Projects
41
+ Given a vault object, you can retrieve it's project list:
40
42
  ```ruby
41
43
  projects = vaults.first.projects
42
44
  ```
@@ -46,6 +48,77 @@ Returns a list of project objects that look like:
46
48
  [{"name"=>"Your Project Name", "id"=>1234}]
47
49
  ```
48
50
 
51
+ ## Searches
52
+ Given a vault object, you can retrieve it's searches list:
53
+ ```ruby
54
+ projects = vaults.first.searches
55
+ ```
56
+
57
+ Returns a list of searches objects that look like:
58
+ ```ruby
59
+ [{"name"=>"Your Search Name", "id"=>1234}]
60
+ ```
61
+
62
+ ## Exporting
63
+ With a ```Search``` object and one or more ```Project``` objects, you can export matching data.
64
+
65
+ ### Synchronous Export
66
+ The simplest way to export data is using the ```Search#export``` method. The CDD API is asynchronous, this method wraps the API in a synchronous method. You can use in two ways:
67
+
68
+ ```ruby
69
+ require 'cdd'
70
+
71
+ cdd = CDD::Client.new(YOUR_TOKEN)
72
+ vaults = cdd.vaults
73
+ vault = vaults.first
74
+ search = vault.searches.first
75
+ projects = vault.projects
76
+ data = search.export(projects, "csv") # The second parameter defaults to CSV and can be omitted.
77
+ puts data
78
+ ```
79
+
80
+ Or by using the block syntax, this time in SDF format:
81
+
82
+ ```ruby
83
+ require 'cdd'
84
+
85
+ cdd = CDD::Client.new(YOUR_TOKEN)
86
+ vaults = cdd.vaults
87
+ vault = vaults.first
88
+ search = vault.searches.first
89
+ projects = vault.projects
90
+ search.export(projects, "sdf") do |data|
91
+ puts data
92
+ end
93
+ ```
94
+
95
+ ### Asynchronous Export
96
+
97
+ The synchronous export method is provided as a courtesy. If you could be exporting any non-trivial amount of data, you should use the aynchronous method.
98
+
99
+ To begin an export using the method ```Search#start_export```. This will return an ```Export``` object which can be polled or used to retrieve data.
100
+
101
+ ```ruby
102
+ require 'cdd'
103
+
104
+ cdd = CDD::Client.new(YOUR_TOKEN)
105
+ vaults = cdd.vaults
106
+ vault = vaults.first
107
+ search = vault.searches.first
108
+ projects = vault.projects
109
+ export = search.start_export(projects, "sdf")
110
+ export_state = export.poll
111
+ puts export_state["status"]
112
+ ```
113
+
114
+ When ```export.poll["status"] == "finished"``` the export is complete and can be downloaded. You retrieve the data with the following method:
115
+
116
+ ```ruby
117
+ export.data
118
+ ```
119
+
120
+ That will return a string containing either the CSV or SDF results.
121
+
49
122
  ## Contributing
50
123
 
51
124
  1. Fork it
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
data/lib/cdd/base.rb ADDED
@@ -0,0 +1,13 @@
1
+ module CDD
2
+ class Base
3
+ attr_accessor :id
4
+ attr_accessor :client
5
+
6
+ def initialize(client, options={})
7
+ self.client = client
8
+ options.each do |k,v|
9
+ self.send("#{k}=",v)
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/cdd/client.rb CHANGED
@@ -12,12 +12,12 @@ module CDD
12
12
  end
13
13
 
14
14
  def vaults
15
- execute(vaults_uri).collect do |hash|
15
+ @vaults ||= execute(vaults_url).collect do |hash|
16
16
  CDD::Vault.new(self,hash)
17
17
  end
18
18
  end
19
19
 
20
- def vaults_uri
20
+ def vaults_url
21
21
  "/api/v1/vaults"
22
22
  end
23
23
 
data/lib/cdd/export.rb ADDED
@@ -0,0 +1,25 @@
1
+ module CDD
2
+ class Export < Base
3
+ attr_accessor :format
4
+ attr_accessor :status
5
+ attr_accessor :search
6
+ attr_accessor :vault
7
+
8
+ def poll
9
+ client.execute(poll_url)
10
+ end
11
+
12
+ def poll_url
13
+ "/api/v1/vaults/#{vault.id}/export_progress/#{self.id}"
14
+ end
15
+
16
+ def data
17
+ response = RestClient.get "#{client.url}#{data_url}", { :params => {}, "X-CDD-Token" => client.token }
18
+ response.to_s if response
19
+ end
20
+
21
+ def data_url
22
+ "/api/v1/vaults/#{vault.id}/exports/#{id}"
23
+ end
24
+ end
25
+ end
data/lib/cdd/project.rb CHANGED
@@ -1,22 +1,6 @@
1
1
  module CDD
2
- class Project
3
- attr_accessor :id
2
+ class Project < Base
4
3
  attr_accessor :name
5
- attr_accessor :client
6
-
7
- def initialize(client, options={})
8
- self.client = client
9
- options.each do |k,v|
10
- self.send("#{k}=",v)
11
- end
12
- end
13
-
14
- def projects
15
- client.execute(projects_url)
16
- end
17
-
18
- def projects_url
19
- "/api/v1/vaults/#{self.id}/projects"
20
- end
4
+ attr_accessor :vault
21
5
  end
22
6
  end
data/lib/cdd/search.rb ADDED
@@ -0,0 +1,27 @@
1
+ module CDD
2
+ class Search < Base
3
+ attr_accessor :name
4
+ attr_accessor :vault
5
+
6
+ def start_export(projects=[], format="csv")
7
+ project_ids = projects.collect { |p| p.id }
8
+ params = { :search => self.id, :projects => project_ids }
9
+ result = JSON.parse(RestClient.post("#{client.url}#{vault.export_url(format)}", params, { "X-CDD-Token" => client.token }))
10
+ CDD::Export.new(client, { :search => self, :vault => vault, :format => format }.merge(result))
11
+ end
12
+
13
+ def export(projects=[], format="csv", &block)
14
+ e = start_export(projects, format)
15
+ state = e.poll
16
+ while ["new", "started", "starting"].include?(state["status"]) do
17
+ state = e.poll
18
+ end
19
+ data = e.data
20
+ if block
21
+ return block.call(data)
22
+ else
23
+ return data
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/cdd/vault.rb CHANGED
@@ -1,24 +1,29 @@
1
1
  module CDD
2
- class Vault
3
- attr_accessor :id
2
+ class Vault < Base
4
3
  attr_accessor :name
5
- attr_accessor :client
6
-
7
- def initialize(client, options={})
8
- self.client = client
9
- options.each do |k,v|
10
- self.send("#{k}=",v)
11
- end
12
- end
13
4
 
14
5
  def projects
15
- client.execute(projects_url).collect do |hash|
16
- CDD::Project.new(self.client, hash)
6
+ @project ||= client.execute(projects_url).collect do |hash|
7
+ CDD::Project.new(self.client, {:vault => self}.merge(hash))
17
8
  end
18
9
  end
19
10
 
20
11
  def projects_url
21
12
  "/api/v1/vaults/#{self.id}/projects"
22
13
  end
14
+
15
+ def searches
16
+ @searches ||= client.execute(searches_url).collect do |hash|
17
+ CDD::Search.new(client, {:vault => self}.merge(hash))
18
+ end
19
+ end
20
+
21
+ def searches_url
22
+ "/api/v1/vaults/#{self.id}/searches"
23
+ end
24
+
25
+ def export_url(format="csv")
26
+ "/api/v1/vaults/#{self.id}/exports.#{format}"
27
+ end
23
28
  end
24
29
  end
data/lib/cdd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CDD
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/cdd.rb CHANGED
@@ -1,4 +1,7 @@
1
+ require "cdd/base"
1
2
  require "cdd/client"
3
+ require "cdd/export"
2
4
  require "cdd/project"
5
+ require "cdd/search"
3
6
  require "cdd/vault"
4
7
  require "cdd/version"
data/spec/cdd_spec.rb ADDED
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ describe CDD do
4
+
5
+ context "A default client" do
6
+ context "when requesting vaults" do
7
+ let(:token) { ENV["CDD_TOKEN_ID"] || "1234567890" }
8
+ let(:client) { CDD::Client.new(token) }
9
+
10
+ it "should have the correct vaults_url" do
11
+ client.vaults_url.should == "/api/v1/vaults"
12
+ end
13
+
14
+ it "should call the default vaults url" do
15
+ RestClient.should_receive(:get).with("https://app.collaborativedrug.com/api/v1/vaults", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "1234", :name => "Test Vault"}].to_json))
16
+ client.vaults
17
+ end
18
+
19
+ context "when requesting vaults" do
20
+ before(:all) do
21
+ RestClient.stub(:get).with("https://app.collaborativedrug.com/api/v1/vaults", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "1234", :name => "Test Vault"}].to_json))
22
+ end
23
+ let(:vaults) { client.vaults }
24
+
25
+ it "should return an array of vaults" do
26
+ vaults.class.should == Array
27
+ vaults.first.class.should == CDD::Vault
28
+ end
29
+
30
+ context "the vault" do
31
+ let(:vault) { vaults.first }
32
+
33
+ it "should have the correct values" do
34
+ vault.id.should == "1234"
35
+ vault.name.should == "Test Vault"
36
+ end
37
+
38
+ it "should have the correct default export_url" do
39
+ vault.export_url.should == "/api/v1/vaults/#{vault.id}/exports.csv"
40
+ end
41
+
42
+ it "should have the correct csv export_url" do
43
+ vault.export_url("csv").should == "/api/v1/vaults/#{vault.id}/exports.csv"
44
+ end
45
+
46
+ it "should have the correct sdf export_url" do
47
+ vault.export_url("sdf").should == "/api/v1/vaults/#{vault.id}/exports.sdf"
48
+ end
49
+
50
+ it "should have the correct searches_url" do
51
+ vault.searches_url.should == "/api/v1/vaults/#{vault.id}/searches"
52
+ end
53
+
54
+ it "should call the default searches url" do
55
+ RestClient.should_receive(:get).with("https://app.collaborativedrug.com/api/v1/vaults/#{vault.id}/searches", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "12345", :name => "Test Search"}].to_json))
56
+ vault.searches
57
+ end
58
+
59
+ context "when requesting searches" do
60
+ before(:all) do
61
+ RestClient.stub(:get).with("https://app.collaborativedrug.com/api/v1/vaults/#{vault.id}/searches", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "12345", :name => "Test Search"}].to_json))
62
+ end
63
+ let(:searches) { vault.searches }
64
+
65
+ it "should return an array of searches" do
66
+ searches.class.should == Array
67
+ searches.first.class.should == CDD::Search
68
+ end
69
+
70
+ context "the search" do
71
+ let(:search) { searches.first }
72
+
73
+ it "should have the correct values" do
74
+ search.id.should == "12345"
75
+ search.name.should == "Test Search"
76
+ end
77
+ end
78
+ end
79
+
80
+ it "should have the correct projects_url" do
81
+ vaults.first.projects_url.should == "/api/v1/vaults/#{vaults.first.id}/projects"
82
+ end
83
+
84
+ it "should call the default projects url" do
85
+ RestClient.should_receive(:get).with("https://app.collaborativedrug.com/api/v1/vaults/#{vaults.first.id}/projects", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "123456", :name => "Test Project"}].to_json))
86
+ vault.projects
87
+ end
88
+
89
+ context "when requesting projects" do
90
+ before(:all) do
91
+ RestClient.stub(:get).with("https://app.collaborativedrug.com/api/v1/vaults/#{vaults.first.id}/projects", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "123456", :name => "Test Project"}].to_json))
92
+ end
93
+ let(:projects) { vault.projects }
94
+
95
+ it "should return an array of projects" do
96
+ projects.class.should == Array
97
+ projects.first.class.should == CDD::Project
98
+ end
99
+
100
+ context "the project" do
101
+ let(:project) { projects.first }
102
+
103
+ it "should have the correct values" do
104
+ project.id.should == "123456"
105
+ project.name.should == "Test Project"
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ context "A client with a custom url" do
115
+ context "when requesting vaults" do
116
+ let(:token) { ENV["CDD_TOKEN_ID"] || "1234567890" }
117
+ let(:client) { CDD::Client.new(token, :url => "http://example.com") }
118
+
119
+ it "should call the custom vaults url" do
120
+ RestClient.should_receive(:get).with("http://example.com/api/v1/vaults", { :params=>{} , "X-CDD-Token" => token}).and_return(fake_response([{:id => "1234", :name => "Test Vault"}].to_json))
121
+ vaults = client.vaults
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,7 @@
1
+ require 'cdd'
2
+
3
+ def fake_response(body)
4
+ net_http_resp = Net::HTTPResponse.new(1.0, 200, "OK")
5
+ # net_http_resp.add_field 'Set-Cookie', 'Monster'
6
+ RestClient::Response.create(body, net_http_resp, nil)
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -89,10 +89,15 @@ files:
89
89
  - Rakefile
90
90
  - cdd.gemspec
91
91
  - lib/cdd.rb
92
+ - lib/cdd/base.rb
92
93
  - lib/cdd/client.rb
94
+ - lib/cdd/export.rb
93
95
  - lib/cdd/project.rb
96
+ - lib/cdd/search.rb
94
97
  - lib/cdd/vault.rb
95
98
  - lib/cdd/version.rb
99
+ - spec/cdd_spec.rb
100
+ - spec/spec_helper.rb
96
101
  homepage: http://github.com/cpetersen/cdd
97
102
  licenses: []
98
103
  post_install_message:
@@ -107,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
112
  version: '0'
108
113
  segments:
109
114
  - 0
110
- hash: -2264778669571184428
115
+ hash: 3887554079488693763
111
116
  required_rubygems_version: !ruby/object:Gem::Requirement
112
117
  none: false
113
118
  requirements:
@@ -116,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
121
  version: '0'
117
122
  segments:
118
123
  - 0
119
- hash: -2264778669571184428
124
+ hash: 3887554079488693763
120
125
  requirements: []
121
126
  rubyforge_project:
122
127
  rubygems_version: 1.8.24
@@ -124,4 +129,6 @@ signing_key:
124
129
  specification_version: 3
125
130
  summary: Collaborative Drug Discovery is a web based tool for storing and sharing
126
131
  pharmaceutical research. This is a ruby wrapper for their API.
127
- test_files: []
132
+ test_files:
133
+ - spec/cdd_spec.rb
134
+ - spec/spec_helper.rb