diff --git a/.travis.yml b/.travis.yml index ffda774..9b941c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,5 @@ env: global: - secure: Dx2pqzulcauIhVXEqQoNbnwm/EZzam9smHyBd1sPHnnOt5OgikYqnZ5dJOq8gh9wCRCHt1c64NSW0g/04VDofqpBGFifL4eO/c1ZtDioTG4LT+aPXLkt0S7VXP0r2J29JyvcVMBgTi3WRQdBDk4Pd1kYgQlASGSctQmK+Cqw3Jc= - secure: K8Uy8SoQN8S9WuFbeZFBID6XLa7ecgzzB6HoETk91BYJccXTv+VrccnbQ6XYO2cFa5aZyL7QnQjzMRfhjofypr1jvOjVoegfulUGhveypUzEQ1ylnCA8V0bBPHuvMN76ut5fJuUfD49j5dcFjtrdiollZVUiVW1TKjzpJKA4fi0= - - secure: dpj8ThIN7WHFqis/Sy3K2eyI34gjzAyqQcsLy1uz5fM46reSJ6/TMurl12jgPLNototlox6T+HxLTmegu4j3O7JmUeQDAR4S+2nwPbvZmC/s8APKPca5fOEJ2iW8Fyp5MKC13fUjvq+qm/CCr8o0Dmz3QdjIdftDxBsspUGlpEA= + - secure: PubBxjOCOwDBzTthw7ORFCd9elZi4V2/KXi1Nnrs/oz6sNWyXC2//JLJXSBFe5GwSIt8bngbNWuRcS4ckhn0DAPgPRePDtLQ+HzHDW7noFihG3lbHwamp/wXwVQZl18vBqjntt8XoN3UtV8HgoHBV5L5HuL4rtCyMcarkxSVFnI= + - secure: N8fuPmTG2oO51NG7gmulHHhc32Qb3yXS4La9uYjqSkhezhNQDJVpURnjPlXSviC1JmSA3C5TzbJpzSAb80EBAhoyIQmm/G9V+c20eijL+XTs/JYvIAUyz6wqfrtYNMlY1WwYTFmBwzyax+KYij0mguF7uawsuTgTbHGMHBRBdRQ= diff --git a/README.md b/README.md index 181e923..0587e20 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ Conveniently produce authorized [imgflo](https://github.com/jonnor/imgflo) URLs. ## Running tests -Set environment variables for `IMGFLO_KEY`, `IMGFLO_SECRET`, and `IMGFLO_TOKEN` then run `npm test`. +Set environment variables for `IMGFLO_KEY`, `IMGFLO_SECRET`, `IMGFLO_TOKEN_01`, and `IMGFLO_TOKEN_02` then run `npm test`. diff --git a/index.coffee b/index.coffee index 26c52c0..08a9f19 100644 --- a/index.coffee +++ b/index.coffee @@ -9,9 +9,12 @@ path = require 'path' # @param graph [String] The name of the imgflo graph. # @param params [Object] The parameters to pass to imgflo. # @option params input [String] The input URL to pass to imgflo. +# @param format [String] The desired format of the resulting image. e.g. 'jpg' +# or 'png'. If not specified, the extension of the file provided as input is +# used. # @return [String] The imgflo URL. # -imgflo = (config, graph, params) -> +imgflo = (config, graph, params, format) -> throw new Error 'imgflo config object not provided' unless config? {server, key, secret} = config @@ -28,6 +31,9 @@ imgflo = (config, graph, params) -> extension = path.extname(input).match(/^\.(\w+)/)[1] return input if extension is 'gif' + format ?= extension + graph = "#{graph}.#{format}" if format? + query = "?#{qs.stringify(params)}" token = MD5 "#{graph}#{query}#{secret}" diff --git a/spec/imgflo-url.coffee b/spec/imgflo-url.coffee index e05f5d7..575b2ec 100644 --- a/spec/imgflo-url.coffee +++ b/spec/imgflo-url.coffee @@ -118,16 +118,39 @@ describe 'imgflo-url', -> describe 'with imgflo params', -> - it 'should produce a URL', -> - config = - server: 'https://imgflo.herokuapp.com/' - key: process.env.IMGFLO_KEY - secret: process.env.IMGFLO_SECRET - - url = imgflo config, 'gradientmap', - input: 'https://pbs.twimg.com/media/BlM0d2-CcAAT9ic.jpg:large' - color1: '#0A2A2F' - color2: '#FDE7A0' - srgb: true - - expect(url).to.equal "https://imgflo.herokuapp.com/graph/#{process.env.IMGFLO_KEY}/#{process.env.IMGFLO_TOKEN}/gradientmap?input=https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FBlM0d2-CcAAT9ic.jpg%3Alarge&color1=%230A2A2F&color2=%23FDE7A0&srgb=true" + context 'not specifying an image format', -> + + it 'should produce the correct URL', -> + config = + server: 'https://imgflo.herokuapp.com/' + key: process.env.IMGFLO_KEY + secret: process.env.IMGFLO_SECRET + + params = + input: 'https://pbs.twimg.com/media/BlM0d2-CcAAT9ic.jpg:large' + color1: '#0A2A2F' + color2: '#FDE7A0' + srgb: true + + url = imgflo config, 'gradientmap', params + + expect(url).to.equal "https://imgflo.herokuapp.com/graph/#{process.env.IMGFLO_KEY}/#{process.env.IMGFLO_TOKEN_01}/gradientmap.jpg?input=https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FBlM0d2-CcAAT9ic.jpg%3Alarge&color1=%230A2A2F&color2=%23FDE7A0&srgb=true" + + + context 'specifying an image format', -> + + it 'should produce the correct URL', -> + config = + server: 'https://imgflo.herokuapp.com/' + key: process.env.IMGFLO_KEY + secret: process.env.IMGFLO_SECRET + + params = + input: 'https://pbs.twimg.com/media/BlM0d2-CcAAT9ic.jpg:large' + color1: '#0A2A2F' + color2: '#FDE7A0' + srgb: true + + url = imgflo config, 'gradientmap', params, 'png' + + expect(url).to.equal "https://imgflo.herokuapp.com/graph/#{process.env.IMGFLO_KEY}/#{process.env.IMGFLO_TOKEN_02}/gradientmap.png?input=https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FBlM0d2-CcAAT9ic.jpg%3Alarge&color1=%230A2A2F&color2=%23FDE7A0&srgb=true"