snapshot-ruby 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ Snapshot
2
+ ========
3
+
4
+ The Snapshot gem provides an nice, ActiveRecord-ish interface to the
5
+ [Snapshot](http://snapshothq.com/) API.
6
+
7
+ * [Fork the code on Github](https://github.com/jeremyboles/snapshot-ruby)
8
+
9
+ Installation
10
+ ------------
11
+
12
+ gem install snapshot-ruby
13
+
14
+ Using the gem
15
+ -------------
16
+
17
+ require 'rubygems'
18
+ require 'snapshot'
19
+
20
+ ### Connecting the Client ###
21
+
22
+ Snapshot.configure do |config|
23
+ config.domain = 'yourdomain.snapshothq.com'
24
+ config.access_key = 'your access key'
25
+ config.secret_key = 'your secret key'
26
+ end
27
+
28
+ # or with a hash
29
+ Snapshot.configure({:access_key => ...})
30
+
31
+ ### Typical Usage ###
32
+
33
+ The API for the gem was modeled after ActiveRecord to make it easy to pick up.
34
+ Uploading a video to Snapshot is done with via the `create` method:
35
+
36
+ img = Snapshot::Image.create('example.jpg')
37
+ # => #<Snapshot::Image:0x00000100a51080>
38
+
39
+ img.id
40
+ # => 'adf4675e46a6078c1bbc6a663a47e1e56e4622e5'
41
+
42
+ img.created_at
43
+ # => 2011-01-13 15:30:34 -0600
44
+
45
+ img.format
46
+ # => "JPEG"
47
+
48
+ img.width
49
+ # => 720
50
+
51
+ img.height
52
+ # => 640
53
+
54
+ Once and image has been uploaded, you can reference and process the image based on its `id`:
55
+
56
+ img = Snapshot::Image.find('adf4675e46a6078c1bbc6a663a47e1e56e4622e5')
57
+ # => #<Snapshot::Image:0x000001008a3df0>
58
+
59
+ img = Snapshot::Image.find('wrong id')
60
+ # => nil
61
+
62
+ To delete an image, call its `destroy` method:
63
+
64
+ img.destroy
65
+ # => true
66
+
@@ -0,0 +1,40 @@
1
+ require 'uri'
2
+
3
+ module Snapshot
4
+ class Connection
5
+ attr_accessor :access_key, :domain, :secret_key
6
+
7
+ # Creates a new Connection object.
8
+ #
9
+ # ==== Parameters
10
+ #
11
+ # * +opts+ - (optional) String or Hash
12
+ #
13
+ def initialize(opts={})
14
+ if opts.is_a?(Hash)
15
+ opts.each do |m,v|
16
+ self.send("#{m}=", v)
17
+ end
18
+ else
19
+ uri = URI.parse(opts)
20
+ self.access_key, self.domain, self.secret_key = uri.user, uri.host, uri.password
21
+ self.domain << ":#{uri.port}" unless uri.port == 80
22
+ end
23
+ end
24
+
25
+ # Returns an authenticated RestClient::Resource
26
+ #
27
+ def resource
28
+ @resource ||= RestClient::Resource.new(self.url, {
29
+ :user => self.access_key,
30
+ :password => self.secret_key
31
+ })
32
+ end
33
+
34
+ # Returns a URL to Snapshot for the connection
35
+ #
36
+ def url
37
+ "http://#{domain}/"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,115 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+
4
+ module Snapshot
5
+ class Image
6
+ attr_accessor :created_at, :filesize, :format, :height, :id, :width
7
+
8
+ class << self
9
+ # Uploads a file to the connected Snapshot account.
10
+ #
11
+ # ==== Parameters
12
+ #
13
+ # * +file+ - File or path to a file
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # # With a path:
18
+ # img = Snapshot::Image.create('example.jpg')
19
+ #
20
+ # # With a file:
21
+ # file = File.open('example.jpg')
22
+ # img = Snapshot::Image.create(file)
23
+ #
24
+ def create(file)
25
+ file = File.open(file) if file.is_a?(String)
26
+ result = Snapshot.connection.resource.post(:file => file)
27
+ info = JSON.parse(result)['file']
28
+
29
+ self.new.tap do |img|
30
+ img.created_at = Time.at(info['uploaded_at'].to_i)
31
+ img.filesize = info['size'].to_i
32
+ img.format = info['type']
33
+ img.height = info['height'].to_i
34
+ img.id = info['id']
35
+ img.width = info['width'].to_i
36
+ end
37
+ end
38
+
39
+ # Deletes a file from Snapshot.
40
+ #
41
+ # ==== Parameters
42
+ #
43
+ # * +id+ - The ID of the image
44
+ #
45
+ # ==== Examples
46
+ #
47
+ # Snapshot::Image.destroy('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
48
+ #
49
+ def destroy(id)
50
+ find(id).destroy
51
+ end
52
+
53
+ # Finds a file on the connected Snapshot account.
54
+ #
55
+ # ==== Parameters
56
+ #
57
+ # * +id+ - The ID of the image
58
+ #
59
+ # ==== Examples
60
+ #
61
+ # Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
62
+ #
63
+ def find(id)
64
+ result = Snapshot.connection.resource["#{id}"].get
65
+ info = JSON.parse(result)
66
+
67
+ self.new.tap do |img|
68
+ img.created_at = Time.at(info['uploaded_at'].to_i)
69
+ img.filesize = info['size'].to_i
70
+ img.format = info['type']
71
+ img.height = info['height'].to_i
72
+ img.id = info['id']
73
+ img.width = info['width'].to_i
74
+ end
75
+ rescue RestClient::ResourceNotFound
76
+ nil
77
+ end
78
+ end
79
+
80
+ # Removes the image from the connect Snapshot account
81
+ #
82
+ # ==== Examples
83
+ #
84
+ # img = Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
85
+ # img.destroy
86
+ #
87
+ def destroy
88
+ Snapshot.connection.resource["#{id}"].delete
89
+ true
90
+ rescue RestClient::ResourceNotFound
91
+ nil
92
+ end
93
+
94
+ # Return the URL of the image
95
+ #
96
+ # ==== Examples
97
+ #
98
+ # img = Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
99
+ # img.url # => http://subdomain.snapshothq.com/a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a.jpg
100
+ #
101
+ def url(&block)
102
+ Snapshot::URL.new(id, extension, &block)
103
+ end
104
+
105
+ private
106
+
107
+ def extension
108
+ case self.format
109
+ when 'JPEG' then '.jpg'
110
+ when 'PNG' then '.png'
111
+ end
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,22 @@
1
+ module Snapshot
2
+ class URL
3
+ attr_reader :extension, :id
4
+
5
+ def initialize(id, extension, &block)
6
+ @id = id
7
+ @extension = extension
8
+ @options = []
9
+ tap(&block) if block_given?
10
+ end
11
+
12
+ def method_missing(process, *options)
13
+ @options << [process.to_s, options]
14
+ end
15
+
16
+ def to_s
17
+ @options = @options.flatten
18
+ @options << '' unless @options.empty?
19
+ Snapshot.connection.url << @options.join('/') << id << '.' << extension
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Snapshot
2
+ VERSION = '0.1'
3
+ end
data/lib/snapshot.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'snapshot/connection'
2
+ require 'snapshot/image'
3
+ require 'snapshot/url'
4
+ require 'snapshot/version'
5
+
6
+ module Snapshot
7
+ extend self
8
+
9
+ # Configures and connects to Snapshot.
10
+ #
11
+ # ==== Parameters
12
+ #
13
+ # * +opts+ - String or Hash
14
+ # * +block+ - The block is optional and is passed a Connection object
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # # Using a String:
19
+ # Snapshot.configure('http://access_key:secret_ket@subdomain.snapshothq.com/')
20
+ #
21
+ # # Using a Hash:
22
+ # Snapshot.configure(:access_key => 'access_key', :domain => 'subdomain.snapshothq.com', :secret_key => 'secret_key')
23
+ #
24
+ # # Using a block:
25
+ # Snapshot.configure do |config|
26
+ # config.access_key = 'access_key'
27
+ # config.domain = 'subdomain.snapshothq.com'
28
+ # config.secret_key = 'secret_key
29
+ # end
30
+ #
31
+ def configure(opts={})
32
+ if opts
33
+ connect!(opts)
34
+ else
35
+ yield @connection = Connection.new
36
+ end
37
+ end
38
+
39
+ # Connects the library to Snapshot. If no connection options are given, the
40
+ # method will try to use the environment variable <code>SNAPSHOT_URL</code>.
41
+ #
42
+ # ==== Parameters
43
+ #
44
+ # * +opts+ - (optional) String or Hash
45
+ #
46
+ # ==== Examples
47
+ #
48
+ # # Using a String:
49
+ # Snapshot.connect!('http://access_key:secret_ket@subdomain.snapshothq.com/')
50
+ #
51
+ # # Using a Hash:
52
+ # Snapshot.connect!(:access_key => 'access_key', :domain => 'subdomain.snapshothq.com', :secret_key => 'secret_key')
53
+ #
54
+ def connect!(opts=nil)
55
+ opts = ENV['SNAPSHOT_URL'] if opts.nil?
56
+ @connection = Connection.new(opts)
57
+ end
58
+
59
+ # Returns the connection for the library. Will raise an error if the
60
+ # library has not been connected via the <code>connect!</code> method
61
+ #
62
+ def connection
63
+ raise 'Not connected. Please connect! first.' unless @connection
64
+ @connection
65
+ end
66
+
67
+ # Terminates the connection to Snapshot
68
+ def disconnect!
69
+ @connection = nil
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snapshot-ruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Jeremy Boles
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-01-31 00:00:00 -06:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: json
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ - 6
31
+ version: 1.4.6
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rest-client
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 6
45
+ - 1
46
+ version: 1.6.1
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: fakeweb
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 3
60
+ version: "1.3"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - "="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 2
73
+ - 4
74
+ - 0
75
+ version: 2.4.0
76
+ type: :development
77
+ version_requirements: *id004
78
+ description: Snap is a very simple image storage and processing services for developers.
79
+ email:
80
+ - jeremy@jeremyboles.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files: []
86
+
87
+ files:
88
+ - lib/snapshot/connection.rb
89
+ - lib/snapshot/image.rb
90
+ - lib/snapshot/url.rb
91
+ - lib/snapshot/version.rb
92
+ - lib/snapshot.rb
93
+ - README.md
94
+ has_rdoc: true
95
+ homepage: https://github.com/jeremyboles/snapshot-ruby
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 1
118
+ - 3
119
+ - 7
120
+ version: 1.3.7
121
+ requirements: []
122
+
123
+ rubyforge_project: snapshot-ruby
124
+ rubygems_version: 1.3.7
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Ruby wrapper for storing and manipulating images via Snapshot.
128
+ test_files: []
129
+