attachment_helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cf8951a9aee20e8c28cf899ec617bbb42a1eaa7
4
+ data.tar.gz: 3d53284fb7b13fef431ddc0b8008a90bee97eb3d
5
+ SHA512:
6
+ metadata.gz: a0207a12a87a4a9005474e2e7662b00e00779912b2fd463b7e3f7a4bb82b4437bd277003b90bfd043c66819a177fcf08afc96fe16d16631e038dfc16f77622a6
7
+ data.tar.gz: 5fcd1e47c48941527e85fc21a9a282375ee6643f806850ca02c5226bf2ab256d3a90f3b66b24d5c3b6171899a133aefd1a369d99908e097930df360e198d2c38
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ /spec/examples.txt
14
+
15
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require gem_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in attachment_path.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 jiangzhi.xie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,93 @@
1
+ # AttachmentPath
2
+
3
+ Some Helper methods to generate url and get filename from attachment path
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'attachment_path'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install attachment_path
20
+
21
+ ## Usage
22
+
23
+ ### Setup
24
+
25
+ ```
26
+ AttachmentHelper.attachment_host = 'http://myhost.com'
27
+ ```
28
+
29
+ ### Define attachments
30
+
31
+ ```
32
+ class Topic
33
+ include AttachmentHelper
34
+
35
+ has_attachments [:logo_path, {size: '100x100'}], :pdf_path, :image_paths
36
+
37
+ def logo_path
38
+ 'uploads/a.png'
39
+ end
40
+
41
+ def pdf_path
42
+ 'http://hello.com/assets/a.pdf'
43
+ end
44
+
45
+ def image_paths
46
+ ['a.png', 'http://other.com/b.png']
47
+ end
48
+ end
49
+ ```
50
+
51
+ ### helper methods
52
+
53
+ ```
54
+ topic = Topic.new
55
+ ```
56
+
57
+ Helper methods for path
58
+
59
+ ```
60
+ topic.logo_url # => 'http://myhost.com/uploads/a.png?size=100x100'
61
+ topic.logo_url(size: 200, t: 123) # => 'http://myhost.com/uploads/a.png?size=200&t=123'
62
+ topic.logo_filename # => 'a.png'
63
+ ```
64
+
65
+ Helper methods for url
66
+
67
+ ```
68
+ topic.pdf_url # => 'http://hello.com/assets/a.pdf'
69
+ topic.pdf_url(foo: :bar) # => 'http://hello.com/assets/a.pdf?foo=bar'
70
+ topic.pdf_filename # => 'a.pdf'
71
+ ```
72
+
73
+ Helper methods for a list
74
+
75
+ ```
76
+ topic.image_urls # => ['http://hello/a.png', 'http://other.com/b.png']
77
+ topic.image_urls(a: :foo) # => ['http://hello/a.png?a=foo', 'http://other.com/b.png?a=foo']
78
+ topic.image_filenames # => ['a.png', 'b.png']
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiejiangzhi/attachment_helper.
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "attachment_helper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attachment_helper"
8
+ spec.version = AttachmentHelper::VERSION
9
+ spec.authors = ["jiangzhi.xie"]
10
+ spec.email = ["xiejiangzhi@gmail.com"]
11
+
12
+ spec.summary = %q{Some attachment helpers for attachment path}
13
+ spec.description = %q{Generate url and get filename from attachment path}
14
+ spec.homepage = "https://github.com/xiejiangzhi/attachment_helper"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry", '~> 0.10.4'
28
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "attachment_path"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,77 @@
1
+ require 'attachment_helper'
2
+ require 'benchmark'
3
+
4
+ AttachmentHelper.attachment_host = 'http://myhost.com'
5
+
6
+ class Topic
7
+ include AttachmentHelper
8
+
9
+ has_attachments [:logo_path, {size: '100x100'}], :pdf_path, :image_paths
10
+
11
+ def logo_path
12
+ 'uploads/a.png'
13
+ end
14
+
15
+ def pdf_path
16
+ 'http://hello.com/assets/a.pdf'
17
+ end
18
+
19
+ def image_paths
20
+ ['a.png', 'http://other.com/b.png']
21
+ end
22
+ end
23
+
24
+ topic = Topic.new
25
+
26
+ total_times = 100000
27
+ puts "Times: #{total_times}"
28
+
29
+ Benchmark.bm do |x|
30
+ x.report('Run logo_url') do
31
+ total_times.times do
32
+ topic.logo_url
33
+ end
34
+ end
35
+
36
+ x.report('Run logo_url(opts)') do
37
+ total_times.times do
38
+ topic.logo_url(foo: :bar, long: 'this_is_some_long_text')
39
+ end
40
+ end
41
+
42
+ x.report('Run logo_filename') do
43
+ total_times.times do
44
+ topic.logo_filename
45
+ end
46
+ end
47
+
48
+ x.report('Run pdf_url') do
49
+ total_times.times do
50
+ topic.pdf_url
51
+ end
52
+ end
53
+
54
+ x.report('Run pdf_url(opts)') do
55
+ total_times.times do
56
+ topic.pdf_url(foo: :bar, long: 'this_is_some_long_text')
57
+ end
58
+ end
59
+
60
+ x.report('Run image_urls') do
61
+ total_times.times do
62
+ topic.image_urls
63
+ end
64
+ end
65
+
66
+ x.report('Run image_urls(opts)') do
67
+ total_times.times do
68
+ topic.image_urls(foo: :bar, long: 'this_is_some_long_text')
69
+ end
70
+ end
71
+
72
+ x.report('Run image_filenames') do
73
+ total_times.times do
74
+ topic.image_filenames
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,39 @@
1
+ require 'attachment_helper'
2
+
3
+ AttachmentHelper.attachment_host = 'http://myhost.com'
4
+
5
+ module ExtAttachmentURL
6
+ def get_url_by_path(path, opts = {})
7
+ if path =~ /https?:\/\/private\.com/
8
+ super(path, opts.merge(token: 'signed_token'))
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+
15
+ AttachmentHelper.singleton_class.prepend(ExtAttachmentURL)
16
+
17
+ class Topic
18
+ include AttachmentHelper
19
+
20
+ has_attachments [:logo_path, {size: '100x100'}], :pdf_path, :image_paths
21
+
22
+ def logo_path
23
+ 'http://private.com/a.png'
24
+ end
25
+
26
+ def image_paths
27
+ ['a.png', 'http://private.com/b.png']
28
+ end
29
+ end
30
+
31
+ topic = Topic.new
32
+
33
+ puts topic.logo_url
34
+ puts topic.logo_url(size: 200, t: 123)
35
+
36
+ puts topic.image_urls
37
+ puts topic.image_urls(size: 200, t: 123)
38
+
39
+
@@ -0,0 +1,36 @@
1
+ require 'attachment_helper'
2
+
3
+ AttachmentHelper.attachment_host = 'http://myhost.com'
4
+
5
+ class Topic
6
+ include AttachmentHelper
7
+
8
+ has_attachments [:logo_path, {size: '100x100'}], :pdf_path, :image_paths
9
+
10
+ def logo_path
11
+ 'uploads/a.png'
12
+ end
13
+
14
+ def pdf_path
15
+ 'http://hello.com/assets/a.pdf'
16
+ end
17
+
18
+ def image_paths
19
+ ['a.png', 'http://other.com/b.png']
20
+ end
21
+ end
22
+
23
+ topic = Topic.new
24
+
25
+ puts topic.logo_url
26
+ puts topic.logo_url(size: 200, t: 123)
27
+ puts topic.logo_filename
28
+
29
+ puts topic.pdf_url
30
+ puts topic.pdf_url(foo: :bar)
31
+ puts topic.pdf_filename
32
+
33
+ puts topic.image_urls.inspect
34
+ puts topic.image_urls(a: :foo).inspect
35
+ puts topic.image_filenames.inspect
36
+
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "attachment_helper/class_methods"
4
+ require "attachment_helper/version"
5
+
6
+ require 'uri'
7
+
8
+ module AttachmentHelper
9
+ class << self
10
+ attr_accessor :attachment_host
11
+
12
+ def included(cls)
13
+ cls.extend(AttachmentHelper::ClassMethods)
14
+ end
15
+
16
+ def attachment_host
17
+ @attachment_host ||= 'http://please-set-attachment-host.com'
18
+ end
19
+
20
+
21
+ def symbolize_keys(hash)
22
+ hash.each_with_object({}) {|kv, r| r[(kv.first.to_sym rescue kv.first)] = kv.last }
23
+ end
24
+
25
+ def get_url_by_path(path, options = {})
26
+ return if path.nil?
27
+ query = options.empty? ? '' : "?#{URI.encode_www_form(options)}"
28
+ if path =~ /^http/
29
+ "#{path}#{query}"
30
+ else
31
+ "#{AttachmentHelper.attachment_host}/#{path}#{query}"
32
+ end
33
+ end
34
+
35
+ def get_filename_by_path(path)
36
+ return if path.nil?
37
+ path.split('/').last
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,72 @@
1
+ # Attachment Helper
2
+ #
3
+ # Specify some attachment reader:
4
+ # # avatar_path: a string
5
+ # # pdf_paths: a array of string
6
+ # has_attachments :avatar_path, :pdf_paths
7
+ # # or
8
+ # has_attachments [:avatar_path, {size: '100x100'}], :pdf_paths
9
+ #
10
+ # Generated Methods:
11
+ # avatar_url
12
+ # avatar_filename
13
+ # pdf_urls
14
+ # pdf_filenames
15
+
16
+ module AttachmentHelper
17
+ module ClassMethods
18
+ # field => options
19
+ def has_attachments(*fields)
20
+ fields.each do |field, opts|
21
+ default_opts = AttachmentHelper.symbolize_keys(opts || {})
22
+ case field
23
+ when /_paths$/
24
+ define_attachments_methods(field.to_s, default_opts)
25
+ else
26
+ define_attachment_methods(field.to_s, default_opts)
27
+ end
28
+ end
29
+ end
30
+
31
+ def define_attachment_methods(field, default_opts = {})
32
+ prefix = field.gsub(/_path$/, '')
33
+ m_url, m_filename = ["#{prefix}_url", "#{prefix}_filename"]
34
+
35
+ # {name}_url
36
+ define_method(m_url) do |options = {}|
37
+ val = self.public_send(field)
38
+
39
+ AttachmentHelper.get_url_by_path(
40
+ val, default_opts.merge(AttachmentHelper.symbolize_keys(options))
41
+ )
42
+ end
43
+
44
+ # attachment/{uuid}/filename.pdf return filename.pdf
45
+ define_method(m_filename) do
46
+ val = self.public_send(field)
47
+ AttachmentHelper.get_filename_by_path(val)
48
+ end
49
+ end
50
+
51
+ def define_attachments_methods(field, default_opts)
52
+ prefix = field.gsub(/_paths$/, '')
53
+ m_urls, m_filenames = ["#{prefix}_urls", "#{prefix}_filenames"]
54
+
55
+ define_method(m_urls) do |options = {}|
56
+ val = self.public_send(field)
57
+ return nil if val.nil?
58
+ Array(val).map do |v|
59
+ AttachmentHelper.get_url_by_path(
60
+ v, default_opts.merge(AttachmentHelper.symbolize_keys(options))
61
+ )
62
+ end
63
+ end
64
+
65
+ define_method(m_filenames) do
66
+ val = self.public_send(field)
67
+ return nil if val.nil?
68
+ Array(val).map { |v| AttachmentHelper.get_filename_by_path(v) }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module AttachmentHelper
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attachment_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jiangzhi.xie
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ description: Generate url and get filename from attachment path
70
+ email:
71
+ - xiejiangzhi@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - attachment_helper.gemspec
84
+ - bin/console
85
+ - bin/setup
86
+ - examples/benchmark.rb
87
+ - examples/custom_url.rb
88
+ - examples/getting_start.rb
89
+ - lib/attachment_helper.rb
90
+ - lib/attachment_helper/class_methods.rb
91
+ - lib/attachment_helper/version.rb
92
+ homepage: https://github.com/xiejiangzhi/attachment_helper
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Some attachment helpers for attachment path
116
+ test_files: []