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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzI1NDkwMDYxMzc3YzFkMjU4OTI3YjYzYjVjYTc4M2RiZGEwNzM5Nw==
5
+ data.tar.gz: !binary |-
6
+ NDMyYTFlNzU1YzljZjFhNjY5MDE2OGIyZTZjYzlkYzdjYjkwZjBjYg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OTYyZjNmODMwYmRhMTljYzNlOTVmMDg2ZTEyNzg2M2NiNTRiM2MyNWI1ZWYx
10
+ ODBmZWZkOWU5MWY5MjBiNTVlZmYyYzMxM2VhMWJlOTU1ODhiYzdlYTQ4OTlj
11
+ NzdhNzk0YmExMzJkYzljNTNiYTY1MDUxZjM5YWNhZGVmZGRlNDA=
12
+ data.tar.gz: !binary |-
13
+ NDExMmM1Yjg1OGFjNThiNWYyYjgyN2ExNTlkNmJjMzEwNDFmMTJhZDUzYzdk
14
+ NmU2ZjU3YjZkYTY2ZjhiNDRmNWIyZDYzMTJmMDNkNjFlNTMxODE5MmZmOWIz
15
+ MzlkMTE1MzJiM2M2MTZiNjMyYWZhODgyMjQxZTBhNGMzZDJhMWM=
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --order TYPE[random]
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in giphy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sebastian Sogamoso
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,126 @@
1
+ # Giphy
2
+
3
+ Because GIFs make life fun! Use [Giphy API](http://api.giphy.com) from your Ruby programs and
4
+ command line. Check out [Giphy Labs](http://labs.giphy.com/) for inspiration.
5
+
6
+ ![](http://media.giphy.com/media/GuDQNjS0qJHpe/200.gif)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'giphy'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install giphy
21
+
22
+ ## Usage
23
+
24
+ Set the API `version` and `api_key`:
25
+
26
+ ```ruby
27
+ Giphy::Configuration.configure do |config|
28
+ config.version = THE_API_VERSION
29
+ config.api_key = YOUR_API_KEY
30
+ end
31
+ ```
32
+
33
+ **Important:** Giphy API is currently in public beta. Unless `version` and `api_key` are set
34
+ to specific values the default values for the beta period will be used. Check
35
+ [Giphy's API](http://labs.giphy.com/) for updates.
36
+
37
+ That's it, you're ready to have fun!
38
+
39
+ #### Recent
40
+ ````ruby
41
+ Giphy.recent(tag: 'cats', limit: 5)
42
+ ````
43
+
44
+ #### Translate
45
+ ````ruby
46
+ Giphy.translate('geek')
47
+ ````
48
+
49
+ #### Search
50
+ ````ruby
51
+ Giphy.search('funny cat', {limit: 50, offset: 25})
52
+ ````
53
+
54
+ #### Flagged
55
+ Write
56
+ ````ruby
57
+ Giphy.flag('m5QHf0caAwgMw')
58
+ ````
59
+ Read
60
+ ````ruby
61
+ Giphy.flagged
62
+ ````
63
+
64
+ #### Favorites
65
+ Write
66
+ ````ruby
67
+ Giphy.favorite('12HoHdqnDxz5NS')
68
+ ````
69
+ Read
70
+ ````ruby
71
+ Giphy.favorites
72
+ ````
73
+
74
+ #### Screensaver
75
+ ````ruby
76
+ Giphy.screensaver('surprise')
77
+ ````
78
+
79
+ #### Random
80
+ ````ruby
81
+ Giphy.random
82
+ ````
83
+
84
+ #### Artists
85
+ ````ruby
86
+ Giphy.artists
87
+ ````
88
+
89
+ #### GIFs by artist
90
+ ````ruby
91
+ Giphy.gif_by_artist('89a', {limit: 5, offset: 10})
92
+ ````
93
+
94
+ #### GIFs by ID
95
+ ````ruby
96
+ Giphy.gif_by_id('feqkVgjJpYtjy', '12HoHdqnDxz5NS', '7rzbxdu0ZEXLy')
97
+ ````
98
+
99
+ ### Command Line Tool
100
+
101
+ There is a small command line tool that let's you search for GIFs from the
102
+ command line and opens it on your browser. Just for fun.
103
+
104
+ $ giphy 'dance'
105
+
106
+ *Currently only supported for Mac.*
107
+
108
+ ## Supported Ruby Versions
109
+ This library aims to support and is [tested against][travis] the following Ruby
110
+ implementations:
111
+
112
+ * Ruby 1.9.3
113
+ * Ruby 2.0.0
114
+
115
+ ## Contributing
116
+
117
+ 1. Fork it
118
+ 2. Create your feature branch: `git checkout -b my-new-feature`
119
+ 3. Run tests: `rake`
120
+ 4. Commit your changes: `git commit -am 'Add some feature'`
121
+ 5. Push to the branch: `git push origin my-new-feature`
122
+ 6. Create new Pull Request
123
+
124
+ ## License
125
+
126
+ See [LICENSE](https://github.com/sebasoga/giphy/blob/master/LICENSE.md) for details.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :test => :spec
7
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Doing this so we can run the following in development (which doesn't have
4
+ # lib/ in the path)
5
+ #
6
+ # $ ./bin/giphy
7
+ #
8
+ $LOAD_PATH.unshift File.expand_path('../../lib/', __FILE__)
9
+ require 'giphy'
10
+
11
+ cli = Giphy::CLI.new(ARGV[0])
12
+ cli.search
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'giphy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "giphy"
8
+ spec.version = Giphy::VERSION
9
+ spec.authors = ["Sebastian Sogamoso"]
10
+ spec.email = ["sebasoga@gmail.com"]
11
+ spec.description = %q{A Ruby interface to the Giphy API.}
12
+ spec.summary = %q{Enjoy Giphy API from you Ruby programs and command line}
13
+ spec.homepage = "http://github.com/sebasoga/giphy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "faraday", "~> 0.8.8"
25
+ spec.add_dependency "faraday_middleware-parse_oj", "~> 0.2.1"
26
+ end
@@ -0,0 +1,8 @@
1
+ require "uri"
2
+
3
+ current_path = File.expand_path("../", __FILE__)
4
+ Dir["#{current_path}/**/*.rb"].each { |file| require file }
5
+
6
+ module Giphy
7
+ extend Giphy::Search
8
+ end
@@ -0,0 +1,35 @@
1
+ module Giphy
2
+ class Artist
3
+ def self.build_batch_from(array)
4
+ array.map { |artist| new(artist) }
5
+ end
6
+
7
+ def initialize(hash)
8
+ @hash = hash
9
+ end
10
+
11
+ def username
12
+ hash.fetch('username')
13
+ end
14
+
15
+ def avatar
16
+ URI(hash.fetch('avatar'))
17
+ end
18
+
19
+ def website
20
+ URI(hash.fetch('website'))
21
+ end
22
+
23
+ def name
24
+ hash.fetch('name')
25
+ end
26
+
27
+ def count
28
+ hash.fetch('count').to_i
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :hash
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module Giphy
2
+ class CLI
3
+ def initialize(keyword)
4
+ @keyword = keyword
5
+ end
6
+
7
+ def search
8
+ system "echo 'Showing the GIF on your default browser: #{url}' && open #{url}"
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :keyword
14
+
15
+ def url
16
+ @url ||= URI("http://giphy.com/embed/#{result.id}")
17
+ end
18
+
19
+ def result
20
+ Giphy.screensaver(keyword)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,72 @@
1
+ module Giphy
2
+ class Client
3
+ def recent(options={})
4
+ get('/recent', options)
5
+ end
6
+
7
+ def translate(word)
8
+ get('/translate', s: word)
9
+ end
10
+
11
+ def search(keyword, options={})
12
+ options_hash = {q: keyword}.merge(options)
13
+ get('/search', options_hash)
14
+ end
15
+
16
+ def flag(id)
17
+ post("/#{id}/flagged")
18
+ end
19
+
20
+ def flagged
21
+ get('/flagged')
22
+ end
23
+
24
+ def favorite(id)
25
+ post("/#{id}/favorites")
26
+ end
27
+
28
+ def favorites
29
+ get('/favorites')
30
+ end
31
+
32
+ def screensaver(tag)
33
+ get('/screensaver', tag: tag)
34
+ end
35
+
36
+ def random
37
+ get('/random')
38
+ end
39
+
40
+ def artists(options={})
41
+ get('/artists', options)
42
+ end
43
+
44
+ def gif(id)
45
+ get("/#{id}")
46
+ end
47
+
48
+ def gifs(ids)
49
+ get('', ids: ids.join(','))
50
+ end
51
+
52
+ private
53
+
54
+ def get(path, options={})
55
+ api_response = request.get(path, options)
56
+ response(api_response)
57
+ end
58
+
59
+ def post(path, options={})
60
+ api_response = request.post(path, options)
61
+ response(api_response)
62
+ end
63
+
64
+ def request
65
+ Giphy::Request
66
+ end
67
+
68
+ def response(api_response)
69
+ Giphy::Response.build(api_response)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,23 @@
1
+ module Giphy
2
+ class Configuration
3
+ DEFAULT_VERSION = 'v1'
4
+ DEFAULT_API_KEY = 'dc6zaTOxFJmzC'
5
+
6
+ class << self
7
+ attr_writer :version, :api_key
8
+
9
+ def configure(&block)
10
+ yield self
11
+ self
12
+ end
13
+
14
+ def version
15
+ @version ||= DEFAULT_VERSION
16
+ end
17
+
18
+ def api_key
19
+ @api_key ||= DEFAULT_API_KEY
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Giphy
2
+ module Errors
3
+ class API < StandardError ; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Giphy
2
+ module Errors
3
+ class ArgumentError < StandardError ; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'giphy/special_gif'
2
+
3
+ module Giphy
4
+ class FavoriteGif < Giphy::SpecialGif
5
+ def tag
6
+ hash.fetch('tag')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ require 'date'
2
+ require 'giphy/special_gif'
3
+
4
+ module Giphy
5
+ class FlaggedGif < Giphy::SpecialGif
6
+ def inappropriate?
7
+ hash.fetch('is_inappropriate') == "1"
8
+ end
9
+
10
+ def wrong_source?
11
+ hash.fetch('is_wrong_source') == "1"
12
+ end
13
+
14
+ def source_corrected
15
+ hash.fetch('source_corrected')
16
+ end
17
+ end
18
+ end