flickr_collage_tty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5837b75e65ac3b0f640aa1a66e9c334eb8adfbad
4
+ data.tar.gz: 2a6d5dced9317ca631b583e4b9eecfe0d04af4ec
5
+ SHA512:
6
+ metadata.gz: 66ae2d3c68f21dcf4d5be10a1b998ed6e5adada9f69ea61c7b1571e50835920b3aa49be59c9d7a8ca27a60a6bedab250a282f22897d3152bc817cc5918afe5f3
7
+ data.tar.gz: a95a98d1943e3b30e9603a75b3d41ceb77f34e2a84018a0d0b1a169e2cf90536c3fec61b7b1fddb3d337c65cb82ee55668309268dee624b48e8d84014a613de6
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ DisplayCopNames: true
4
+ Exclude:
5
+ - flickr_collage_tty.gemspec
6
+ - Rakefile
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Metrics/BlockLength:
12
+ Max: 40
13
+
14
+ Style/GuardClause:
15
+ Enabled: false
16
+
17
+ Metrics/LineLength:
18
+ Max: 100
19
+
20
+ Style/StringLiterals:
21
+ Enabled: false
22
+ SupportedStyles:
23
+ - single_quotes
24
+ - double_quotes
25
+ ConsistentQuotesInMultiline: false
26
+
27
+ Metrics/MethodLength:
28
+ Enabled: false
29
+
30
+ Style/BlockDelimiters:
31
+ Enabled: false
32
+
33
+ Style/ClassAndModuleChildren:
34
+ Enabled: false
35
+
36
+ Layout/MultilineOperationIndentation:
37
+ Enabled: false
38
+
39
+ Layout/MultilineMethodCallIndentation:
40
+ Enabled: false
41
+
42
+ Style/FrozenStringLiteralComment:
43
+ Enabled: false
44
+
45
+ Style/MutableConstant:
46
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ bundler_args: --jobs 10 --retry 10
3
+ language: ruby
4
+ rvm:
5
+ - 2.3.0
6
+ before_install: gem install bundler -v 1.14.6
7
+ script:
8
+ - bundle exec rspec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at long.polyglot@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flickr_collage_tty.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Long Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # FlickrCollageTty
2
+
3
+ [![Code Climate](https://codeclimate.com/github/kimquy/flickr_collage_tty/badges/gpa.svg)](https://codeclimate.com/github/kimquy/flickr_collage_tty)
4
+
5
+ [![Build Status](https://travis-ci.org/kimquy/flickr_collage_tty.svg?branch=master)](https://travis-ci.org/kimquy/flickr_collage_tty)
6
+
7
+ FlickrCollageTty allow you to create a collage from keywords you enter into the terminal. Then it will fetch the most suitable image from Flickr correspond to each keyword.
8
+
9
+
10
+ ![screenshot](https://user-images.githubusercontent.com/2282642/28498501-2c64cce4-6fc9-11e7-889a-1d4dbe1069f3.jpg)
11
+
12
+ ## Installation
13
+
14
+ $ gem install flickr_collage_tty
15
+
16
+ ## Requirements
17
+
18
+ In order to this gem to work the following things are required:
19
+
20
+ * ImageMagick
21
+ * Flickr API key
22
+
23
+ ## Program design
24
+
25
+ ![design](https://user-images.githubusercontent.com/2282642/28501264-815d205a-7002-11e7-9995-9e3095eb100f.png)
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ # Simply run the following command into the terminal
31
+ # Then follow the instruction of the programm
32
+
33
+ # The program will ask for the Flickr API credentials
34
+ # for the first time. If it successful authenticate, then
35
+ # the program will not ask for the credentials again.
36
+
37
+ fc_tty
38
+ ```
39
+
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kimquy/flickr_collage_tty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flickr_collage_tty"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exec/fc_tty ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tty'
4
+ require 'flickr_collage_tty'
5
+ require 'flickr_collage_tty/configuration'
6
+
7
+ prompt = TTY::Prompt.new
8
+ FlickrCollageTty.load_cached_flickr_credentials
9
+
10
+ until FlickrCollageTty.api_key_given?
11
+ api_key = prompt.ask("Enter Flickr api key: ", required: true)
12
+ api_secret = prompt.ask("Enter Flickr api secret: ", required: true)
13
+
14
+ FlickrCollageTty.configure do |config|
15
+ config.api_key = api_key
16
+ config.api_secret = api_secret
17
+ end
18
+ end
19
+
20
+ keywords = prompt.ask("Enter a list of keywords (keywords separated by a comma)") do |q|
21
+ q.required true
22
+ q.convert ->(input) { input.split(/,\s*/).uniq.take(10) }
23
+ end
24
+
25
+ output =
26
+ prompt.ask(
27
+ "Enter output filename (should enter absolute path to the file, ex: ./test.jpg) : ",
28
+ required: true
29
+ )
30
+
31
+ FlickrCollageTty.generate_collage(keywords, output)
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'flickr_collage_tty/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "flickr_collage_tty"
9
+ spec.version = FlickrCollageTty::VERSION
10
+ spec.authors = ["Long Nguyen"]
11
+ spec.email = ["long.polyglot@gmail.com"]
12
+
13
+ spec.summary = %q{Create collage images from Flickr.}
14
+ spec.description = %q{Create collage images from Flickr.}
15
+ spec.homepage = "https://github.com/kimquy/flickr_collage_tty"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.bindir = "exec"
20
+ spec.executables = ["fc_tty"]
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "flickraw"
24
+ spec.add_dependency "tty"
25
+ spec.add_dependency "rmagick-screwdrivers"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "rubocop"
31
+ end
@@ -0,0 +1,12 @@
1
+ require "flickr_collage_tty/version"
2
+ require "flickr_collage_tty/generator"
3
+ require "flickr_collage_tty/configuration"
4
+ require 'flickr_collage_tty/dictionary'
5
+
6
+ module FlickrCollageTty
7
+ def self.generate_collage(keywords, output)
8
+ FlickrCollageTty::Flickr.create
9
+ FlickrCollageTty::Dictionary.load
10
+ FlickrCollageTty::Generator.call(keywords, output)
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ require 'rmagick/screwdrivers'
2
+
3
+ class FlickrCollageTty::Assembler
4
+ COLLAGE_DIR = "/tmp/flickr_collages"
5
+
6
+ def self.call(output, tmp_folder = COLLAGE_DIR)
7
+ new(output, tmp_folder).call
8
+ end
9
+
10
+ def initialize(output, tmp_folder)
11
+ @output = output
12
+ @tmp_folder = tmp_folder
13
+ end
14
+
15
+ def call
16
+ create_collage
17
+ clean_up
18
+ success_message
19
+ end
20
+
21
+ private
22
+
23
+ def options
24
+ {
25
+ output: @output,
26
+ columns: 5,
27
+ rotate_angle: 0,
28
+ scale_range: 0.2,
29
+ thumb_width: 250
30
+ }
31
+ end
32
+
33
+ def clean_up
34
+ 0.upto(9).each do |i|
35
+ TTY::File.remove_file("#{@tmp_folder}/collage_#{i}.jpg")
36
+ end
37
+ end
38
+
39
+ def create_collage
40
+ Magick::Screwdrivers.collage(@tmp_folder, options).write(options[:output])
41
+ end
42
+
43
+ def success_message
44
+ TTY::Prompt.new.say("Created a collage success! You can look at the image at #{@output}")
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ require 'flickr_collage_tty/utils/credential_storage'
2
+ require 'flickr_collage_tty/flickr'
3
+
4
+ module FlickrCollageTty
5
+ class Configuration
6
+ attr_accessor :api_key, :api_secret
7
+
8
+ def initialize
9
+ self.api_key = nil
10
+ self.api_secret = nil
11
+ end
12
+ end
13
+
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def self.configure
23
+ yield(configuration) if block_given?
24
+
25
+ begin
26
+ FlickrCollageTty::Flickr.set_credentials
27
+ FlickRaw::Flickr.new
28
+
29
+ FlickrCollageTty::Utils::CredentialStorage.write(
30
+ key: configuration.api_key,
31
+ secret: configuration.api_secret
32
+ )
33
+ rescue
34
+ TTY::Prompt.new.say("Invalid Flickr API credentials.")
35
+ exit(1) unless ENV["environment"] == "test"
36
+ end
37
+ end
38
+
39
+ def self.api_key_given?
40
+ @configuration &&
41
+ @configuration.api_key &&
42
+ @configuration.api_secret
43
+ end
44
+
45
+ def self.load_cached_flickr_credentials
46
+ credentials = FlickrCollageTty::Utils::CredentialStorage.load
47
+ if credentials
48
+ configure do |config|
49
+ config.api_key = credentials[:api_key]
50
+ config.api_secret = credentials[:api_secret]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,28 @@
1
+ class FlickrCollageTty::Dictionary
2
+ class << self
3
+ attr_accessor :dictionary_url, :instance
4
+
5
+ def load(path = '/usr/share/dict/words')
6
+ self.dictionary_url = path
7
+ self.instance = new(path)
8
+ end
9
+ end
10
+
11
+ attr_reader :data
12
+
13
+ def sample
14
+ data.sample
15
+ end
16
+
17
+ private
18
+
19
+ def initialize(path)
20
+ @path = path
21
+ @data = load
22
+ end
23
+
24
+ def load
25
+ return [] unless File.exist?(@path)
26
+ File.readlines(@path).map(&:strip)
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ class FlickrCollageTty::Downloader
2
+ def self.call(image_urls)
3
+ FlickrCollageTty::Utils::Spinner.start(message: "Downloading image urls ...")
4
+
5
+ image_urls.each_with_index do |url, index|
6
+ TTY::Prompt.new.say("Downloading image from: #{url}")
7
+ TTY::File.download_file(url, "/tmp/flickr_collages/collage_#{index}.jpg")
8
+ end
9
+
10
+ FlickrCollageTty::Utils::Spinner.stop(message: "Finish download images!")
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ require 'flickraw'
2
+
3
+ class FlickrCollageTty::Flickr
4
+ class << self
5
+ attr_accessor :instance
6
+
7
+ def create
8
+ self.instance = new
9
+ end
10
+
11
+ def set_credentials
12
+ FlickRaw.api_key = FlickrCollageTty.configuration.api_key
13
+ FlickRaw.shared_secret = FlickrCollageTty.configuration.api_secret
14
+ end
15
+ end
16
+
17
+ def search(text:)
18
+ options = {
19
+ text: text,
20
+ sort: "interestingness-desc",
21
+ content_type: 1,
22
+ media: :photos
23
+ }
24
+
25
+ response = @flickr.photos.search(options).first
26
+ get_photo_url_from(flick_raw_response: response)
27
+ end
28
+
29
+ private
30
+
31
+ def initialize
32
+ @flickr = FlickRaw::Flickr.new
33
+ end
34
+
35
+ def get_photo_url_from(flick_raw_response:)
36
+ @flickr.photos.getSizes(photo_id: flick_raw_response.id)[-1].source
37
+ rescue
38
+ nil
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ require 'flickr_collage_tty/downloader'
2
+ require 'flickr_collage_tty/images_aggregator'
3
+ require 'flickr_collage_tty/flickr'
4
+ require 'flickr_collage_tty/assembler'
5
+
6
+ class FlickrCollageTty::Generator
7
+ def self.call(keywords, output)
8
+ image_urls = FlickrCollageTty::ImagesAggregator.call(
9
+ keywords,
10
+ FlickrCollageTty::Flickr.instance,
11
+ FlickrCollageTty::Dictionary.instance
12
+ )
13
+ FlickrCollageTty::Downloader.call(image_urls)
14
+ FlickrCollageTty::Assembler.call(output)
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ require 'flickr_collage_tty/utils/spinner'
2
+
3
+ module FlickrCollageTty
4
+ class ImagesAggregator
5
+ MAX_IMAGE_URLS = 10
6
+
7
+ def self.call(keywords, flickr, dictionary)
8
+ new(keywords, flickr, dictionary).call
9
+ end
10
+
11
+ attr_reader :keywords, :flickr, :image_urls, :dictionary
12
+
13
+ def initialize(keywords, flickr, dictionary)
14
+ @keywords = keywords
15
+ @flickr = flickr
16
+ @dictionary = dictionary
17
+ @image_urls = []
18
+ end
19
+
20
+ def call
21
+ FlickrCollageTty::Utils::Spinner.start(message: "Aggregating image urls ...")
22
+
23
+ keywords.each do |keyword|
24
+ get_new_image_url(keyword)
25
+ end
26
+
27
+ while image_urls.size < MAX_IMAGE_URLS
28
+ keyword = dictionary.sample
29
+ get_new_image_url(keyword)
30
+ end
31
+
32
+ FlickrCollageTty::Utils::Spinner.stop(message: "Finish aggregate image urls!")
33
+
34
+ image_urls
35
+ end
36
+
37
+ private
38
+
39
+ def get_new_image_url(keyword)
40
+ url = flickr.search(text: keyword)
41
+ image_urls << url if url
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+
3
+ module FlickrCollageTty
4
+ module Utils
5
+ class CredentialStorage
6
+ DEFAULT_STORAGE = "#{Dir.home}/.fc_tty.yaml"
7
+
8
+ class << self
9
+ def write(key:, secret:)
10
+ data = { api_key: key, api_secret: secret }.to_yaml
11
+ TTY::File.create_file(DEFAULT_STORAGE, data)
12
+ end
13
+
14
+ def load
15
+ YAML.load_file(DEFAULT_STORAGE)
16
+ rescue
17
+ nil
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module FlickrCollageTty
2
+ module Utils
3
+ class Spinner
4
+ class << self
5
+ attr_accessor :instance
6
+ end
7
+
8
+ def self.start(message:)
9
+ self.instance ||= TTY::Spinner.new
10
+ TTY::Prompt.new.say(message)
11
+ self.instance.auto_spin
12
+ end
13
+
14
+ def self.stop(message:)
15
+ self.instance.stop(message)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module FlickrCollageTty
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ dog
2
+ cat
3
+ book
4
+ lamb
5
+ picture
6
+ television
7
+ phone
8
+ laptop
9
+ wallet
10
+ key
11
+ magazine
12
+ paper
13
+ table
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Configuration, order: :defined do
4
+ describe ".configure" do
5
+ let(:error_msg) { "Invalid Flickr API credentials." }
6
+
7
+ context "invalid credentials" do
8
+ let(:api_key) { "abcd" }
9
+ let(:api_secret) { "abcd" }
10
+
11
+ it "does not store the key" do
12
+ expect_any_instance_of(TTY::Prompt).to receive(:say).with(error_msg)
13
+ expect(FlickrCollageTty::Utils::CredentialStorage).not_to receive(:write)
14
+
15
+ FlickrCollageTty.configure do |config|
16
+ config.api_key = api_key
17
+ config.api_secret = api_secret
18
+ end
19
+ end
20
+ end
21
+
22
+ context "valid credentials" do
23
+ let(:api_key) { ENV["flickr_api_key"] }
24
+ let(:api_secret) { ENV["flickr_api_secret"] }
25
+
26
+ it "store the key" do
27
+ expect_any_instance_of(TTY::Prompt).not_to receive(:say).with(error_msg)
28
+ expect(FlickrCollageTty::Utils::CredentialStorage).to receive(:write)
29
+
30
+ FlickrCollageTty.configure do |config|
31
+ config.api_key = api_key
32
+ config.api_secret = api_secret
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Dictionary do
4
+ let(:dictionary_path) {
5
+ File.expand_path('../../fixtures/dictionary.txt', __FILE__)
6
+ }
7
+
8
+ let(:data) {
9
+ %w[
10
+ dog cat book lamb picture television phone laptop
11
+ wallet key magazine paper table
12
+ ].sort
13
+ }
14
+
15
+ describe ".load" do
16
+ it "loads dictionary from the path" do
17
+ dictionary = FlickrCollageTty::Dictionary.load(dictionary_path)
18
+ expect(dictionary.data.sort).to eq(data)
19
+ end
20
+ end
21
+
22
+ describe "#sample" do
23
+ let(:dictionary) { FlickrCollageTty::Dictionary.load(dictionary_path) }
24
+ let(:keyword) { dictionary.sample }
25
+
26
+ it "get a word from the dictionary" do
27
+ expect(dictionary.data).to include(keyword)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Downloader do
4
+ let(:image_urls) {
5
+ [
6
+ "http://www.readersdigest.ca/wp-content/uploads/2011/01/4-ways-cheer-up-depressed-cat.jpg",
7
+ "http://media1.santabanta.com/full1/Animals/Cats/cats-85a.jpg"
8
+ ]
9
+ }
10
+
11
+ describe ".call" do
12
+ it "download the images" do
13
+ image_urls.each_with_index do |url, i|
14
+ expect(TTY::File).to receive(:download_file)
15
+ .with(url, "/tmp/flickr_collages/collage_#{i}.jpg")
16
+ .once
17
+ end
18
+ FlickrCollageTty::Downloader.call(image_urls)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Flickr do
4
+ before do
5
+ FlickrCollageTty.configure do |config|
6
+ config.api_key = ENV["flickr_api_key"]
7
+ config.api_secret = ENV["flickr_api_secret"]
8
+ end
9
+ end
10
+
11
+ describe ".create" do
12
+ before do
13
+ FlickrCollageTty::Flickr.set_credentials
14
+ end
15
+
16
+ it "create a flickr instance" do
17
+ flickr_instance = FlickrCollageTty::Flickr.create
18
+ expect(flickr_instance).to be_an_instance_of(FlickrCollageTty::Flickr)
19
+ end
20
+ end
21
+
22
+ describe ".set_credentials" do
23
+ it "set flickr credentials info" do
24
+ FlickrCollageTty::Flickr.set_credentials
25
+ expect(FlickRaw.api_key).not_to eq(nil)
26
+ expect(FlickRaw.shared_secret).not_to eq(nil)
27
+ end
28
+ end
29
+
30
+ describe "#search" do
31
+ let(:keyword) { "cat" }
32
+
33
+ before do
34
+ FlickrCollageTty::Flickr.set_credentials
35
+ end
36
+
37
+ it "searchs the image and return an image url" do
38
+ flickr_instance = FlickrCollageTty::Flickr.create
39
+
40
+ result = flickr_instance.search(text: keyword)
41
+
42
+ expect(result).to include("http")
43
+ expect(result).to include("jpg")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Generator do
4
+ let(:keywords) { %w[cat dog] }
5
+ let(:output) { "./test.jpg" }
6
+
7
+ before do
8
+ FlickrCollageTty.configure do |config|
9
+ config.api_key = ENV["flickr_api_key"]
10
+ config.api_secret = ENV["flickr_api_secret"]
11
+ end
12
+ FlickrCollageTty::Flickr.set_credentials
13
+ FlickrCollageTty::Flickr.create
14
+ FlickrCollageTty::Dictionary.load
15
+ end
16
+
17
+ describe ".call" do
18
+ it "generate a collage" do
19
+ expect(FlickrCollageTty::ImagesAggregator).to receive(:call).with(
20
+ keywords,
21
+ FlickrCollageTty::Flickr.instance,
22
+ FlickrCollageTty::Dictionary.instance
23
+ )
24
+ expect(FlickrCollageTty::Downloader).to receive(:call)
25
+ expect(FlickrCollageTty::Assembler).to receive(:call).with(output)
26
+ FlickrCollageTty::Generator.call(keywords, output)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.shared_examples "images aggregation" do
4
+ it "aggreates 10 image urls" do
5
+ image_urls = described_class.call(keywords, flickr_instance, dictionary_instance)
6
+ image_urls.each do |image_url|
7
+ expect(image_url).to include("http")
8
+ end
9
+ expect(image_urls.size).to eq(10)
10
+ end
11
+ end
12
+
13
+ RSpec.describe FlickrCollageTty::ImagesAggregator do
14
+ describe ".call" do
15
+ before do
16
+ FlickrCollageTty.configure do |config|
17
+ config.api_key = ENV["flickr_api_key"]
18
+ config.api_secret = ENV["flickr_api_secret"]
19
+ end
20
+ FlickrCollageTty::Flickr.set_credentials
21
+ FlickrCollageTty::Flickr.create
22
+ FlickrCollageTty::Dictionary.load
23
+ end
24
+
25
+ let(:flickr_instance) { FlickrCollageTty::Flickr.instance }
26
+ let(:dictionary_instance) { FlickrCollageTty::Dictionary.instance }
27
+
28
+ context "when input keywords are less than 10" do
29
+ let(:keywords) { %w[cat dog] }
30
+ it_behaves_like "images aggregation"
31
+ end
32
+
33
+ context "when input keywords are 10" do
34
+ let(:keywords) {
35
+ %w[cat dog pig follower computer television lamp paper phone picture]
36
+ }
37
+
38
+ it_behaves_like "images aggregation"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FlickrCollageTty::Utils::CredentialStorage do
4
+ describe ".write" do
5
+ let(:key) { "acbd" }
6
+ let(:secret) { "acbd" }
7
+ let(:data) {
8
+ { api_key: key, api_secret: secret }.to_yaml
9
+ }
10
+
11
+ it "write credential to yaml file" do
12
+ expect(TTY::File).to receive(:create_file)
13
+ .with(described_class::DEFAULT_STORAGE, data)
14
+
15
+ described_class.write(key: key, secret: secret)
16
+ end
17
+ end
18
+
19
+ describe ".load" do
20
+ it "loads yaml file from default storage" do
21
+ expect(YAML).to receive(:load_file).with(described_class::DEFAULT_STORAGE)
22
+ described_class.load
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe FlickrCollageTty do
4
+ it "has a version number" do
5
+ expect(FlickrCollageTty::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ require "bundler/setup"
2
+ require 'tty'
3
+ require "flickr_collage_tty"
4
+ require "flickr_collage_tty/flickr"
5
+ require 'flickr_collage_tty/configuration'
6
+ require 'flickr_collage_tty/dictionary'
7
+ require 'flickr_collage_tty/images_aggregator'
8
+ require 'flickr_collage_tty/utils/spinner'
9
+ require 'flickr_collage_tty/utils/credential_storage'
10
+
11
+ ENV["environment"] = "test"
12
+
13
+ RSpec.configure do |config|
14
+ # Enable flags like --only-failures and --next-failure
15
+ config.example_status_persistence_file_path = ".rspec_status"
16
+
17
+ config.expect_with :rspec do |c|
18
+ c.syntax = :expect
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickr_collage_tty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Long Nguyen
8
+ autorequire:
9
+ bindir: exec
10
+ cert_chain: []
11
+ date: 2017-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flickraw
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rmagick-screwdrivers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Create collage images from Flickr.
112
+ email:
113
+ - long.polyglot@gmail.com
114
+ executables:
115
+ - fc_tty
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - exec/fc_tty
131
+ - flickr_collage_tty.gemspec
132
+ - lib/flickr_collage_tty.rb
133
+ - lib/flickr_collage_tty/assembler.rb
134
+ - lib/flickr_collage_tty/configuration.rb
135
+ - lib/flickr_collage_tty/dictionary.rb
136
+ - lib/flickr_collage_tty/downloader.rb
137
+ - lib/flickr_collage_tty/flickr.rb
138
+ - lib/flickr_collage_tty/generator.rb
139
+ - lib/flickr_collage_tty/images_aggregator.rb
140
+ - lib/flickr_collage_tty/utils/credential_storage.rb
141
+ - lib/flickr_collage_tty/utils/spinner.rb
142
+ - lib/flickr_collage_tty/version.rb
143
+ - spec/fixtures/dictionary.txt
144
+ - spec/flickr_collage_tty/configuration_spec.rb
145
+ - spec/flickr_collage_tty/dictionary_spec.rb
146
+ - spec/flickr_collage_tty/downloader_spec.rb
147
+ - spec/flickr_collage_tty/flickr_spec.rb
148
+ - spec/flickr_collage_tty/generator_spec.rb
149
+ - spec/flickr_collage_tty/images_aggregator_spec.rb
150
+ - spec/flickr_collage_tty/utils/credential_storage_spec.rb
151
+ - spec/flickr_collage_tty_spec.rb
152
+ - spec/spec_helper.rb
153
+ homepage: https://github.com/kimquy/flickr_collage_tty
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.5.1
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Create collage images from Flickr.
177
+ test_files: []