attache_api 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6234cecf6d434e8f65123cbd50ef9581f0709a05
4
+ data.tar.gz: 1862332795ac61a53a672648ebd921ffc7b61f75
5
+ SHA512:
6
+ metadata.gz: 84b45cd11248eaef66edee79eb0d38e90fedc89de43d813bb5946284700b03db2b387b9518c3d95de7fa15bc72421ca8cc77f33b176abf31b124fd7c13a493b8
7
+ data.tar.gz: fa285b2519c2e9d1be70698f24de8b39d9e8364725e2e0fc15ef3b31bc41e55b6cba655a3734c5d314135c258a061486603962be7db4b613468f6636fd8c39b7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 choonkeat
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.md ADDED
@@ -0,0 +1,33 @@
1
+ # Attache/API
2
+
3
+ [![Build Status](https://travis-ci.org/choonkeat/attache_api.svg?branch=master)](https://travis-ci.org/choonkeat/attache_api)
4
+
5
+ Core library and tests to integrate with an [attache](https://github.com/choonkeat/attache) server; to be leveraged by client libraries e.g. [attache_rails](https://github.com/choonkeat/attache_rails) which integrates with ActiveRecord
6
+
7
+ ## Testing against an attache-compatible server
8
+
9
+ Test suite will interact with a server when `ATTACHE_URL` is explicitly set. This can conveniently check if a server at a given URL is attache-compatible. To run such compatibility check, execute the test suite with `ATTACHE_URL` and `ATTACHE_SECRET_KEY` set to the correct values
10
+
11
+ ```
12
+ ATTACHE_URL=http://localhost:9292 ATTACHE_SECRET_KEY=topsecret rake
13
+ ```
14
+
15
+ NOTE: the test suite will upload a small jpg file and delete it immediately, for several iterations.
16
+
17
+ ## Environment variables
18
+
19
+ Important variables:
20
+
21
+ - `ATTACHE_URL` url pointing to the attache server instance, default `http://localhost:9292`
22
+ - `ATTACHE_SECRET_KEY` optional shared secret with attache server, default no secret
23
+
24
+ Optional variables:
25
+
26
+ - `ATTACHE_UPLOAD_DURATION` browser upload signature expiration duration, default 3 hours; used in conjunction with `ATTACHE_SECRET_KEY`
27
+ - `ATTACHE_UPLOAD_URL`, `ATTACHE_DOWNLOAD_URL`, `ATTACHE_DELETE_URL` specific urls pointing to upload, download, delete API end points, default `{ATTACHE_URL}/upload`, `{ATTACHE_URL}/view`, `{ATTACHE_URL}/delete`
28
+ - `ATTACHE_DISCARD_FAILURE_RAISE_ERROR` when set, raises an error if deleting of files causes an error. by default, deletion errors are discarded
29
+
30
+ ## License
31
+
32
+ MIT
33
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,44 @@
1
+ module Attache
2
+ module API
3
+ module Model
4
+ def attache_field_options(attr_value, geometry, options = {})
5
+ V1.attache_options(geometry, attache_field_attributes(attr_value, geometry), options)
6
+ end
7
+
8
+ def attache_field_urls(attr_value, geometry)
9
+ attache_field_attributes(attr_value, geometry).collect {|attrs| attrs['url']}
10
+ end
11
+
12
+ def attache_field_attributes(attr_value, geometry)
13
+ Utils.array(attr_value).inject([]) do |sum, obj|
14
+ sum + Utils.array(obj && obj.tap {|attrs|
15
+ attrs['url'] = V1.attache_url_for(attrs['path'], geometry)
16
+ })
17
+ end
18
+ end
19
+
20
+ def attache_field_set(array)
21
+ new_value = Utils.array(array).inject([]) {|sum,value|
22
+ hash = value.respond_to?(:read) && V1.attache_upload(value) || value
23
+ hash = JSON.parse(hash.to_s) rescue Hash(error: $!) unless hash.kind_of?(Hash)
24
+ okay = hash.respond_to?(:[]) && (hash['path'] || hash[:path])
25
+ okay ? sum + [hash] : sum
26
+ }
27
+ Utils.array(new_value)
28
+ end
29
+
30
+ def attache_mark_for_discarding(old_value, new_value, attaches_discarded)
31
+ obsoleted = Utils.array(old_value).collect {|x| x['path'] } - Utils.array(new_value).collect {|x| x['path'] }
32
+ obsoleted.each {|path| attaches_discarded.push(path) unless path.nil? || path == "" }
33
+ end
34
+
35
+ def attaches_discard!(files)
36
+ files.reject! {|x| x.nil? || x == "" }
37
+ V1.attache_delete(*files.uniq) unless files.empty?
38
+ rescue Exception
39
+ raise if ENV['ATTACHE_DISCARD_FAILURE_RAISE_ERROR']
40
+ STDERR.puts [$!, $@]
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ module Attache
2
+ module API
3
+ module Utils
4
+ class << self
5
+ def array(value)
6
+ case value
7
+ when Array
8
+ value
9
+ else
10
+ [value]
11
+ end.reject {|x| x.nil? || x == "" }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,79 @@
1
+ require 'securerandom'
2
+ require "httpclient"
3
+ require 'openssl'
4
+ require 'uri'
5
+ require 'cgi'
6
+
7
+ module Attache
8
+ module API
9
+ module V1
10
+ ATTACHE_URL = ENV.fetch('ATTACHE_URL') { "http://localhost:9292" }
11
+ ATTACHE_UPLOAD_URL = ENV.fetch('ATTACHE_UPLOAD_URL') { "#{ATTACHE_URL}/upload" }
12
+ ATTACHE_DOWNLOAD_URL = ENV.fetch('ATTACHE_DOWNLOAD_URL') { "#{ATTACHE_URL}/view" }
13
+ ATTACHE_DELETE_URL = ENV.fetch('ATTACHE_DELETE_URL') { "#{ATTACHE_URL}/delete" }
14
+ ATTACHE_UPLOAD_DURATION = ENV.fetch('ATTACHE_UPLOAD_DURATION') { 3*3600 }.to_i # expires signed upload form
15
+ ATTACHE_SECRET_KEY = ENV['ATTACHE_SECRET_KEY'] # unset to test password-less interaction
16
+
17
+ def attache_upload(readable)
18
+ uri = URI.parse(ATTACHE_UPLOAD_URL)
19
+ original_filename =
20
+ readable.respond_to?(:original_filename) && readable.original_filename ||
21
+ readable.respond_to?(:path) && File.basename(readable.path) ||
22
+ 'noname'
23
+ uri.query = { file: original_filename, **attache_auth_options }.collect {|k,v|
24
+ CGI.escape(k.to_s) + "=" + CGI.escape(v.to_s)
25
+ }.join('&')
26
+ res = attache_retry_doing(3) { HTTPClient.post(uri, readable, {'Content-Type' => 'binary/octet-stream'}) }
27
+ res.body
28
+ end
29
+
30
+ def attache_url_for(path, geometry)
31
+ prefix, basename = File.split(path)
32
+ [ATTACHE_DOWNLOAD_URL, prefix, CGI.escape(geometry), CGI.escape(basename)].join('/')
33
+ end
34
+
35
+ def attache_delete(*paths)
36
+ HTTPClient.post_content(
37
+ URI.parse(ATTACHE_DELETE_URL),
38
+ attache_auth_options.merge(paths: paths.join("\n"))
39
+ )
40
+ end
41
+
42
+ def attache_auth_options
43
+ if ATTACHE_SECRET_KEY
44
+ uuid = SecureRandom.uuid
45
+ expiration = (Time.now + ATTACHE_UPLOAD_DURATION).to_i
46
+ hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ATTACHE_SECRET_KEY, "#{uuid}#{expiration}")
47
+ { uuid: uuid, expiration: expiration, hmac: hmac }
48
+ else
49
+ {}
50
+ end
51
+ end
52
+
53
+ def attache_options(geometry, current_value, auth_options: true, placeholder: nil, data: {})
54
+ {
55
+ data: {
56
+ geometry: geometry,
57
+ value: [*current_value],
58
+ placeholder: [*placeholder],
59
+ uploadurl: ATTACHE_UPLOAD_URL,
60
+ downloadurl: ATTACHE_DOWNLOAD_URL,
61
+ }.merge(data || {}).merge(auth_options == false ? {} : attache_auth_options),
62
+ }
63
+ end
64
+
65
+ def attache_retry_doing(max_retries, retries = 0, exception_class = Exception)
66
+ yield
67
+ rescue exception_class
68
+ if (retries += 1) <= max_retries
69
+ max_sleep_seconds = Float(2 ** retries)
70
+ sleep rand(0..max_sleep_seconds)
71
+ retry
72
+ end
73
+ raise
74
+ end
75
+
76
+ self.extend(self)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module Attache
2
+ module API
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'attache/api/version'
2
+ require 'attache/api/utils'
3
+ require 'attache/api/v1'
4
+ require 'attache/api/model'
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attache_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - choonkeat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fastimage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - choonkeat@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - MIT-LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - lib/attache/api.rb
108
+ - lib/attache/api/model.rb
109
+ - lib/attache/api/utils.rb
110
+ - lib/attache/api/v1.rb
111
+ - lib/attache/api/version.rb
112
+ homepage: https://github.com/choonkeat/attache_api
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: API for client lib to integrate with attache server
136
+ test_files: []