secureshare-ruby 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,43 +1,19 @@
1
- require 'rest_client'
2
- require 'secureshare/vault'
3
- require 'secureshare/document'
4
- require "net/http"
5
- require 'secureshare/exception'
1
+ require 'secureshare/config'
2
+ require 'secureshare/client'
6
3
 
7
4
  module SecureShare
8
- @@config = {
9
- endpoint: 'https://api.secureshare.com.au'
10
- }
11
-
12
- def self.config options={}
13
- @@config.merge! options
14
- end
15
-
16
- def self.fetch_vault vault_id
17
- resource = RestClient::Resource.new "#{@@config[:endpoint]}/v1/#{@@config[:api_key]}/vaults/#{vault_id}"
18
- response = resource.get :authorization => %{Token token="#{@@config[:api_secret]}"}, :content_type => :json, :accept => :json
19
- vault = Vault.new(JSON.parse(response.body))
20
- end
5
+ class << self
6
+ def client
7
+ @client ||= SecureShare::Client.new
8
+ end
21
9
 
22
- def self.follow_vault vault_id, email, follow_url
23
- resource = RestClient::Resource.new "#{@@config[:endpoint]}/v1/#{@@config[:api_key]}/vaults/#{vault_id}/follow"
24
- begin
25
- resource.post({ :email => email, :site_name => follow_url }, {:authorization => %{Token token="#{@@config[:api_secret]}"}, :content_type => :json, :accept => :json})
26
- rescue RestClient::UnprocessableEntity => e
27
- error_json = JSON.parse e.http_body
28
- raise SecureShare::RestException.new(*error_json.first)
10
+ def method_missing(method, *args, &block)
11
+ return super unless client.respond_to?(method)
12
+ client.send(method, *args, &block)
29
13
  end
30
- true
31
- end
32
14
 
33
- def self.available?
34
- begin
35
- url = URI.parse(@@config[:endpoint])
36
- request = Net::HTTP.new(url.host, url.port)
37
- request.request_head('/health')
38
- true
39
- rescue
40
- false
15
+ def respond_to?(method, include_private=false)
16
+ client.respond_to?(method, include_private) || super(method, include_private)
41
17
  end
42
18
  end
43
19
  end
@@ -1,43 +1,15 @@
1
- require 'json'
1
+ require 'secureshare/request'
2
+ require 'secureshare/client/documents'
3
+ require 'secureshare/client/follow'
2
4
 
3
5
  module SecureShare
4
6
  class Client
7
+ include SecureShare::Request
8
+ include SecureShare::Client::Documents
9
+ include SecureShare::Client::Follow
5
10
 
6
- def initialize endpoint, access_token, site_name
7
- @endpoint = endpoint
8
- @access_token = access_token
9
- @site_name = site_name
10
- end
11
-
12
- def self.for endpoint, access_token, site_name
13
- new(endpoint)
14
- end
15
-
16
- def fetch_vault vault_id
17
- response = rest_client.get "#{@endpoint}/api/v1/vaults/#{vault_id}", headers
18
- JSON.parse(response.body)
19
- end
20
-
21
- def follow_vault vault_id, email
22
- response = rest_client.post("#{@endpoint}/api/v1/vaults/#{vault_id}/follow", { :email => email, :site_name => @site_name}, headers)
23
- JSON.parse(response.body)
24
- rescue RestClient::UnprocessableEntity => e
25
- error_json = JSON.parse e.http_body
26
- raise SecureShare::RestException.new(*error_json.first)
27
- end
28
-
29
- def rest_client= rest_client
30
- @rest_client = rest_client
31
- end
32
-
33
- private
34
-
35
- def rest_client
36
- @rest_client || RestClient
37
- end
38
-
39
- def headers
40
- {:authorization => %{Token token="#{@access_token}"}, :content_type => :json, :accept => :json}
11
+ def config
12
+ SecureShare.config
41
13
  end
42
14
  end
43
- end
15
+ end
@@ -0,0 +1,10 @@
1
+ module SecureShare
2
+ class Client
3
+ module Documents
4
+
5
+ def documents vault_name
6
+ get "/v1/vaults/#{config.org_name}/#{vault_name}/documents"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SecureShare
2
+ class Client
3
+ module Follow
4
+
5
+ def follow vault_name, options={}
6
+ post "/v1/vaults/#{config.org_name}/#{vault_name}/follow", options
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_support/configurable'
2
+
3
+ module SecureShare
4
+
5
+ def self.configure(&block)
6
+ yield @config ||= SecureShare::Configuration.new
7
+ config.ensure_configured!
8
+ end
9
+
10
+ def self.config
11
+ @config
12
+ end
13
+
14
+ class Configuration
15
+ include ActiveSupport::Configurable
16
+ config_accessor :url do
17
+ 'https://api.secureshare.com.au'
18
+ end
19
+
20
+ ATTRS = [:api_key, :api_secret, :org_name]
21
+
22
+ config_accessor *ATTRS
23
+
24
+ def ensure_configured!
25
+ ATTRS.each do |attr|
26
+ raise "SecureShare config option missing: #{attr}" unless send(attr)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ require 'rest_client'
2
+ require 'ostruct'
3
+
4
+ module SecureShare
5
+ module Request
6
+
7
+ def get path, options={}
8
+ resource = RestClient::Resource.new "#{config.url}#{path}", options
9
+ response = resource.get :authorization => %{Token token="#{config.api_secret}", key="#{config.api_key}"}, :content_type => :json, :accept => :json
10
+ parse_response response
11
+ end
12
+
13
+ def post path, options={}
14
+ resource = RestClient::Resource.new "#{config.url}#{path}"
15
+ response = resource.post options, {:authorization => %{Token token="#{config.api_secret}", key="#{config.api_key}"}, :content_type => :json, :accept => :json}
16
+ parse_response response
17
+ end
18
+
19
+ private
20
+
21
+ def parse_response response
22
+ response_object = JSON.parse(response.body)
23
+ if response_object.is_a?(Enumerable)
24
+ response_object.map do |object|
25
+ OpenStruct.new(object)
26
+ end
27
+ else
28
+ OpenStruct.new(response_object)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "secureshare-ruby"
7
- gem.version = "0.0.9"
7
+ gem.version = "0.0.10"
8
8
  gem.authors = ["Chris Aitchison"]
9
9
  gem.email = ["chris.aitchison@sodalis.com.au"]
10
10
  gem.description = %q{SecureShare Ruby API}
@@ -17,7 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_runtime_dependency 'rest-client'
20
+ gem.add_dependency 'rest-client'
21
+ gem.add_dependency 'activesupport', ['>= 3.0.0']
21
22
 
22
23
  gem.add_development_dependency "rake"
23
24
  gem.add_development_dependency "rspec"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secureshare-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
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-04-02 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rake
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -74,9 +90,10 @@ files:
74
90
  - Rakefile
75
91
  - lib/secureshare.rb
76
92
  - lib/secureshare/client.rb
77
- - lib/secureshare/document.rb
78
- - lib/secureshare/exception.rb
79
- - lib/secureshare/vault.rb
93
+ - lib/secureshare/client/documents.rb
94
+ - lib/secureshare/client/follow.rb
95
+ - lib/secureshare/config.rb
96
+ - lib/secureshare/request.rb
80
97
  - secureshare-ruby.gemspec
81
98
  - spec/secureshare/client_spec.rb
82
99
  - spec/secureshare_spec.rb
@@ -95,18 +112,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
112
  - - ! '>='
96
113
  - !ruby/object:Gem::Version
97
114
  version: '0'
98
- segments:
99
- - 0
100
- hash: 3912854031429418768
101
115
  required_rubygems_version: !ruby/object:Gem::Requirement
102
116
  none: false
103
117
  requirements:
104
118
  - - ! '>='
105
119
  - !ruby/object:Gem::Version
106
120
  version: '0'
107
- segments:
108
- - 0
109
- hash: 3912854031429418768
110
121
  requirements: []
111
122
  rubyforge_project:
112
123
  rubygems_version: 1.8.23
@@ -1,46 +0,0 @@
1
- module SecureShare
2
- class Document
3
- def initialize json
4
- @name = json["name"]
5
- @content_url = json["content_url"]
6
- @description = json["description"]
7
- @content_filename = json["content_filename"]
8
- @content_type = json["content_type"]
9
- @tags = json["tags"]
10
- @updated_at = json["updated_at"]
11
- @created_at = json["created_at"]
12
- end
13
-
14
- def name
15
- @name
16
- end
17
-
18
- def content_url
19
- @content_url
20
- end
21
-
22
- def description
23
- @description
24
- end
25
-
26
- def updated_at
27
- @updated_at
28
- end
29
-
30
- def created_at
31
- @created_at
32
- end
33
-
34
- def content_filename
35
- @content_filename
36
- end
37
-
38
- def tags
39
- @tags
40
- end
41
-
42
- def content_type
43
- @content_type
44
- end
45
- end
46
- end
@@ -1,11 +0,0 @@
1
- module SecureShare
2
-
3
- class RestException < RuntimeError
4
- attr_accessor :field, :message
5
-
6
- def initialize field, message
7
- @field = field
8
- @message = [message].flatten.first
9
- end
10
- end
11
- end
@@ -1,18 +0,0 @@
1
- module SecureShare
2
- class Vault
3
- def initialize vault_json
4
- @name = vault_json["name"]
5
- @documents = vault_json["documents"].map do |document_json|
6
- Document.new document_json
7
- end
8
- end
9
-
10
- def name
11
- @name
12
- end
13
-
14
- def documents
15
- @documents
16
- end
17
- end
18
- end