imagga 0.0.3 → 0.0.5

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.
Files changed (41) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +28 -1
  3. data/imagga.gemspec +1 -0
  4. data/lib/imagga.rb +8 -3
  5. data/lib/imagga/client.rb +12 -34
  6. data/lib/imagga/commands.rb +42 -0
  7. data/lib/imagga/core_client.rb +25 -0
  8. data/lib/imagga/exceptions.rb +3 -0
  9. data/lib/imagga/image.rb +59 -2
  10. data/lib/imagga/image_or_url_parametizer.rb +21 -0
  11. data/lib/imagga/options.rb +74 -0
  12. data/lib/imagga/parametizer.rb +18 -0
  13. data/lib/imagga/rank_color_parametizer.rb +17 -0
  14. data/lib/imagga/resolution_parametizer.rb +17 -0
  15. data/lib/imagga/result_builder.rb +27 -0
  16. data/lib/imagga/version.rb +1 -1
  17. data/spec/fixtures/crop_response.txt +62 -0
  18. data/spec/fixtures/extract_response.txt +253 -0
  19. data/spec/fixtures/rank_response.txt +12 -0
  20. data/spec/integration/crop_spec.rb +60 -0
  21. data/spec/integration/extract_spec.rb +51 -0
  22. data/spec/integration/rank_spec.rb +103 -0
  23. data/spec/lib/commands_spec.rb +70 -0
  24. data/spec/lib/core_client_spec.rb +43 -0
  25. data/spec/lib/crop_info_spec.rb +15 -0
  26. data/spec/lib/crop_options_spec.rb +24 -0
  27. data/spec/lib/crop_result_builder_spec.rb +4 -0
  28. data/spec/lib/extraction_options_spec.rb +16 -51
  29. data/spec/lib/image_crop_spec.rb +34 -0
  30. data/spec/lib/image_or_url_parametizer_spec.rb +65 -0
  31. data/spec/lib/parametizer_spec.rb +42 -0
  32. data/spec/lib/rank_color_parametizer_spec.rb +27 -0
  33. data/spec/lib/rank_color_spec.rb +51 -0
  34. data/spec/lib/rank_options_spec.rb +28 -0
  35. data/spec/lib/rank_result_builder_spec.rb +1 -12
  36. data/spec/lib/resolution_parametizer_spec.rb +60 -0
  37. metadata +63 -28
  38. data/lib/imagga/extract_options.rb +0 -74
  39. data/lib/imagga/extract_result_builder.rb +0 -11
  40. data/lib/imagga/rank_result_builder.rb +0 -9
  41. data/spec/lib/client_spec.rb +0 -352
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require 'fake_web'
3
+
4
+ describe "Multicolor search" do
5
+
6
+ let(:base_uri) { 'http://example.com' }
7
+ let(:fake_service_url) { 'http://example.com/colorsearchserver.php' }
8
+ let(:rank_response) { IO.read('./spec/fixtures/rank_response.txt') }
9
+ let(:failed_signature_response) { '{"error_code":3,"error_message":"Invalid signature"}' }
10
+
11
+ subject { Imagga::Client.new(api_key: '123456', api_secret: 'secret', base_uri: base_uri) }
12
+
13
+ context "with successful request" do
14
+ before do
15
+ FakeWeb.register_uri(:post, fake_service_url, body: rank_response)
16
+ end
17
+
18
+ it "searches images by color" do
19
+ subject.rank(
20
+ colors: [
21
+ Imagga::RankColor.new(percent: 60, r: 10, g: 20, b: 30),
22
+ Imagga::RankColor.new(percent: 20, hex: '#ff00ff'),
23
+ '39,12,34,123'
24
+ ],
25
+ type: :overall,
26
+ dist: 3000,
27
+ count: 10
28
+ ).size.should == 2
29
+ end
30
+ end
31
+
32
+ context "with missing colors in params" do
33
+ it "throws an error" do
34
+ expect {
35
+ subject.rank(
36
+ type: :overall,
37
+ dist: 3000,
38
+ count: 10
39
+ ) }.to raise_error(ArgumentError, 'colors is missing')
40
+ end
41
+ end
42
+
43
+ context "with missing colors in params" do
44
+ it "throws an error" do
45
+ expect {
46
+ subject.rank(
47
+ colors: [
48
+ Imagga::RankColor.new(percent: 60, r: 10, g: 20, b: 30),
49
+ Imagga::RankColor.new(percent: 20, hex: '#ff00ff'),
50
+ '39,12,34,123'
51
+ ],
52
+ dist: 3000,
53
+ count: 10
54
+ ) }.to raise_error(ArgumentError, 'type is missing')
55
+ end
56
+ end
57
+
58
+ context "with failing request" do
59
+ before do
60
+ FakeWeb.register_uri(:post, fake_service_url, body: failed_signature_response)
61
+ end
62
+
63
+ it "raises exception" do
64
+ expect { subject.rank(
65
+ colors: [
66
+ Imagga::RankColor.new(percent: 60, r: 10, g: 20, b: 30),
67
+ Imagga::RankColor.new(percent: 20, hex: '#ff00ff'),
68
+ '39,12,34,123'
69
+ ],
70
+ type: :overall,
71
+ dist: 3000,
72
+ count: 10
73
+ ) }.to raise_error(Imagga::ClientException)
74
+ end
75
+
76
+ context "exception" do
77
+ before do
78
+ begin
79
+ subject.rank(
80
+ colors: [
81
+ Imagga::RankColor.new(percent: 60, r: 10, g: 20, b: 30),
82
+ Imagga::RankColor.new(percent: 20, hex: '#ff00ff'),
83
+ '39,12,34,123'
84
+ ],
85
+ type: :overall,
86
+ dist: 3000,
87
+ count: 10
88
+ )
89
+ rescue Imagga::ClientException => e
90
+ @exception = e
91
+ end
92
+ end
93
+
94
+ it "has message" do
95
+ @exception.message.should == 'Invalid signature'
96
+ end
97
+
98
+ it "has error code" do
99
+ @exception.error_code.should == 3
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'fake_web'
3
+
4
+ class DummyCommand < Imagga::BaseCommand
5
+ def args(options); options.merge(c: :d); end
6
+ def service_path; '/service.php'; end
7
+ end
8
+
9
+ describe Imagga::BaseCommand do
10
+ subject { DummyCommand.new('123456', 'secret','http://example.com') }
11
+
12
+ it "has api_key" do
13
+ subject.api_key.should == '123456'
14
+ end
15
+
16
+ it "has api_secret" do
17
+ subject.api_secret.should == 'secret'
18
+ end
19
+
20
+ it "has base_uri" do
21
+ subject.base_uri.should == 'http://example.com'
22
+ end
23
+
24
+ it "has class base_uri" do
25
+ subject.class.base_uri.should == 'http://example.com'
26
+ end
27
+
28
+ describe "#execute" do
29
+ it "sends service request with params and gets and parses json result" do
30
+ described_class.should_receive(:post).with('/service.php', body: { a: :b, c: :d }) { '{"f": "g"}' }
31
+ subject.execute(a: :b).should == { 'f' => 'g' }
32
+ end
33
+ end
34
+ end
35
+
36
+ describe Imagga::ExtractCommand do
37
+ subject { described_class.new('123456', 'secret','http://example.com') }
38
+
39
+ it "has service_path" do
40
+ subject.service_path.should == '/colorsearchserver.php'
41
+ end
42
+
43
+ it "has options class defined" do
44
+ subject.options_class.should == Imagga::ExtractOptions
45
+ end
46
+ end
47
+
48
+ describe Imagga::RankCommand do
49
+ subject { described_class.new('123456', 'secret','http://example.com') }
50
+
51
+ it "has service_path" do
52
+ subject.service_path.should == '/colorsearchserver.php'
53
+ end
54
+
55
+ it "has options class defined" do
56
+ subject.options_class.should == Imagga::RankOptions
57
+ end
58
+ end
59
+
60
+ describe Imagga::CropCommand do
61
+ subject { described_class.new('123456', 'secret','http://example.com') }
62
+
63
+ it "has service_path" do
64
+ subject.service_path.should == '/extractionrestserver.php'
65
+ end
66
+
67
+ it "has options class defined" do
68
+ subject.options_class.should == Imagga::CropOptions
69
+ end
70
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::CoreClient do
4
+
5
+ subject { described_class.new(api_key: '123456', api_secret: 'secret', base_uri: 'http://example.com') }
6
+
7
+ it "has api_key" do
8
+ subject.api_key.should == '123456'
9
+ end
10
+
11
+ it "has api_secret" do
12
+ subject.api_secret.should == 'secret'
13
+ end
14
+
15
+ it "has base_uri" do
16
+ subject.base_uri.should == 'http://example.com'
17
+ end
18
+
19
+ describe "#extract" do
20
+ it "executes extract image info command" do
21
+ executes_command(Imagga::ExtractCommand, :extract)
22
+ end
23
+ end
24
+
25
+ describe "#rank" do
26
+ it "executes rank image command" do
27
+ executes_command(Imagga::RankCommand, :rank)
28
+ end
29
+ end
30
+
31
+ describe "#crop" do
32
+ it "executes crop image command" do
33
+ executes_command(Imagga::CropCommand, :crop)
34
+ end
35
+ end
36
+
37
+ def executes_command(command_class, method_name)
38
+ command = mock
39
+ command_class.should_receive(:new).with('123456', 'secret', 'http://example.com') { command }
40
+ command.should_receive(:execute).with(a: :b) { 'result' }
41
+ subject.send(method_name, a: :b).should == 'result'
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::CropInfo do
4
+ let(:crop_info) { JSON.parse(IO.read('./spec/fixtures/crop_response.txt')) }
5
+ subject { described_class.new(crop_info['smart_croppings'].first) }
6
+
7
+ it "has url" do
8
+ subject.url.should == 'http://www.stockpodium.com/stock-photo-7890736/couple-child-spending-time-together-image.jpg'
9
+ end
10
+
11
+ it "has croppings" do
12
+ subject.croppings.size.should == 3
13
+ subject.croppings.first.x2.should == 355
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::CropOptions do
4
+ subject { described_class.new('apikey', 'secret') }
5
+
6
+ describe "#options" do
7
+ it "builds simple options" do
8
+ subject.options(
9
+ urls: 'http://image',
10
+ resolutions: '100x40,50x100',
11
+ no_scaling: true
12
+ ).should == {
13
+ api_key: "apikey",
14
+ method: "imagga.process.crop",
15
+ no_scaling: 1,
16
+ resolutions: "100x40,50x100",
17
+ sig: "dde5487ab116f710402d7821a241389a",
18
+ urls: "http://image",
19
+ v: "1.0"
20
+ }
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::CropResultBuilder do
4
+ end
@@ -1,73 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Imagga::ExtractOptions do
4
+
4
5
  subject { described_class.new('apikey', 'secret') }
5
6
 
6
7
  describe "#options" do
8
+
7
9
  it "builds simple options" do
8
- subject.options('http://image').should == {
9
- urls: 'http://image',
10
- method: 'imagga.colorsearch.extract',
11
- sig: '76e667179a6520da86e272a609bc7fab',
10
+ subject.options(urls: 'http://image').should == {
11
+ urls: 'http://image',
12
+ method: 'imagga.colorsearch.extract',
13
+ sig: '76e667179a6520da86e272a609bc7fab',
12
14
  api_key: 'apikey',
13
- v: '1.0'
15
+ v: '1.0'
14
16
  }
15
17
  end
16
18
 
17
19
  it "builds complex options" do
18
- subject.options([
19
- Imagga::Image.new(url: 'http://image1', id: '333'),
20
- Imagga::Image.new(url: 'http://image2')
21
- ], extract_overall_colors: true, extract_object_colors: false).should == {
20
+ subject.options(
22
21
  urls: 'http://image1,http://image2',
23
- ids: '333,0',
22
+ ids: '333,0',
23
+ extract_overall_colors: true,
24
+ extract_object_colors: false
25
+ ).should == {
26
+ urls: 'http://image1,http://image2',
27
+ ids: '333,0',
24
28
  method: 'imagga.colorsearch.extract',
25
29
  extract_overall_colors: 1,
26
- extract_object_colors: 0,
27
- sig: 'd66a0e3409ed08878ce7efae2827de87',
30
+ extract_object_colors: 0,
31
+ sig: 'd66a0e3409ed08878ce7efae2827de87',
28
32
  api_key: 'apikey',
29
33
  v: '1.0'
30
34
  }
31
- end
32
- end
33
-
34
- describe "#build_urls" do
35
- it "builds string of urls based on single image url string" do
36
- subject.build_urls('http://image').should == 'http://image'
37
- end
38
-
39
- it "builds string of urls based on image url string arrays" do
40
- subject.build_urls(['http://image1', 'http://image2']).should == 'http://image1,http://image2'
41
- end
42
-
43
- it "builds string of urls based on single image object" do
44
- subject.build_urls(Imagga::Image.new(url: 'http://image')).should == 'http://image'
45
- end
46
-
47
- it "builds string of urls based on image object array" do
48
- subject.build_urls([
49
- Imagga::Image.new(url: 'http://image1'),
50
- Imagga::Image.new(url: 'http://image2')]).should == 'http://image1,http://image2'
51
- end
52
- end
53
-
54
- describe "#build_ids" do
55
- it "returns 0 based on single image url string" do
56
- subject.build_ids('http://image').should == nil
57
- end
58
-
59
- it "returns array of 0-s given image url string arrays" do
60
- subject.build_ids(['http://image1', 'http://image2']).should == nil
61
- end
62
-
63
- it "builds string of ids based on single image object" do
64
- subject.build_ids(Imagga::Image.new(url: 'http://image', id: '123')).should == '123'
65
- end
66
35
 
67
- it "builds string of ids based on image object array" do
68
- subject.build_ids([
69
- Imagga::Image.new(url: 'http://image1', id: '123'),
70
- Imagga::Image.new(url: 'http://image2')]).should == '123,0'
71
36
  end
72
37
  end
73
38
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::ImageCrop do
4
+ subject { described_class.new(
5
+ 'target_width' => 10,
6
+ 'target_height' => 22,
7
+ 'x1' => 33, 'y1' => 44,
8
+ 'x2' => 55, 'y2' => 66
9
+ ) }
10
+
11
+ it "has target_widht" do
12
+ subject.target_width.should == 10
13
+ end
14
+
15
+ it "has target height" do
16
+ subject.target_height.should == 22
17
+ end
18
+
19
+ it "has x1 and y1" do
20
+ subject.x1.should == 33
21
+ subject.y1.should == 44
22
+ end
23
+
24
+ it "has x2 and y2" do
25
+ subject.x2.should == 55
26
+ subject.y2.should == 66
27
+ end
28
+
29
+ describe "#info" do
30
+ it 'returns infor string about the crop suggestions' do
31
+ subject.info.should == 'target: (10,22), crop: (33, 44) to (55, 66)'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Imagga::ImageOrUrlParametizer do
4
+ subject { described_class.new }
5
+
6
+ describe "#parametrize" do
7
+ it "builds string of urls based on single image url string" do
8
+ subject.parametrize('http://image').should == { urls: 'http://image' }
9
+ end
10
+
11
+ it "builds string of urls based on image url string arrays" do
12
+ subject.parametrize(['http://image1', 'http://image2']).should == { urls: 'http://image1,http://image2' }
13
+ end
14
+
15
+ it "builds string of urls based on single image object" do
16
+ subject.parametrize(Imagga::Image.new(url: 'http://image', id: '99')).should == { urls: 'http://image', ids: '99' }
17
+ end
18
+
19
+ it "builds string of urls based on image object array" do
20
+ subject.parametrize([
21
+ Imagga::Image.new(url: 'http://image1', id: '333'),
22
+ Imagga::Image.new(url: 'http://image2')]).should == { urls: 'http://image1,http://image2', ids: '333,0' }
23
+ end
24
+ end
25
+
26
+ describe "#build_urls" do
27
+ it "builds string of urls based on single image url string" do
28
+ subject.build_urls('http://image').should == { urls: 'http://image' }
29
+ end
30
+
31
+ it "builds string of urls based on image url string arrays" do
32
+ subject.build_urls(['http://image1', 'http://image2']).should == { urls: 'http://image1,http://image2' }
33
+ end
34
+
35
+ it "builds string of urls based on single image object" do
36
+ subject.build_urls(Imagga::Image.new(url: 'http://image')).should == { urls: 'http://image' }
37
+ end
38
+
39
+ it "builds string of urls based on image object array" do
40
+ subject.build_urls([
41
+ Imagga::Image.new(url: 'http://image1'),
42
+ Imagga::Image.new(url: 'http://image2')]).should == { urls: 'http://image1,http://image2' }
43
+ end
44
+ end
45
+
46
+ describe "#build_comma_separated_string" do
47
+ it "returns 0 based on single image url string" do
48
+ subject.build_ids('http://image').should == { ids: nil }
49
+ end
50
+
51
+ it "returns array of 0-s given image url string arrays" do
52
+ subject.build_ids(['http://image1', 'http://image2']).should == { ids: nil }
53
+ end
54
+
55
+ it "builds string of ids based on single image object" do
56
+ subject.build_ids(Imagga::Image.new(url: 'http://image', id: '123')).should == { ids: '123' }
57
+ end
58
+
59
+ it "builds string of ids based on image object array" do
60
+ subject.build_ids([
61
+ Imagga::Image.new(url: 'http://image1', id: '123'),
62
+ Imagga::Image.new(url: 'http://image2')]).should == { ids: '123,0' }
63
+ end
64
+ end
65
+ end