loadgif 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc0b3850ce68bb81d186d8cda4ef23294871bf01760e858e5184500b45c629f4
4
- data.tar.gz: 000a00611ebd43caa5c28b929b495db0b251d14dae31e23ed62af18f73617a7a
3
+ metadata.gz: 8d2c7c57d1b7f66787413b204547df95830b5c9838e4e81e913a833e37253599
4
+ data.tar.gz: caeb1d531a07d3b30e28827d2a720686ebc92803929d44f6cc51f2e9efc06cd9
5
5
  SHA512:
6
- metadata.gz: 924ec23fbfee65839c8f34c33a9742462c29b0a483c97e2dd6c04a7df7dc6a9b09467adb2e1fa4823291e2cd91795bbd9cf30461ac5c80875be56c90ecda4194
7
- data.tar.gz: d5920004834fe4d33c805c88c7cf54bd8f355d6d489f4826a1af09a2b2d5e415e562f216b956928bf1a285379e6c70a1d2a448beac5b95cb1070bc2f1146e0d3
6
+ metadata.gz: 2e015c1d12d40f638d1ded7072c060cedd17650d90c95cad42a43ad4423066730008b0e4f5312890a28c910788a8be8e7fdc2cd26d59d1133cd45671e343ce9e
7
+ data.tar.gz: 97abc8fcd6e07b051370a0c5987838eb99eeb805139350110a03eb244f24dd9246f31f36b4f0756b9b33ae79e5ca0476678a7d395907e6957543b90856570157
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Loadgif
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/loadgif`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ GIFs make 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.
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ ![](https://media.giphy.com/media/13p77tfexyLtx6/200.gif)
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,7 +23,74 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ Set the API `version` and `api_key`:
27
+
28
+ ```ruby
29
+ Loadgif::Configuration.configure do |config|
30
+ config.version = THE_API_VERSION
31
+ config.api_key = YOUR_API_KEY
32
+ end
33
+ ```
34
+
35
+ **Important:** Giphy API is currently in public beta. Unless `version` and `api_key` are set
36
+ to specific values the default values for the beta period will be used. Check
37
+ [Giphy's API](http://labs.giphy.com/) for updates.
38
+
39
+ That's it, you're ready to have fun!
40
+
41
+ #### Trending
42
+ ````ruby
43
+ Loadgif.trending(limit: 5)
44
+ ````
45
+
46
+ #### Translate
47
+ ````ruby
48
+ Loadgif.translate('geek')
49
+ ````
50
+
51
+ #### Search
52
+ ````ruby
53
+ Loadgif.search('funny cat', {limit: 50, offset: 25})
54
+ ````
55
+
56
+ #### Favorites
57
+ Write
58
+ ````ruby
59
+ Loadgif.favorite('12HoHdqnDxz5NS')
60
+ ````
61
+ Read
62
+ ````ruby
63
+ Loadgif.favorites('absurdnoise', {limit: 50, offset: 25})
64
+ ````
65
+
66
+ #### Screensaver
67
+ ````ruby
68
+ Loadgif.screensaver('surprise')
69
+ ````
70
+
71
+ #### Random
72
+ ````ruby
73
+ Loadgif.random
74
+ ````
75
+
76
+ For getting random gifs with a specific tag:
77
+
78
+ ````ruby
79
+ Loadgif.random('american psycho')
80
+ ````
81
+
82
+ #### GIFs by ID
83
+ ````ruby
84
+ Loadgif.gif_by_id('13p77tfexyLtx6', 'l2Sqb0owUC5s5tz5m', 'xT9DPoKxz6nrODpwVa')
85
+ ````
86
+
87
+ ### Command Line Tool
88
+
89
+ There is a small command line tool that let's you search for GIFs from the
90
+ command line and opens it on your browser. Just for fun.
91
+
92
+ $ loadgif 'dance'
93
+
26
94
 
27
95
  ## Development
28
96
 
@@ -41,3 +109,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
41
109
  ## Code of Conduct
42
110
 
43
111
  Everyone interacting in the Loadgif project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/loadgif/blob/master/CODE_OF_CONDUCT.md).
112
+
data/lib/loadgif.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require "loadgif/version"
2
+ require "uri"
3
+
4
+ current_path = File.expand_path("../", __FILE__)
5
+ Dir["#{current_path}/**/*.rb"].each { |file| require file }
2
6
 
3
7
  module Loadgif
4
8
  class Error < StandardError; end
5
9
  # Your code goes here...
6
- def self.hi
7
- puts "Hello world!"
8
- end
10
+ extend Loadgif::Search
9
11
  end
@@ -0,0 +1,34 @@
1
+ require 'launchy'
2
+
3
+ module Loadgif
4
+ class CLI
5
+ def self.run(keyword)
6
+ new(keyword).search
7
+ end
8
+
9
+ def initialize(keyword)
10
+ @keyword = keyword
11
+ end
12
+
13
+ def search
14
+ system "echo 'Showing the GIF on your browser'"
15
+ Launchy.open(url)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :keyword
21
+
22
+ def url
23
+ @url ||= URI("http://giphy.com/embed/#{result.id}")
24
+ end
25
+
26
+ def result
27
+ Loadgif.screensaver(keyword)
28
+ rescue Loadgif::Errors::API
29
+ GifNotFound.new('YyKPbc5OOTSQE')
30
+ end
31
+ end
32
+
33
+ GifNotFound = Struct.new(:id)
34
+ end
@@ -0,0 +1,61 @@
1
+ module Loadgif
2
+ class Client
3
+ def trending(options={})
4
+ get('/trending', 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 favorite(id)
17
+ post("/#{id}/favorites")
18
+ end
19
+
20
+ def favorites(username='', options={})
21
+ options_hash = {username: username}.merge(options)
22
+ get('/favorites', options_hash)
23
+ end
24
+
25
+ def screensaver(tag)
26
+ get('/screensaver', tag: tag)
27
+ end
28
+
29
+ def random(tag='')
30
+ get('/random', { tag: tag })
31
+ end
32
+
33
+ def gif(id)
34
+ get("/#{id}")
35
+ end
36
+
37
+ def gifs(ids)
38
+ get('', ids: ids.join(','))
39
+ end
40
+
41
+ private
42
+
43
+ def get(path, options={})
44
+ api_response = request.get(path, options)
45
+ response(api_response)
46
+ end
47
+
48
+ def post(path, options={})
49
+ api_response = request.post(path, options)
50
+ response(api_response)
51
+ end
52
+
53
+ def request
54
+ Loadgif::Request
55
+ end
56
+
57
+ def response(api_response)
58
+ Loadgif::Response.build(api_response)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ module Loadgif
2
+ class Configuration
3
+ DEFAULT_VERSION = 'v1'
4
+ DEFAULT_API_KEY = 'jdS99NKYEMxgqe6s4amvZe1qgRV3TtVn'
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,5 @@
1
+ module Giphy
2
+ module Errors
3
+ class Unexpected < StandardError ; end
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'Loadgif/special_gif'
2
+
3
+ module Loadgif
4
+ class FavoriteGif < Loadgif::SpecialGif
5
+ def tag
6
+ hash.fetch('tag')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,87 @@
1
+ module Loadgif
2
+ class Gif
3
+ def self.build_batch_from(array_or_hash)
4
+ if array_or_hash.is_a?(Array)
5
+ array_or_hash.map { |gif| new(gif) }
6
+ else
7
+ new(array_or_hash)
8
+ end
9
+ end
10
+
11
+ def initialize(hash)
12
+ @hash = hash
13
+ end
14
+
15
+ def id
16
+ hash.fetch('id')
17
+ end
18
+
19
+ def url
20
+ URI(hash.fetch('url', ''))
21
+ end
22
+
23
+ def bitly_gif_url
24
+ URI(hash.fetch('bitly_gif_url', ''))
25
+ end
26
+
27
+ def bitly_fullscreen_url
28
+ URI(hash.fetch('bitly_fullscreen_url', ''))
29
+ end
30
+
31
+ def bitly_tiled_url
32
+ URI(hash.fetch('bitly_tiled_url', ''))
33
+ end
34
+
35
+ def embed_url
36
+ URI(hash.fetch('embed_url', ''))
37
+ end
38
+
39
+ def fixed_height_image
40
+ @fixed_height_image ||= image(images['fixed_height'])
41
+ end
42
+
43
+ def fixed_height_still_image
44
+ @fixed_height_still_image ||= image(images['fixed_height_still'])
45
+ end
46
+
47
+ def fixed_height_downsampled_image
48
+ @fixed_height_downsampled_image ||= image(images['fixed_height_downsampled'])
49
+ end
50
+
51
+ def fixed_width_image
52
+ @fixed_width_image ||= image(images['fixed_width'])
53
+ end
54
+
55
+ def fixed_width_still_image
56
+ @fixed_width_still_image ||= image(images['fixed_width_still'])
57
+ end
58
+
59
+ def fixed_width_downsampled_image
60
+ @fixed_width_downsampled_image ||= image(images['fixed_width_downsampled'])
61
+ end
62
+
63
+ def original_image
64
+ @original_image ||= image(images['original'])
65
+ end
66
+
67
+ def image_original_url
68
+ URI(hash.fetch('image_original_url', ''))
69
+ end
70
+
71
+ private
72
+
73
+ attr_reader :hash
74
+
75
+ def images
76
+ @images ||= hash.fetch('images', {})
77
+ end
78
+
79
+ def image(image_hash)
80
+ image_hash ? Loadgif::Image.new(image_hash) : null_image
81
+ end
82
+
83
+ def null_image
84
+ NullImage.new
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,34 @@
1
+ module Loadgif
2
+ class GifByID
3
+ def get(ids)
4
+ case ids.size
5
+ when 0
6
+ raise Loadgif::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
+ Loadgif::Client.new
28
+ end
29
+
30
+ def gif
31
+ Loadgif::Gif
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,47 @@
1
+ module Loadgif
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
+ def mp4
28
+ URI(hash.fetch('mp4'))
29
+ end
30
+
31
+ def mp4_size
32
+ hash.fetch('mp4_size', 0).to_i
33
+ end
34
+
35
+ def webp
36
+ URI(hash.fetch('webp'))
37
+ end
38
+
39
+ def webp_size
40
+ hash.fetch('webp_size', 0).to_i
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :hash
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ module Loadgif
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,59 @@
1
+ module Loadgif
2
+ class RandomGif
3
+ def initialize(hash)
4
+ @hash = hash
5
+ end
6
+
7
+ def id
8
+ hash.fetch('id')
9
+ end
10
+
11
+ def url
12
+ URI(hash.fetch('url'))
13
+ end
14
+
15
+ def image_original_url
16
+ URI(hash.fetch('image_original_url'))
17
+ end
18
+
19
+ def image_url
20
+ URI(hash.fetch('image_url'))
21
+ end
22
+
23
+ def image_mp4_url
24
+ URI(hash.fetch('image_mp4_url'))
25
+ end
26
+
27
+ def image_frames
28
+ hash.fetch('image_frames')
29
+ end
30
+
31
+ def image_width
32
+ hash.fetch('image_width')
33
+ end
34
+
35
+ def image_height
36
+ hash.fetch('image_height')
37
+ end
38
+
39
+ def rating
40
+ hash.fetch('rating')
41
+ end
42
+
43
+ def username
44
+ hash.fetch('username')
45
+ end
46
+
47
+ def caption
48
+ hash.fetch('caption')
49
+ end
50
+
51
+ def tags
52
+ hash.fetch('tags')
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :hash
58
+ end
59
+ end
@@ -0,0 +1,59 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday_middleware/parse_oj'
4
+
5
+ module Loadgif
6
+ class Request
7
+ URL = 'http://api.giphy.com'
8
+
9
+ def self.get(path, options={})
10
+ new.get(path, options)
11
+ end
12
+
13
+ def self.post(path, options={})
14
+ new.post(path, options)
15
+ end
16
+
17
+ def get(path, options={})
18
+ connection.get(complete_path(path), complete_options(options))
19
+ end
20
+
21
+ def post(path, options={})
22
+ connection.post(complete_path(path), complete_options(options))
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :api_response
28
+
29
+ def connection
30
+ Faraday.new(url: URL) do |connection|
31
+ connection.use FaradayMiddleware::FollowRedirects
32
+ connection.use Faraday::Response::RaiseError
33
+ connection.request :url_encoded
34
+ connection.response :oj
35
+ connection.adapter Faraday.default_adapter
36
+ end
37
+ end
38
+
39
+ def version
40
+ configuration.version
41
+ end
42
+
43
+ def api_key
44
+ configuration.api_key
45
+ end
46
+
47
+ def configuration
48
+ Loadgif::Configuration
49
+ end
50
+
51
+ def complete_options(options)
52
+ {api_key: api_key}.merge(options)
53
+ end
54
+
55
+ def complete_path(path)
56
+ "/#{version}/gifs#{path}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,28 @@
1
+ module Loadgif
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
+ raise Loadgif::Errors::Unexpected unless response.body
13
+ response.body['data'] || raise(Loadgif::Errors::API.new(error))
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :response
19
+
20
+ def error
21
+ "#{meta['error_type']} #{meta['error_message']}"
22
+ end
23
+
24
+ def meta
25
+ @meta ||= response.body.fetch('meta')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ module Loadgif
2
+ module Search
3
+ def trending(options={})
4
+ result = client.trending(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 favorite(id)
19
+ result = client.favorite(id)
20
+ favorite_gif.new(result)
21
+ end
22
+
23
+ def favorites(username, options)
24
+ result = client.favorites(username, options)
25
+ favorite_gif.build_batch_from(result)
26
+ end
27
+
28
+ def screensaver(tag)
29
+ result = client.screensaver(tag)
30
+ gif.new(result)
31
+ end
32
+
33
+ def random(tag='')
34
+ result = client.random(tag)
35
+ random_gif.new(result)
36
+ end
37
+
38
+ def gif_by_id(*ids)
39
+ GifByID.new.get(ids)
40
+ end
41
+
42
+ private
43
+
44
+ def client
45
+ Loadgif::Client.new
46
+ end
47
+
48
+ def gif
49
+ Loadgif::Gif
50
+ end
51
+
52
+ def favorite_gif
53
+ Loadgif::FavoriteGif
54
+ end
55
+
56
+ def random_gif
57
+ Loadgif::RandomGif
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ module Loadgif
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
@@ -1,3 +1,3 @@
1
1
  module Loadgif
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/loadgif-0.1.0.gem ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loadgif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - cis-neelam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-01 00:00:00.000000000 Z
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,7 +54,24 @@ files:
54
54
  - bin/console
55
55
  - bin/setup
56
56
  - lib/loadgif.rb
57
+ - lib/loadgif/cli.rb
58
+ - lib/loadgif/client.rb
59
+ - lib/loadgif/configuration.rb
60
+ - lib/loadgif/errors/api.rb
61
+ - lib/loadgif/errors/argument_error.rb
62
+ - lib/loadgif/errors/unexpected.rb
63
+ - lib/loadgif/favorite_gif.rb
64
+ - lib/loadgif/gif.rb
65
+ - lib/loadgif/gif_by_id.rb
66
+ - lib/loadgif/image.rb
67
+ - lib/loadgif/null_image.rb
68
+ - lib/loadgif/random_gif.rb
69
+ - lib/loadgif/request.rb
70
+ - lib/loadgif/response.rb
71
+ - lib/loadgif/search.rb
72
+ - lib/loadgif/special_gif.rb
57
73
  - lib/loadgif/version.rb
74
+ - loadgif-0.1.0.gem
58
75
  - loadgif.gemspec
59
76
  homepage: http://rubygems.org/gems/loadgif
60
77
  licenses: