npo_assets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bart Zonneveld
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.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = npo_assets
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Bart Zonneveld. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "npo_assets"
8
+ gem.summary = %Q{Wrapper for media db}
9
+ gem.description = %Q{Wrapper for media db}
10
+ gem.email = "loop@superinfinite.com"
11
+ gem.homepage = "http://github.com/bartzon/npo_assets"
12
+ gem.authors = ["Bart Zonneveld"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "npo_assets #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ class NpoAssetsGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template('npo_assets.rb', 'db/migrate', :migration_file_name => 'create_assets')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAssets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :assets do |t|
4
+ t.string :url
5
+ t.integer :remote_id
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :blacklists
11
+ end
12
+ end
@@ -0,0 +1,70 @@
1
+ module NPO
2
+ module Assets
3
+ class Asset < ActiveRecord::Base
4
+ include HTTParty
5
+ format :xml
6
+
7
+ attr_accessor :file
8
+
9
+ before_save :post_to_media_server
10
+ after_destroy :delete_from_media_server
11
+
12
+ class << self
13
+ def list(account_name=nil, options={})
14
+ url = "#{ NPO::Assets::BASE_URL }.xml#{ extract_list_options(options) }"
15
+ res = get(url, :headers => headers(account_name))
16
+
17
+ if res && res['assets']
18
+ res['assets']['asset'].each do |vars|
19
+ create(:url => vars['url'], :remote_id => vars['id'])
20
+ end
21
+ else
22
+ []
23
+ end
24
+ end
25
+
26
+ def headers(account_name=nil)
27
+ {'X-Account' => account_name || NPO::Assets::ACCOUNT_NAME}
28
+ end
29
+
30
+ private
31
+ def extract_list_options(options)
32
+ opts = []
33
+ opts << "page=#{options[:page]}" if options[:page]
34
+ opts << "per_page=#{options[:per_page]}" if options[:per_page]
35
+ opts.any? ? "?#{opts.join('&')}" : ''
36
+ end
37
+ end
38
+
39
+ def url(size='')
40
+ File.join(NPO::Assets::BASE_URL, size, attributes['url'])
41
+ end
42
+
43
+ private
44
+ def post_to_media_server
45
+ # return early if we got initialized with a remote_id, and this callback is therefore
46
+ # run due to saving an imported asset
47
+ return true if self.remote_id && new_record?
48
+
49
+ url = [NPO::Assets::BASE_URL]
50
+ url << self.remote_id unless new_record?
51
+
52
+ response = self.class.send(new_record? ? :post : :put,
53
+ url.join('/'),
54
+ :query => { :asset => { :file => @file } },
55
+ :headers => self.class.headers)
56
+ if response.code == 422
57
+ response['errors']['error'].each { |msg| errors[:base] << msg }
58
+ false
59
+ else
60
+ self.url = response['asset']['url']
61
+ true
62
+ end
63
+ end
64
+
65
+ def delete_from_media_server
66
+ HTTParty.delete("#{NPO::Assets::BASE_URL}/#{self.remote_id}", :headers => self.class.headers)
67
+ end
68
+ end
69
+ end
70
+ end
data/lib/npo_assets.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'httparty'
2
+ require 'npo_assets/asset'
3
+
4
+ module NPO
5
+ module Assets
6
+ BASE_URL = 'http://media.omroep.nl/assets'
7
+ ACCOUNT_NAME = nil
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{npo_assets}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bart Zonneveld"]
12
+ s.date = %q{2010-03-03}
13
+ s.description = %q{Wrapper for media db}
14
+ s.email = %q{loop@superinfinite.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "generators/npo_assets/npo_assets_generator.rb",
27
+ "generators/npo_assets/templates/npo_assets.rb",
28
+ "lib/npo_assets.rb",
29
+ "lib/npo_assets/asset.rb",
30
+ "npo_assets.gemspec",
31
+ "spec/npo_assets/asset_spec.rb",
32
+ "spec/npo_assets_spec.rb",
33
+ "spec/spec.opts",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/bartzon/npo_assets}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{Wrapper for media db}
41
+ s.test_files = [
42
+ "spec/npo_assets/asset_spec.rb",
43
+ "spec/npo_assets_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,246 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe NPO::Assets::Asset do
4
+ before(:all) do
5
+ setup_db
6
+ end
7
+
8
+ after(:all) do
9
+ teardown_db
10
+ end
11
+
12
+ describe "posting to the server" do
13
+ before(:each) do
14
+ hash = {"asset" => {"url" => "url", "remote_id" => "2"}}
15
+ @response = HTTParty::Response.new(hash, hash, 200, nil)
16
+ NPO::Assets::Asset.stub!(:post).and_return @response
17
+ @asset = NPO::Assets::Asset.new(:file => "file")
18
+ end
19
+
20
+ describe "when creating a new asset" do
21
+ def do_create
22
+ @asset.save
23
+ end
24
+
25
+ it "should send the correct vars" do
26
+ NPO::Assets::Asset.should_receive(:post).with("http://media.omroep.nl/assets",
27
+ :query => {:asset => {:file => "file"}},
28
+ :headers => {'X-Account' => 'omroep_nl'})
29
+ do_create
30
+ end
31
+
32
+ describe "with successfull save" do
33
+ before(:each) do
34
+ hash = {"asset" => {"created_at" => "2010-03-03T12:42:47Z",
35
+ "id" => "1",
36
+ "name" => "name",
37
+ "updated_at" => "2010-03-03T12:42:47Z",
38
+ "url" => "000/000/000.jpg",
39
+ "file_size" => "12645"} }
40
+ @response = HTTParty::Response.new(hash, hash, 201, nil)
41
+ NPO::Assets::Asset.stub!(:post).and_return @response
42
+ end
43
+
44
+ it "should return true" do
45
+ do_create.should be_true
46
+ end
47
+
48
+ it "should assign the url to the asset" do
49
+ do_create
50
+ @asset.attributes['url'].should == '000/000/000.jpg'
51
+ end
52
+
53
+ it "should save the asset" do
54
+ do_create
55
+ @asset.should_not be_new_record
56
+ end
57
+ end
58
+
59
+ describe "with failed save" do
60
+ before(:each) do
61
+ hash = {"errors"=>{"error"=>"File must be set."}}
62
+ @response = HTTParty::Response.new(hash, hash, 422, nil)
63
+ NPO::Assets::Asset.stub!(:post).and_return @response
64
+ end
65
+
66
+ it "should return false" do
67
+ do_create.should be_false
68
+ end
69
+
70
+ it "should not save the asset" do
71
+ do_create
72
+ @asset.should be_new_record
73
+ end
74
+
75
+ it "should add the error to the asset" do
76
+ do_create
77
+ @asset.errors[:base].should == ["File must be set."]
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "when updating an asset" do
83
+ before(:each) do
84
+ @asset.remote_id = 2
85
+ @asset.save
86
+ @asset.file = "new_file"
87
+ NPO::Assets::Asset.stub!(:put).and_return @response
88
+ end
89
+
90
+ def do_update
91
+ @asset.save
92
+ end
93
+
94
+ it "should send the correct vars" do
95
+ NPO::Assets::Asset.should_receive(:put).with("http://media.omroep.nl/assets/2",
96
+ :query => {:asset => {:file => "new_file"}},
97
+ :headers => {'X-Account' => 'omroep_nl'})
98
+ do_update
99
+ end
100
+
101
+ describe "with successfull save" do
102
+ before(:each) do
103
+ hash = {"asset" => {"created_at" => "2010-03-03T12:42:47Z",
104
+ "id" => "1",
105
+ "name" => "name",
106
+ "updated_at" => "2010-03-03T12:42:47Z",
107
+ "url" => "000/000/000.jpg",
108
+ "file_size" => "12645"} }
109
+ @response = HTTParty::Response.new(hash, hash, 201, nil)
110
+ NPO::Assets::Asset.stub!(:put).and_return @response
111
+ end
112
+
113
+ it "should return true" do
114
+ do_update.should be_true
115
+ end
116
+
117
+ it "should assign the url to the asset" do
118
+ do_update
119
+ @asset.attributes['url'].should == '000/000/000.jpg'
120
+ end
121
+ end
122
+
123
+ describe "with failed save" do
124
+ before(:each) do
125
+ hash = {"errors"=>{"error"=>"File must be set."}}
126
+ @response = HTTParty::Response.new(hash, hash, 422, nil)
127
+ NPO::Assets::Asset.stub!(:put).and_return @response
128
+ end
129
+
130
+ it "should return false" do
131
+ do_update.should be_false
132
+ end
133
+
134
+ it "should add the error to the asset" do
135
+ do_update
136
+ @asset.errors[:base].should == ["File must be set."]
137
+ end
138
+ end
139
+ end
140
+
141
+ describe "when deleting an asset" do
142
+ before(:each) do
143
+ @asset.remote_id = 2
144
+ @asset.save
145
+ HTTParty.stub!(:delete).and_return @response
146
+ end
147
+
148
+ it "should send the correct vars" do
149
+ HTTParty.should_receive(:delete).with("http://media.omroep.nl/assets/2", :headers => {'X-Account' => 'omroep_nl'})
150
+ @asset.destroy
151
+ end
152
+ end
153
+ end
154
+
155
+ describe "when generating an url" do
156
+ before(:each) do
157
+ hash = {"asset" => {"url" => "1.jpg"}}
158
+ @response = HTTParty::Response.new(hash, hash, 200, nil)
159
+ NPO::Assets::Asset.stub!(:post).and_return @response
160
+
161
+ @asset = NPO::Assets::Asset.create!
162
+ end
163
+
164
+ it "should return the correct url without a size" do
165
+ @asset.url.should == "http://media.omroep.nl/assets/1.jpg"
166
+ end
167
+
168
+ it "should return the correct url with a size" do
169
+ @asset.url('100x200').should == "http://media.omroep.nl/assets/100x200/1.jpg"
170
+ end
171
+ end
172
+
173
+ describe "when listing the assets" do
174
+ describe "request" do
175
+ before(:each) do
176
+ NPO::Assets::Asset.stub!(:get).and_return ""
177
+ end
178
+
179
+ def do_list(account='omroep_nl', options={})
180
+ NPO::Assets::Asset.list(account, options)
181
+ end
182
+
183
+ def should_get(url, options)
184
+ NPO::Assets::Asset.should_receive(:get).with(url, options)
185
+ end
186
+
187
+ it "should call the correct url with an explicit account name" do
188
+ should_get('http://media.omroep.nl/assets.xml', :headers => {'X-Account' => 'foo'})
189
+ do_list('foo')
190
+ end
191
+
192
+ it "should call the correct url without an explicit account name" do
193
+ should_get('http://media.omroep.nl/assets.xml', :headers => {'X-Account' => 'omroep_nl'})
194
+ NPO::Assets::Asset.list
195
+ end
196
+
197
+ describe "with options" do
198
+ it "should use :per_page" do
199
+ should_get('http://media.omroep.nl/assets.xml?per_page=10', :headers => {'X-Account' => 'foo'})
200
+ do_list('foo', :per_page => 10)
201
+ end
202
+
203
+ it "should use :page" do
204
+ should_get('http://media.omroep.nl/assets.xml?page=2', :headers => {'X-Account' => 'foo'})
205
+ do_list('foo', :page => 2)
206
+ end
207
+
208
+ it "should use :page and :per_page" do
209
+ should_get('http://media.omroep.nl/assets.xml?page=2&per_page=10', :headers => {'X-Account' => 'foo'})
210
+ do_list('foo', :page => 2, :per_page => 10)
211
+ end
212
+ end
213
+ end
214
+
215
+ describe "response" do
216
+ before(:each) do
217
+ @response = {"assets"=>{"results"=>"2", "total_pages"=>"1",
218
+ "asset"=>[{"name"=>"bach", "url"=>"000/000/001.jpg", "id"=>"1"},
219
+ {"name"=>"bach", "url"=>"000/000/002.jpg", "id"=>"2"}]}}
220
+
221
+ NPO::Assets::Asset.stub!(:get).and_return @response
222
+ end
223
+
224
+ def do_list
225
+ NPO::Assets::Asset.list
226
+ end
227
+
228
+ it "should return [] if there are no items" do
229
+ NPO::Assets::Asset.stub!(:get).and_return("nil_classes" => nil)
230
+ do_list.should == []
231
+ end
232
+
233
+ it "should generate new assets for each returned asset" do
234
+ NPO::Assets::Asset.should_receive(:create).with(:url => "000/000/001.jpg", :remote_id => "1").once
235
+ NPO::Assets::Asset.should_receive(:create).with(:url => "000/000/002.jpg", :remote_id => "2").once
236
+ do_list
237
+ end
238
+
239
+ it "should not post to the server *again*" do
240
+ NPO::Assets::Asset.should_not_receive(:post)
241
+ NPO::Assets::Asset.should_not_receive(:put)
242
+ do_list
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe NPO::Assets do
4
+ it "should set the correct BASE_URL" do
5
+ NPO::Assets::BASE_URL.should == 'http://media.omroep.nl/assets'
6
+ end
7
+
8
+ it "should set an empty account name" do
9
+ NPO::Assets::ACCOUNT_NAME.should == nil
10
+ end
11
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,39 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+
6
+ require 'npo_assets'
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+
10
+ begin
11
+ require 'sqlite3'
12
+ rescue MissingSourceFile => e
13
+ puts "Gem sqlite3-ruby could not be found, please install it"
14
+ exit
15
+ end
16
+
17
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
18
+
19
+ $stdout = StringIO.new
20
+
21
+ def setup_db
22
+ ActiveRecord::Base.logger
23
+ ActiveRecord::Schema.define(:version => 1) do
24
+ create_table :assets do |t|
25
+ t.string :url
26
+ t.integer :remote_id
27
+ end
28
+ end
29
+ end
30
+
31
+ def teardown_db
32
+ ActiveRecord::Base.connection.tables.each do |table|
33
+ ActiveRecord::Base.connection.drop_table(table)
34
+ end
35
+ end
36
+
37
+ Spec::Runner.configure do |config|
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npo_assets
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Bart Zonneveld
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-03 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Wrapper for media db
35
+ email: loop@superinfinite.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - generators/npo_assets/npo_assets_generator.rb
51
+ - generators/npo_assets/templates/npo_assets.rb
52
+ - lib/npo_assets.rb
53
+ - lib/npo_assets/asset.rb
54
+ - npo_assets.gemspec
55
+ - spec/npo_assets/asset_spec.rb
56
+ - spec/npo_assets_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/bartzon/npo_assets
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.6
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Wrapper for media db
89
+ test_files:
90
+ - spec/npo_assets/asset_spec.rb
91
+ - spec/npo_assets_spec.rb
92
+ - spec/spec_helper.rb