giphy 0.0.1

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 (45) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.md +22 -0
  6. data/README.md +126 -0
  7. data/Rakefile +7 -0
  8. data/bin/giphy +12 -0
  9. data/giphy.gemspec +26 -0
  10. data/lib/giphy.rb +8 -0
  11. data/lib/giphy/artist.rb +35 -0
  12. data/lib/giphy/cli.rb +23 -0
  13. data/lib/giphy/client.rb +72 -0
  14. data/lib/giphy/configuration.rb +23 -0
  15. data/lib/giphy/errors/api.rb +5 -0
  16. data/lib/giphy/errors/argument_error.rb +5 -0
  17. data/lib/giphy/favorite_gif.rb +9 -0
  18. data/lib/giphy/flagged_gif.rb +18 -0
  19. data/lib/giphy/gif.rb +83 -0
  20. data/lib/giphy/gif_by_id.rb +34 -0
  21. data/lib/giphy/image.rb +31 -0
  22. data/lib/giphy/null_image.rb +23 -0
  23. data/lib/giphy/request.rb +56 -0
  24. data/lib/giphy/response.rb +27 -0
  25. data/lib/giphy/search.rb +85 -0
  26. data/lib/giphy/special_gif.rb +25 -0
  27. data/lib/giphy/version.rb +3 -0
  28. data/spec/fixtures/giphy.rb +3 -0
  29. data/spec/giphy/artist_spec.rb +32 -0
  30. data/spec/giphy/cli_spec.rb +17 -0
  31. data/spec/giphy/client_spec.rb +94 -0
  32. data/spec/giphy/configuration_spec.rb +34 -0
  33. data/spec/giphy/favorite_gif_spec.rb +28 -0
  34. data/spec/giphy/flagged_gif_spec.rb +34 -0
  35. data/spec/giphy/gif_by_id_spec.rb +40 -0
  36. data/spec/giphy/gif_spec.rb +89 -0
  37. data/spec/giphy/image_spec.rb +32 -0
  38. data/spec/giphy/null_image_spec.rb +11 -0
  39. data/spec/giphy/request_spec.rb +49 -0
  40. data/spec/giphy/response_spec.rb +43 -0
  41. data/spec/giphy/search_spec.rb +111 -0
  42. data/spec/giphy/special_gif_spec.rb +15 -0
  43. data/spec/shared_examples/special_gif_contract_spec.rb +29 -0
  44. data/spec/spec_helper.rb +6 -0
  45. metadata +175 -0
@@ -0,0 +1,83 @@
1
+ module Giphy
2
+ class Gif
3
+ def self.build_batch_from(array)
4
+ array.map { |gif| new(gif)}
5
+ end
6
+
7
+ def initialize(hash)
8
+ @hash = hash
9
+ end
10
+
11
+ def id
12
+ hash.fetch('id')
13
+ end
14
+
15
+ def url
16
+ URI(hash.fetch('url', ''))
17
+ end
18
+
19
+ def bitly_gif_url
20
+ URI(hash.fetch('bitly_gif_url', ''))
21
+ end
22
+
23
+ def bitly_fullscreen_url
24
+ URI(hash.fetch('bitly_fullscreen_url', ''))
25
+ end
26
+
27
+ def bitly_tiled_url
28
+ URI(hash.fetch('bitly_tiled_url', ''))
29
+ end
30
+
31
+ def embed_url
32
+ URI(hash.fetch('embed_url', ''))
33
+ end
34
+
35
+ def fixed_height_image
36
+ @fixed_height_image ||= images['fixed_height'] ? image(images['fixed_height']) : null_image
37
+ end
38
+
39
+ def fixed_height_still_image
40
+ @fixed_height_still_image ||= images['fixed_height_still'] ? image(images['fixed_height_still']) : null_image
41
+ end
42
+
43
+ def fixed_height_downsampled_image
44
+ @fixed_height_downsampled_image ||= images['fixed_height_downsampled'] ? image(images['fixed_height_downsampled']) : null_image
45
+ end
46
+
47
+ def fixed_width_image
48
+ @fixed_width_image ||= images['fixed_width'] ? image(images['fixed_width']) : null_image
49
+ end
50
+
51
+ def fixed_width_still_image
52
+ @fixed_width_still_image ||= images['fixed_width_still'] ? image(images['fixed_width_still']) : null_image
53
+ end
54
+
55
+ def fixed_width_downsampled_image
56
+ @fixed_width_downsampled_image ||= images['fixed_width_downsampled'] ? image(images['fixed_width_downsampled']) : null_image
57
+ end
58
+
59
+ def original_image
60
+ @original_image ||= images['original'] ? image(images['original']) : null_image
61
+ end
62
+
63
+ def image_original_url
64
+ URI(hash.fetch('image_original_url', ''))
65
+ end
66
+
67
+ private
68
+
69
+ attr_reader :hash
70
+
71
+ def images
72
+ @images ||= hash.fetch('images', {})
73
+ end
74
+
75
+ def image(image_hash)
76
+ Giphy::Image.new(image_hash)
77
+ end
78
+
79
+ def null_image
80
+ NullImage.new
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,34 @@
1
+ module Giphy
2
+ class GifByID
3
+ def get(ids)
4
+ case ids.size
5
+ when 0
6
+ raise Giphy::Errors::ArgumentError.new('wrong number of arguments (0 for 1...Infinite)')
7
+ when 1
8
+ one_gif(ids.first)
9
+ else
10
+ several_gifs(ids)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def one_gif(id)
17
+ result = client.gif(id)
18
+ gif.new(result)
19
+ end
20
+
21
+ def several_gifs(ids)
22
+ result = client.gifs(ids)
23
+ gif.build_batch_from(result)
24
+ end
25
+
26
+ def client
27
+ Giphy::Client.new
28
+ end
29
+
30
+ def gif
31
+ Giphy::Gif
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ module Giphy
2
+ class Image
3
+ def initialize(hash)
4
+ @hash = hash
5
+ end
6
+
7
+ def url
8
+ URI(hash.fetch('url'))
9
+ end
10
+
11
+ def width
12
+ hash.fetch('width').to_i
13
+ end
14
+
15
+ def height
16
+ hash.fetch('height').to_i
17
+ end
18
+
19
+ def size
20
+ hash.fetch('size', 0).to_i
21
+ end
22
+
23
+ def frames
24
+ hash.fetch('frames', 0).to_i
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :hash
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module Giphy
2
+ class NullImage
3
+ def url
4
+ URI('')
5
+ end
6
+
7
+ def width
8
+ 0
9
+ end
10
+
11
+ def height
12
+ 0
13
+ end
14
+
15
+ def size
16
+ 0
17
+ end
18
+
19
+ def frames
20
+ 0
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,56 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware/parse_oj'
3
+
4
+ module Giphy
5
+ class Request
6
+ URL = 'http://api.giphy.com'
7
+
8
+ def self.get(path, options={})
9
+ new.get(path, options)
10
+ end
11
+
12
+ def self.post(path, options={})
13
+ new.post(path, options)
14
+ end
15
+
16
+ def get(path, options={})
17
+ connection.get(complete_path(path), complete_options(options))
18
+ end
19
+
20
+ def post(path, options={})
21
+ connection.post(complete_path(path), complete_options(options))
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :api_response
27
+
28
+ def connection
29
+ Faraday.new(url: URL) do |connection|
30
+ connection.request :url_encoded
31
+ connection.response :oj
32
+ connection.adapter Faraday.default_adapter
33
+ end
34
+ end
35
+
36
+ def version
37
+ configuration.version
38
+ end
39
+
40
+ def api_key
41
+ configuration.api_key
42
+ end
43
+
44
+ def configuration
45
+ Giphy::Configuration
46
+ end
47
+
48
+ def complete_options(options)
49
+ {api_key: api_key}.merge(options)
50
+ end
51
+
52
+ def complete_path(path)
53
+ "/#{version}/gifs#{path}"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,27 @@
1
+ module Giphy
2
+ class Response
3
+ def self.build(response)
4
+ new(response).data
5
+ end
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ def data
12
+ response.body['data'] || raise(Giphy::Errors::API.new(error))
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :response
18
+
19
+ def error
20
+ "#{meta['error_type']} #{meta['error_message']}"
21
+ end
22
+
23
+ def meta
24
+ @meta ||= response.body.fetch('meta')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,85 @@
1
+ module Giphy
2
+ module Search
3
+ def recent(options={})
4
+ result = client.recent(options)
5
+ gif.build_batch_from(result)
6
+ end
7
+
8
+ def translate(word)
9
+ result = client.translate(word)
10
+ gif.build_batch_from(result)
11
+ end
12
+
13
+ def search(keyword, options={})
14
+ result = client.search(keyword, options)
15
+ gif.build_batch_from(result)
16
+ end
17
+
18
+ def flag(id)
19
+ result = client.flag(id)
20
+ flagged_gif.new(result)
21
+ end
22
+
23
+ def flagged
24
+ result = client.flagged
25
+ flagged_gif.build_batch_from(result)
26
+ end
27
+
28
+ def favorite(id)
29
+ result = client.favorite(id)
30
+ favorite_gif.new(result)
31
+ end
32
+
33
+ def favorites
34
+ result = client.favorites
35
+ favorite_gif.build_batch_from(result)
36
+ end
37
+
38
+ def screensaver(tag)
39
+ result = client.screensaver(tag)
40
+ gif.new(result)
41
+ end
42
+
43
+ def random
44
+ result = client.random
45
+ gif.new(result)
46
+ end
47
+
48
+ def artists
49
+ result = client.artists
50
+ artist.build_batch_from(result)
51
+ end
52
+
53
+ def gif_by_artist(username, options={})
54
+ options_hash = {username: username}.merge(options)
55
+ result = client.artists(options_hash)
56
+ gif.build_batch_from(result)
57
+ end
58
+
59
+ def gif_by_id(*ids)
60
+ GifByID.new.get(ids)
61
+ end
62
+
63
+ private
64
+
65
+ def client
66
+ Giphy::Client.new
67
+ end
68
+
69
+ def gif
70
+ Giphy::Gif
71
+ end
72
+
73
+ def flagged_gif
74
+ Giphy::FlaggedGif
75
+ end
76
+
77
+ def favorite_gif
78
+ Giphy::FavoriteGif
79
+ end
80
+
81
+ def artist
82
+ Giphy::Artist
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ module Giphy
4
+ class SpecialGif
5
+ def self.build_batch_from(array)
6
+ array.map { |special_gif| new(special_gif)}
7
+ end
8
+
9
+ def initialize(hash)
10
+ @hash = hash
11
+ end
12
+
13
+ def create_date
14
+ DateTime.parse(hash.fetch('create_date'))
15
+ end
16
+
17
+ def gif_id
18
+ hash.fetch('gif_id')
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :hash
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Giphy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ module Fixtures
2
+
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Giphy::Artist do
4
+ subject { Giphy::Artist.new(hash) }
5
+
6
+ context "when initialized with a valid hash" do
7
+ let(:hash) do
8
+ { 'username' => "frankmacchia",
9
+ "avatar" => "http://media.giphy.com/avatars/frankmacchia.gif",
10
+ "website" => "http://frankmacchia.blogspot.com/",
11
+ "name" => "Frank Macchia",
12
+ "count" => 7
13
+ }
14
+ end
15
+
16
+ its(:username) { should eq "frankmacchia" }
17
+ its(:avatar) { should eq URI("http://media.giphy.com/avatars/frankmacchia.gif") }
18
+ its(:website) { should eq URI("http://frankmacchia.blogspot.com/") }
19
+ its(:name) { should eq "Frank Macchia" }
20
+ its(:count) { should eq 7 }
21
+ end
22
+
23
+ context "when initialized with an empty/invalid hash" do
24
+ let(:hash) { {} }
25
+
26
+ it { expect{ subject.username }.to raise_error KeyError }
27
+ it { expect{ subject.avatar }.to raise_error KeyError }
28
+ it { expect{ subject.website }.to raise_error KeyError }
29
+ it { expect{ subject.name }.to raise_error KeyError }
30
+ it { expect{ subject.count }.to raise_error KeyError }
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Giphy::CLI do
4
+ describe "#search" do
5
+ subject { Giphy::CLI.new('horse') }
6
+
7
+ before { Giphy.stub(screensaver: double(id: 'OoFJ7iMGUBpiE')) }
8
+
9
+ it "echoes a message to the console and opens the url using Kernel#system" do
10
+ Giphy.stub(screensaver: double(id: 'OoFJ7iMGUBpiE'))
11
+ Giphy::CLI.new('horse')
12
+ subject.should_receive(:system).
13
+ with("echo 'Showing the GIF on your default browser: http://giphy.com/embed/OoFJ7iMGUBpiE' && open http://giphy.com/embed/OoFJ7iMGUBpiE")
14
+ subject.search
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Giphy::Client do
4
+ let(:api_response) { double }
5
+ let(:response) { double }
6
+
7
+ before { Giphy::Response.stub(:build).with(api_response).and_return(response) }
8
+
9
+ subject { Giphy::Client.new }
10
+
11
+ describe "#recent" do
12
+ it "does a GET on the 'recent' endpoint" do
13
+ Giphy::Request.stub(:get).with('/recent', options: 'options').and_return(api_response)
14
+ expect(subject.recent(options: 'options')).to eq response
15
+ end
16
+ end
17
+
18
+ describe "#translate" do
19
+ it "does a GET on the 'transalate' endpoint" do
20
+ Giphy::Request.stub(:get).with('/translate', s: 'word').and_return(api_response)
21
+ expect(subject.translate('word')).to eq response
22
+ end
23
+ end
24
+
25
+ describe "#search" do
26
+ it "does a GET on the 'search' endpoint" do
27
+ Giphy::Request.stub(:get).with('/search', {q: 'keyword', options: 'options'}).and_return(api_response)
28
+ expect(subject.search('keyword', options: 'options')).to eq response
29
+ end
30
+ end
31
+
32
+ describe "#flag" do
33
+ it "does a POST on the 'flagged' endpoint" do
34
+ Giphy::Request.stub(:post).with('/12HoHdqnDxz5NS/flagged', {}).and_return(api_response)
35
+ expect(subject.flag('12HoHdqnDxz5NS')).to eq response
36
+ end
37
+ end
38
+
39
+ describe "#flagged" do
40
+ it "does a GET on the 'flagged' endpoint" do
41
+ Giphy::Request.stub(:get).with('/flagged', {}).and_return(api_response)
42
+ expect(subject.flagged).to eq response
43
+ end
44
+ end
45
+
46
+ describe "#favorite" do
47
+ it "does a POST on the 'favorites' endpoint" do
48
+ Giphy::Request.stub(:post).with('/12HoHdqnDxz5NS/favorites', {}).and_return(api_response)
49
+ expect(subject.favorite('12HoHdqnDxz5NS')).to eq response
50
+ end
51
+ end
52
+
53
+ describe "#favorites" do
54
+ it "does a GET on the 'favorites' endpoint" do
55
+ Giphy::Request.stub(:get).with('/favorites', {}).and_return(api_response)
56
+ expect(subject.favorites).to eq response
57
+ end
58
+ end
59
+
60
+ describe "#screensaver" do
61
+ it "does a GET on the 'screensaver' endpoint" do
62
+ Giphy::Request.stub(:get).with('/screensaver', {tag: 'tag'}).and_return(api_response)
63
+ expect(subject.screensaver('tag')).to eq response
64
+ end
65
+ end
66
+
67
+ describe "#ramdom" do
68
+ it "does a GET on the 'random' endpoint" do
69
+ Giphy::Request.stub(:get).with('/random', {}).and_return(api_response)
70
+ expect(subject.random).to eq response
71
+ end
72
+ end
73
+
74
+ describe "#artists" do
75
+ it "does a GET on the 'artists' endpoint" do
76
+ Giphy::Request.stub(:get).with('/artists', {}).and_return(api_response)
77
+ expect(subject.artists).to eq response
78
+ end
79
+ end
80
+
81
+ describe "#gif" do
82
+ it "does a GET on the 'gif_by_id' endpoint" do
83
+ Giphy::Request.stub(:get).with('/12HoHdqnDxz5NS', {}).and_return(api_response)
84
+ expect(subject.gif('12HoHdqnDxz5NS')).to eq response
85
+ end
86
+ end
87
+
88
+ describe "#gifs" do
89
+ it "does a GET on the 'artists' endpoint" do
90
+ Giphy::Request.stub(:get).with('', {ids: '12HoHdqnDxz5NS,dc6zaTOxFJmzC'}).and_return(api_response)
91
+ expect(subject.gifs(['12HoHdqnDxz5NS', 'dc6zaTOxFJmzC'])).to eq response
92
+ end
93
+ end
94
+ end