mime-typer 0.1 → 0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19b714b76fb015b1b2c44eb1ffa0288b1135a4bf
4
- data.tar.gz: 5e6fb027e2c2224c5014e41cf009e548cd3a8550
3
+ metadata.gz: 2854587c48b7973c8f88bfa17d732ab1113fac1c
4
+ data.tar.gz: 8072db2297467952ab51ee30989f12e69b862cf9
5
5
  SHA512:
6
- metadata.gz: 03b530b087f9a800765ded623b6249c28192043b91b027d653eba8e99c0c45f0205354a4d6957a1d0f1a0686c253e6a8818c5e43e8844ab0ea2978bcfe9b9a4b
7
- data.tar.gz: b139ddbfc4322087e0cbb9aaabdcaf7561563c5897eaa0c898874e5d13ac0b0f810d71c41af6c74994a5d385852d7fed885ec4af9da1688c73614e016b98f04e
6
+ metadata.gz: 9694fb0a7685436fd57d83dce29710f100a947daa4d76e07cb9c83f32fe96749d6a69ba1cb286b73d1275f66c28df42e4a80e26e14ccd35baa6ed362523dcf0b
7
+ data.tar.gz: 3ac3d8ff70d6a6e94b74c3ea3edfb70ac3acd881ae6c9994910277549a48aa744eed23a790015d76d1786d7783090c4ae1b0d442b64fd4f3910f9b32c625786d
data/lib/mime-typer.rb CHANGED
@@ -1,8 +1,75 @@
1
+ require 'net/http'
2
+ require 'tempfile'
3
+
4
+ class Net::HTTPResponse
5
+ attr_reader :socket
6
+ end
7
+
1
8
  module MIME
9
+ # Public: A simple class to detect mime type of a file, using libmagic
10
+ # You can detect both local and remote file types
11
+ #
12
+ # Examples
13
+ #
14
+ # MIME::Typer.detect('/my/awesome/image/path.jpg')
15
+ # # => 'image/jpeg'
2
16
  class Typer
17
+ # Public: Detects the MIME type of a file, given a path or a file
18
+ #
19
+ # file - The String path or the File to be detected
20
+ #
21
+ # Examples
22
+ #
23
+ # detect('/my/awesome/image/path.jpg')
24
+ # # => 'image/jpeg'
25
+ #
26
+ # Returns a String containing the MIME Type.
3
27
  def self.detect(file)
4
28
  path = file.is_a?(File) ? file.path : file
5
29
  `file --mime-type #{path} | awk '{ print $(NF) }'`.strip
6
30
  end
31
+
32
+ # Public: Detects the MIME type of a remote file, given an url
33
+ #
34
+ # uri - The String uri of the file
35
+ #
36
+ # Examples
37
+ #
38
+ # remote('http://my.awesome.site.com/my_image.jpg')
39
+ # # => 'image/jpeg'
40
+ #
41
+ # Returns a String containing the MIME Type.
42
+ def self.remote(uri)
43
+ temp = Tempfile.new('mime-typer')
44
+ temp.binmode
45
+ temp.write(load_remote(uri))
46
+ temp.close
47
+ detect temp.path
48
+ end
49
+
50
+ # Internal: Loads the first 200 bytes of a file
51
+ #
52
+ # uri - The String uri of the file
53
+ # bytes - The Number of bytes to be loaded (default: 200)
54
+ #
55
+ # Examples
56
+ #
57
+ # remote('http://my.awesome.site.com/my_pdf.jpg')
58
+ # # => 'PDF 1.4 etc...'
59
+ #
60
+ # Returns a binary String containing the first 200 bytes of the file.
61
+ def self.load_remote(uri, bytes = 200)
62
+ result = ''
63
+ uri = URI(uri)
64
+ Net::HTTP.start(uri.host, uri.port) do |http|
65
+ request = Net::HTTP::Get.new(uri.request_uri)
66
+ http.request(request) do |response|
67
+ result = response.socket.read(bytes)
68
+ http.finish
69
+ end
70
+ end rescue nil
71
+ result
72
+ end
73
+ private_class_method :load_remote
7
74
  end
8
75
  end
data/mime-typer.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  lib = 'mime-typer'
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'mime-typer'
4
- s.version = '0.1'
5
- s.date = '2014-07-31'
4
+ s.version = '0.2'
5
+ s.date = '2014-08-04'
6
6
  s.summary = 'MIME Typer'
7
7
  s.description = 'A gem to detect mime type of a file, using libmagic.'
8
8
  s.authors = ['Piero Dotti']
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.license = 'MIT'
12
12
  s.extensions = %w(dipendences/Rakefile)
13
13
 
14
- s.files = %w(Gemfile LICENSE README.md Rakefile dog.jpg)
14
+ s.files = %w(Gemfile LICENSE README.md Rakefile test.png)
15
15
  s.files << "#{lib}.gemspec"
16
16
  s.files += Dir.glob("lib/**/*.rb")
17
17
  s.files += Dir.glob("test/**/*.rb")
data/test.png ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime-typer
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piero Dotti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-31 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -50,9 +50,9 @@ files:
50
50
  - README.md
51
51
  - Rakefile
52
52
  - dipendences/Rakefile
53
- - dog.jpg
54
53
  - lib/mime-typer.rb
55
54
  - mime-typer.gemspec
55
+ - test.png
56
56
  homepage: https://github.com/ProGM/mime-typer
57
57
  licenses:
58
58
  - MIT
data/dog.jpg DELETED
Binary file