├── .rspec ├── Rakefile ├── spec ├── spec_helper.rb └── html │ └── pipeline │ └── flickr_filter_spec.rb ├── Gemfile ├── bin ├── setup └── console ├── lib └── html │ └── pipeline │ ├── flickr │ ├── version.rb │ └── flickr_filter.rb │ └── flickr.rb ├── .gitignore ├── .travis.yml ├── Guardfile ├── html-pipeline-flickr.gemspec └── README.md /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'html/pipeline/flickr' 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | group :test do 4 | gem "guard-rspec" 5 | gem "guard" 6 | end 7 | gemspec 8 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /lib/html/pipeline/flickr/version.rb: -------------------------------------------------------------------------------- 1 | module HTML 2 | class Pipeline 3 | module Flickr 4 | VERSION = "0.1.1" 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | **.gem 11 | -------------------------------------------------------------------------------- /lib/html/pipeline/flickr.rb: -------------------------------------------------------------------------------- 1 | require "html/pipeline/flickr/version" 2 | require "html/pipeline/flickr/flickr_filter" 3 | 4 | module HTML 5 | class Pipeline 6 | module Flickr 7 | # Your code goes here... 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | 3 | script: "bundle exec rspec" 4 | rvm: 5 | - 2.3 6 | - ruby-head 7 | 8 | notifications: 9 | email: 10 | recipients: 11 | - stan001212@gmail.com 12 | on_failure: change 13 | on_success: never 14 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | guard 'rspec', cmd: "bundle exec rspec" do 2 | # watch /lib/ files 3 | watch(%r{^lib/(.+).rb$}) do |m| 4 | "spec/#{m[1]}_spec.rb" 5 | end 6 | 7 | # watch /spec/ files 8 | watch(%r{^spec/(.+).rb$}) do |m| 9 | "spec/#{m[1]}.rb" 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "html/pipeline/flickr" 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 15 | -------------------------------------------------------------------------------- /html-pipeline-flickr.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'html/pipeline/flickr/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "html-pipeline-flickr" 8 | spec.version = HTML::Pipeline::Flickr::VERSION 9 | spec.authors = ["Stan Luo"] 10 | spec.email = ["stan001212@gmail.com"] 11 | 12 | spec.summary = %q{Flickr filter for html-pipeline} 13 | spec.description = %q{A html-pipeline filter that converts flickr url into a linkable image} 14 | spec.homepage = "https://github.com/st0012/html-pipeline-flickr" 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 17 | spec.bindir = "exe" 18 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_dependency "html-pipeline", "~> 2.0" 22 | spec.add_development_dependency "rake", "~> 10.0" 23 | end 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/st0012/html-pipeline-flickr.svg?branch=master)](https://travis-ci.org/st0012/html-pipeline-flickr) 2 | 3 | # HTML::Pipeline Flickr Gem 4 | A html-pipeline filter that converts flickr url into a linkable image 5 | 6 | ## Installation 7 | 8 | Add this line to your application's Gemfile: 9 | 10 | ```ruby 11 | gem 'html-pipeline' 12 | gem 'html-pipeline-flickr' 13 | ``` 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | Or install it yourself as: 20 | 21 | $ gem install html-pipeline-flickr 22 | 23 | ## Usage 24 | 25 | See [html-pipeline](https://github.com/jch/html-pipeline) 26 | 27 | 28 | ## Development 29 | 30 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. 31 | 32 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 33 | 34 | ## Contributing 35 | 36 | 1. [Fork it]( https://github.com/st0012/html-pipeline-flickr/fork ) 37 | 2. Create your feature branch (`git checkout -b my-new-feature`) 38 | 3. Commit your changes (`git commit -am 'Add some feature'`) 39 | 4. Push to the branch (`git push origin my-new-feature`) 40 | 5. Create a new Pull Request 41 | -------------------------------------------------------------------------------- /lib/html/pipeline/flickr/flickr_filter.rb: -------------------------------------------------------------------------------- 1 | require 'html/pipeline' 2 | require 'uri' 3 | require 'net/http' 4 | require 'json' 5 | 6 | module HTML 7 | class Pipeline 8 | # HTML Filter for converting flickr's link into linkable image 9 | # 10 | # Context options: 11 | # :flickr_maxwidth, flickr_minheight - representing maxwidth and maxheight in oembed format's params. 12 | # :flickr_link_attr - HTML attributes for the link that will be generated 13 | 14 | class FlickrFilter < TextFilter 15 | def call 16 | regex = %r{(=")*(https?:\/\/(www\.)?flickr\.com\/photos\/[^\s<]*)} 17 | uri = URI("https://www.flickr.com/services/oembed") 18 | 19 | link_attr = context[:flickr_link_attr] || "" 20 | 21 | @text.gsub(regex) do |match| 22 | if $1.nil? 23 | params = { 24 | url: match, 25 | format: :json, 26 | maxwidth: max_value(:width), 27 | maxheight: max_value(:height) 28 | } 29 | 30 | uri.query = URI.encode_www_form(params) 31 | 32 | response = JSON.parse(Net::HTTP.get(uri)) 33 | 34 | %{#{response[} 35 | else 36 | match 37 | end 38 | end 39 | rescue 40 | @text 41 | end 42 | 43 | private 44 | 45 | def max_value(attr) 46 | value = context["flickr_max#{attr}".to_sym] 47 | value.to_i >= 0 ? value.to_i : 0 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /spec/html/pipeline/flickr_filter_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe HTML::Pipeline::FlickrFilter do 4 | subject { HTML::Pipeline::FlickrFilter } 5 | let(:flickr_url) { "https://www.flickr.com/photos/99434203@N05/9379906996" } 6 | 7 | it 'has a version number' do 8 | expect(HTML::Pipeline::Flickr::VERSION).not_to be nil 9 | end 10 | 11 | context "Default context" do 12 | it "generates with default options" do 13 | expect(subject.to_html(flickr_url)).to eq( 14 | %(Ebony&Alex-011-IMGP4840) 15 | ) 16 | end 17 | end 18 | context "Multiple source" do 19 | it "generates two content" do 20 | expect(subject.to_html(flickr_url + " test " + flickr_url)).to eq( 21 | %(Ebony&Alex-011-IMGP4840 test Ebony&Alex-011-IMGP4840) 22 | ) 23 | end 24 | end 25 | 26 | context "With maxwidth & maxheight options" do 27 | it "gets different size of image" do 28 | result = subject.to_html( 29 | flickr_url, 30 | flickr_maxwidth: 100, 31 | flickr_maxheight: 200 32 | ) 33 | 34 | expect(result).to eq( 35 | %(Ebony&Alex-011-IMGP4840) 36 | ) 37 | end 38 | end 39 | 40 | context "With link_attr option" do 41 | it "generates link with attributes" do 42 | result = subject.to_html(flickr_url, flickr_link_attr: "target='_blank'") 43 | 44 | expect(result).to eq( 45 | %(Ebony&Alex-011-IMGP4840) 46 | ) 47 | end 48 | end 49 | 50 | context "Url is in links's href" do 51 | it "does nothing" do 52 | result = subject.to_html( 53 | %{}, 54 | flickr_link_attr: "target='_blank'" 55 | ) 56 | 57 | expect(result).to eq( 58 | %{} 59 | ) 60 | end 61 | end 62 | end 63 | --------------------------------------------------------------------------------