all_seeing_pi 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "mocha"
24
24
  spec.add_development_dependency "pry"
25
- spec.add_development_dependency "vcr"
26
- spec.add_development_dependency "webmock"
27
- spec.add_runtime_dependency "aws-sdk"
25
+ spec.add_runtime_dependency "fog"
26
+ spec.add_runtime_dependency "phashion"
27
+ spec.add_runtime_dependency "httparty"
28
28
  end
data/capture.sh CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
 
3
- DATE=$(date +%Y-%m-%d_%H-%M-%S-%N)
3
+ DATE=$(date +%Y-%m-%d-%H-%M-%S-%N)
4
4
 
5
5
  raspistill -vf -hf -t 0 -o $DATE.jpg
6
6
  echo $DATE.jpg
@@ -1,5 +1,5 @@
1
1
  require "all_seeing_pi/version"
2
- require "all_seeing_pi/golem"
2
+ require "all_seeing_pi/gollum"
3
3
  require "all_seeing_pi/camera"
4
4
  require "all_seeing_pi/uploader"
5
5
  require "all_seeing_pi/configuration"
@@ -19,10 +19,14 @@ module AllSeeingPi
19
19
  end
20
20
 
21
21
  def self.watch
22
- golem = AllSeeingPi::Golem.new
22
+ golem = AllSeeingPi::Gollum.new
23
23
 
24
24
  loop do
25
25
  golem.spy
26
26
  end
27
27
  end
28
+
29
+ def self.report(msg)
30
+ puts msg unless ENV['ALL_SEEING_PI_ENV'] == 'test'
31
+ end
28
32
  end
@@ -1,7 +1,14 @@
1
1
  module AllSeeingPi
2
2
  class Camera
3
+ attr_reader :script
4
+
5
+ def initialize
6
+ path = '../../../capture.sh'
7
+ @script = AllSeeingPi.config[:capture_script] || File.expand_path(path, __FILE__)
8
+ end
9
+
3
10
  def capture
4
- `./capture.sh`.strip
11
+ `#{script}`.strip
5
12
  end
6
13
  end
7
14
  end
@@ -1,6 +1,6 @@
1
1
  module AllSeeingPi
2
2
  class Configuration
3
- attr_accessor :aws_key, :aws_secret, :redis_host, :redis_password
3
+ attr_accessor :aws_key, :aws_secret, :palantir_url, :directory_name, :capture_script, :palantir_access_token
4
4
 
5
5
  def initialize
6
6
  reset!
@@ -0,0 +1,52 @@
1
+ require 'all_seeing_pi/camera'
2
+ require 'all_seeing_pi/uploader'
3
+ require 'phashion'
4
+ require 'httparty'
5
+
6
+ module AllSeeingPi
7
+ class Gollum
8
+ attr_reader :camera, :uploader
9
+
10
+ def initialize
11
+ @camera = AllSeeingPi::Camera.new
12
+ @uploader = AllSeeingPi::Uploader.new
13
+ end
14
+
15
+ def spy
16
+ image_path = @camera.capture
17
+ return unless File.exists?(image_path)
18
+
19
+ AllSeeingPi.report "Captured #{image_path}"
20
+
21
+ url = store_image(image_path)
22
+ phash = get_phash(image_path)
23
+ send_to_palantir(image_path, phash, url)
24
+
25
+ FileUtils.rm(image_path)
26
+ end
27
+
28
+ def get_phash(image_path)
29
+ Phashion::Image.new(image_path).fingerprint
30
+ end
31
+
32
+ def store_image(image_path)
33
+ @uploader.upload(image_path)
34
+ end
35
+
36
+ def send_to_palantir(image_path, phash, url)
37
+ data = {
38
+ :name => File.basename(image_path),
39
+ :phash => phash,
40
+ :url => url,
41
+ :directory_name => AllSeeingPi.config[:directory_name]
42
+ }
43
+
44
+ HTTParty.post(
45
+ "#{AllSeeingPi.config[:palantir_url]}/api/images",
46
+ :query => { :image => data, :access_token => AllSeeingPi.config[:palantir_access_token]}
47
+ )
48
+
49
+ AllSeeingPi.report "Sent to Palantir - name: #{data[:name]}, phash: #{data[:phash]}, directory: #{data[:directory_name]}"
50
+ end
51
+ end
52
+ end
@@ -1,36 +1,40 @@
1
- require 'aws-sdk'
1
+ require 'fog'
2
2
 
3
3
  module AllSeeingPi
4
4
  class Uploader
5
- BUCKET_PREFIX = 'all_seeing_pi'
5
+ DIRECTORY_PREFIX = 'all-seeing-pi'
6
6
 
7
- attr_reader :s3
7
+ attr_reader :client
8
8
 
9
9
  def initialize
10
- @s3 = AWS::S3.new
11
- require 'pry'; binding.pry if $debug
10
+ @client = Fog::Storage.new(
11
+ provider: 'AWS',
12
+ aws_access_key_id: AllSeeingPi.config[:aws_key],
13
+ aws_secret_access_key: AllSeeingPi.config[:aws_secret]
14
+ )
12
15
  end
13
16
 
14
17
  def upload(image_path)
15
- buckets = s3.buckets
18
+ directories = client.directories
16
19
 
17
- bucket_name = fetch_or_create_bucket_name(buckets)
18
- buckets.create(bucket_name) unless buckets[bucket_name].exists?
20
+ directory_name = fetch_or_create_directory_name(directories)
21
+ directory = directories.get(directory_name) || directories.create(key: directory_name)
19
22
 
20
23
  filename = File.basename(image_path)
21
- buckets[bucket_name].objects[filename].write(:file => image_path)
24
+ image = directory.files.create(key: filename, body: File.open(image_path), public: true)
25
+ image.public_url
22
26
  end
23
27
 
24
- def fetch_or_create_bucket_name(buckets)
25
- bucket_name = buckets.map do |bucket|
26
- name = bucket.name
27
- name if name.match /all_seeing_pi/
28
+ def fetch_or_create_directory_name(directories)
29
+ directory_name = directories.map do |directory|
30
+ key = directory.key
31
+ key if key.match /#{DIRECTORY_PREFIX}/
28
32
  end.compact.first
29
33
 
30
- bucket_name ||= "#{BUCKET_PREFIX}_#{bucket_id}"
34
+ AllSeeingPi.config[:directory_name] ||= "#{DIRECTORY_PREFIX}-#{directory_id}"
31
35
  end
32
36
 
33
- def bucket_id
37
+ def directory_id
34
38
  Time.now.to_f.to_s.split('.').join.to_i
35
39
  end
36
40
  end
@@ -1,3 +1,3 @@
1
1
  module AllSeeingPi
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ class GollumTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ Fog.mock!
7
+ AllSeeingPi.config.load_config_from_file('test/all_seeing_pi.yml')
8
+
9
+ @fixture = 'test/fixtures/eye_of_sauron.jpg'
10
+ @filename = File.basename(@fixture)
11
+ @golem = AllSeeingPi::Gollum.new
12
+ @phash = @golem.get_phash(@fixture)
13
+ @palantir_url = 'http://all-seeing-pi.com/api/images'
14
+ @public_url = 'http://public-url.com'
15
+
16
+ @image_data = {
17
+ :name => @filename,
18
+ :phash => @phash,
19
+ :url => @public_url,
20
+ :directory_name => AllSeeingPi.config[:directory_name]
21
+ }
22
+ @token = AllSeeingPi.config[:palantir_access_token]
23
+ end
24
+
25
+ def test_spy_sends_capture_to_the_camera
26
+ @golem.stubs(:store_image)
27
+ FileUtils.stubs(:rm)
28
+ HTTParty.stubs(:post)
29
+
30
+ @golem.camera.expects(:capture).returns(@fixture)
31
+ @golem.spy
32
+ end
33
+
34
+ def test_store_image
35
+ @golem.camera.stubs(:capture).returns(@fixture)
36
+ FileUtils.stubs(:rm)
37
+ HTTParty.stubs(:post)
38
+
39
+ @golem.uploader.expects(:upload).with(@fixture)
40
+ @golem.spy
41
+ end
42
+
43
+ def test_spy_deletes_the_image
44
+ @golem.camera.stubs(:capture).returns(@fixture)
45
+ @golem.stubs(:store_image)
46
+ HTTParty.stubs(:post)
47
+
48
+ FileUtils.expects(:rm).with(@fixture)
49
+ @golem.spy
50
+ end
51
+
52
+ def test_spy_calculates_the_phash
53
+ @golem.camera.stubs(:capture).returns(@fixture)
54
+ FileUtils.stubs(:rm)
55
+ HTTParty.stubs(:post)
56
+
57
+ image = mock(:fingerprint)
58
+ Phashion::Image.expects(:new).with(@fixture).returns(image)
59
+ @golem.spy
60
+ end
61
+
62
+ def test_spy_sends_palantir_the_image_details
63
+ @golem.camera.stubs(:capture).returns(@fixture)
64
+ @golem.stubs(:store_image).returns(@public_url)
65
+ FileUtils.stubs(:rm)
66
+ HTTParty.stubs(:post)
67
+
68
+ @golem.expects(:send_to_palantir).with(@fixture, @phash, @public_url)
69
+ @golem.spy
70
+ end
71
+
72
+ def test_send_to_palantir_uses_uri_from_config
73
+ HTTParty.expects(:post).with( @palantir_url, { :query => { :image => @image_data, :access_token => @token } } )
74
+ @golem.send_to_palantir(@fixture, @phash, @public_url)
75
+ end
76
+
77
+ def test_send_to_palantir_posts_the_image_data
78
+ HTTParty.expects(:post).with( @palantir_url, { :query => { :image => @image_data, :access_token => @token } } )
79
+ @golem.send_to_palantir(@fixture, @phash, @public_url)
80
+ end
81
+ end
@@ -2,18 +2,6 @@ require 'minitest/autorun'
2
2
 
3
3
  Bundler.require
4
4
 
5
- require 'vcr'
6
5
  require 'mocha'
7
6
 
8
- VCR.configure do |c|
9
- c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
10
- c.hook_into :webmock
11
- end
12
-
13
- AllSeeingPi.config.load_config_from_file('test/all_seeing_pi.yml')
14
-
15
- AWS.config(
16
- access_key_id: AllSeeingPi.config[:aws_key],
17
- secret_access_key: AllSeeingPi.config[:aws_secret]
18
- )
19
7
  ENV['ALL_SEEING_PI_ENV'] = 'test'
@@ -2,24 +2,28 @@ require 'test_helper'
2
2
 
3
3
  class UploaderTest < MiniTest::Unit::TestCase
4
4
  def setup
5
+ Fog.mock!
6
+ AllSeeingPi.config.load_config_from_file('test/all_seeing_pi.yml')
7
+
5
8
  @fixture = 'test/fixtures/eye_of_sauron.jpg'
6
9
  @filename = File.basename(@fixture)
7
10
  @uploader = AllSeeingPi::Uploader.new
8
- @bucket_name = AllSeeingPi::Uploader::BUCKET_PREFIX
9
11
  end
10
12
 
11
13
  def test_upload
12
- VCR.use_cassette('uploader_test_upload') do
13
- @uploader.upload(@fixture)
14
- assert @uploader.s3.buckets[@bucket_name].objects[@filename].exists?, "#{@fixture} was not uploaded."
15
- end
14
+ @uploader.upload(@fixture)
15
+ assert @uploader.client.directories.get(AllSeeingPi.config[:directory_name]).files.head(@filename)
16
+ end
17
+
18
+ def test_upload_returns_public_url
19
+ regex = /#{AllSeeingPi::Uploader::DIRECTORY_PREFIX}.*\/#{@filename}/
20
+ public_url = @uploader.upload(@fixture)
21
+ assert public_url.match(regex), "Expected #{regex.inspect} to match #{public_url}"
16
22
  end
17
23
 
18
- def test_fetch_or_create_bucket_name
19
- VCR.use_cassette('uploader_test_fetch_or_create_bucket_name') do
20
- name = @uploader.fetch_or_create_bucket_name(@uploader.s3.buckets)
21
- assert_match /#{@bucket_name}/, name
22
- end
24
+ def test_fetch_or_create_directory_name
25
+ name = @uploader.fetch_or_create_directory_name(@uploader.client.directories)
26
+ assert_match /#{AllSeeingPi::Uploader::DIRECTORY_PREFIX}/, name
23
27
  end
24
28
 
25
29
  def test_uploader_uses_credentials_from_config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: all_seeing_pi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-05 00:00:00.000000000 Z
12
+ date: 2014-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -76,14 +76,14 @@ dependencies:
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  - !ruby/object:Gem::Dependency
79
- name: vcr
79
+ name: fog
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
83
  - - ! '>='
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
- type: :development
86
+ type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
@@ -92,14 +92,14 @@ dependencies:
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
94
  - !ruby/object:Gem::Dependency
95
- name: webmock
95
+ name: phashion
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
99
  - - ! '>='
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
- type: :development
102
+ type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
@@ -108,7 +108,7 @@ dependencies:
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  - !ruby/object:Gem::Dependency
111
- name: aws-sdk
111
+ name: httparty
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
@@ -142,15 +142,15 @@ files:
142
142
  - lib/all_seeing_pi.rb
143
143
  - lib/all_seeing_pi/camera.rb
144
144
  - lib/all_seeing_pi/configuration.rb
145
- - lib/all_seeing_pi/golem.rb
145
+ - lib/all_seeing_pi/gollum.rb
146
146
  - lib/all_seeing_pi/uploader.rb
147
147
  - lib/all_seeing_pi/version.rb
148
148
  - test/fixtures/eye_of_sauron.jpg
149
149
  - test/test_all_seeing_pi.rb
150
150
  - test/test_camera.rb
151
151
  - test/test_configuration.rb
152
+ - test/test_gollum.rb
152
153
  - test/test_helper.rb
153
- - test/test_palantir.rb
154
154
  - test/test_uploader.rb
155
155
  homepage: http://github.com/1337807/all_seeing_pi
156
156
  licenses:
@@ -182,7 +182,7 @@ test_files:
182
182
  - test/test_all_seeing_pi.rb
183
183
  - test/test_camera.rb
184
184
  - test/test_configuration.rb
185
+ - test/test_gollum.rb
185
186
  - test/test_helper.rb
186
- - test/test_palantir.rb
187
187
  - test/test_uploader.rb
188
188
  has_rdoc:
@@ -1,31 +0,0 @@
1
- require 'all_seeing_pi/camera'
2
- require 'all_seeing_pi/uploader'
3
-
4
- module AllSeeingPi
5
- class Golem
6
- attr_reader :camera, :uploader
7
-
8
- def initialize
9
- @camera = AllSeeingPi::Camera.new
10
- @uploader = AllSeeingPi::Uploader.new
11
- end
12
-
13
- def spy
14
- image_path = @camera.capture
15
- return unless File.exists?(image_path)
16
-
17
- report "Captured #{image_path}"
18
-
19
- store_image(image_path)
20
- FileUtils.rm(image_path)
21
- end
22
-
23
- def report(msg)
24
- puts msg unless ENV['ALL_SEEING_PI_ENV'] == 'test'
25
- end
26
-
27
- def store_image(image_path)
28
- @uploader.upload(image_path)
29
- end
30
- end
31
- end
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
-
3
- class GolemTest < MiniTest::Unit::TestCase
4
- def setup
5
- @fixture = 'test/fixtures/eye_of_sauron.jpg'
6
- @golem = AllSeeingPi::Golem.new
7
- end
8
-
9
- def test_spy_sends_capture_to_the_camera
10
- @golem.stubs(:store_image)
11
- FileUtils.stubs(:rm)
12
-
13
- @golem.camera.expects(:capture).returns(@fixture)
14
- @golem.spy
15
- end
16
-
17
- def test_store_image
18
- @golem.camera.stubs(:capture).returns(@fixture)
19
- FileUtils.stubs(:rm)
20
-
21
- @golem.uploader.expects(:upload).with(@fixture)
22
- @golem.spy
23
- end
24
-
25
- def test_spy_deletes_the_image
26
- @golem.camera.stubs(:capture).returns(@fixture)
27
- @golem.stubs(:store_image)
28
-
29
- FileUtils.expects(:rm).with(@fixture)
30
- @golem.spy
31
- end
32
- end