Skip to content

Commit

Permalink
Add option for specifying desired output format.
Browse files Browse the repository at this point in the history
* Use input format as default to preserve format since the imgflo
server currently returns jpg if unspecified.
  • Loading branch information
paulyoung committed Oct 29, 2014
1 parent c66b55e commit a8819f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
8 changes: 7 additions & 1 deletion index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"

Expand Down
49 changes: 36 additions & 13 deletions spec/imgflo-url.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit a8819f6

Please sign in to comment.