imaginary 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in imaginary.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hendrik Mans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # Imaginary::Client
2
+
3
+ client = Imaginary::Client.new('http://imaginary.dev/',
4
+ bucket: 'test', username: 'zomg', password: 'zomg', secret: '12345')
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'imaginary'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install imaginary
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # integrate rspec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'imaginary/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "imaginary"
8
+ gem.version = Imaginary::VERSION
9
+ gem.authors = ["Hendrik Mans"]
10
+ gem.email = ["hendrik@mans.de"]
11
+ gem.description = %q{Client gem for Imaginary.}
12
+ gem.summary = %q{Client gem for Imaginary.}
13
+ gem.homepage = "https://github.com/hmans/imaginary"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'httmultiparty', '>= 0.3.8'
21
+
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'fakeweb'
24
+ end
@@ -0,0 +1,75 @@
1
+ require "imaginary/version"
2
+ require 'httmultiparty'
3
+
4
+ module Imaginary
5
+ class Client
6
+ include HTTMultiParty
7
+
8
+ def initialize(url, options = {})
9
+ @base_url = url
10
+ @auth = { username: options[:username], password: options[:password] }
11
+ @bucket = options[:bucket]
12
+ @secret = options[:secret]
13
+ end
14
+
15
+ def post(url, params)
16
+ params[:basic_auth] ||= @auth
17
+ self.class.post(url, params)
18
+ end
19
+
20
+ def add_image_from_file(file, name = nil)
21
+ r = post("#{@base_url}/api/buckets/#{@bucket}/images.json", body: { image: {
22
+ name: name,
23
+ image: file
24
+ }})
25
+
26
+ if r["errors"]
27
+ raise "Failed. #{r["errors"]}"
28
+ else
29
+ r["name"]
30
+ end
31
+ end
32
+
33
+ def add_image_from_url(url, name = nil)
34
+ r = post("#{@base_url}/api/buckets/#{@bucket}/images.json", body: { image: {
35
+ name: name,
36
+ image_url: url
37
+ }})
38
+
39
+ if r["errors"]
40
+ raise "Failed. #{r["errors"]}"
41
+ else
42
+ r["name"]
43
+ end
44
+ end
45
+
46
+ def image_url(name, options = nil)
47
+ parts = ['x', @bucket]
48
+
49
+ if options
50
+ if options.is_a?(String)
51
+ parts << options
52
+ elsif options.is_a?(Array)
53
+ parts += options
54
+ else
55
+ raise "Invalid options."
56
+ end
57
+ end
58
+
59
+ # add image name
60
+ parts << name
61
+
62
+ # build path
63
+ path = parts.join('/')
64
+
65
+ # add signature
66
+ if @secret
67
+ signature_string = [path, @secret].join('/')
68
+ signature = Digest::SHA1.hexdigest(signature_string)[0..15]
69
+ path << "-#{signature}"
70
+ end
71
+
72
+ @base_url + path
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module Imaginary
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imaginary::Client do
4
+ let :client do
5
+ Imaginary::Client.new('http://imaginary.test.org/',
6
+ bucket: 'some_bucket', username: 'some_user', password: '123secret', secret: 'abcdefg')
7
+ end
8
+
9
+ let :client_without_secret do
10
+ Imaginary::Client.new('http://imaginary.test.org/',
11
+ bucket: 'some_bucket', username: 'some_user', password: '123secret')
12
+ end
13
+
14
+ subject { client }
15
+
16
+ describe '#add_image_from_url' do
17
+ it "should add the image to the server and return its name" do
18
+ FakeWeb.register_uri :post,
19
+ "http://some_user:123secret@imaginary.test.org//api/buckets/some_bucket/images.json",
20
+ body: '{"name": "kitten"}',
21
+ content_type: 'application/json; charset=utf-8'
22
+
23
+ client.add_image_from_url('http://placekitten.com/200/300', 'kitten').should == 'kitten'
24
+ end
25
+ end
26
+
27
+ describe '#add_image_from_file' do
28
+ it "should add the image to the server and return its name" do
29
+ FakeWeb.register_uri :post,
30
+ "http://some_user:123secret@imaginary.test.org//api/buckets/some_bucket/images.json",
31
+ body: '{"name": "hmans"}',
32
+ content_type: 'application/json; charset=utf-8'
33
+
34
+ client.add_image_from_file(File.new('./spec/files/hmans.jpg'), 'hmans').should == 'hmans'
35
+ end
36
+ end
37
+
38
+ describe '#image_url' do
39
+ context 'when client is configured to use a secret' do
40
+ subject { client.image_url('some_image') }
41
+ it { should == 'http://imaginary.test.org/x/some_bucket/some_image-0bc24ea1f1ab4e6f' }
42
+ end
43
+
44
+ context "when client is configured to not use a secret" do
45
+ subject { client_without_secret.image_url('some_image') }
46
+ it { should == 'http://imaginary.test.org/x/some_bucket/some_image' }
47
+ end
48
+ end
49
+ end
Binary file
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'fakeweb'
5
+
6
+ require 'imaginary'
7
+
8
+ FakeWeb.allow_net_connect = false
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imaginary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hendrik Mans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httmultiparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: fakeweb
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Client gem for Imaginary.
63
+ email:
64
+ - hendrik@mans.de
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - imaginary-client.gemspec
76
+ - lib/imaginary.rb
77
+ - lib/imaginary/version.rb
78
+ - spec/client_spec.rb
79
+ - spec/files/hmans.jpg
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/hmans/imaginary
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Client gem for Imaginary.
105
+ test_files:
106
+ - spec/client_spec.rb
107
+ - spec/files/hmans.jpg
108
+ - spec/spec_helper.rb
109
+ has_rdoc: