Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feature/help-text' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
patsissons committed May 15, 2016
2 parents de30caf + b18661c commit 80ec1c9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/giphy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ class Giphy

@regex = new RegExp "^\\s*(#{Giphy.endpoints.join('|')}|#{Giphy.HelpName})?\\s*(.*?)$", 'i'

constructor: (api) ->
constructor: (robot, api) ->
throw new Error 'Robot is required' if not robot
throw new Error 'Giphy API is required' if not api

@robot = robot
@api = api
@defaultLimit = process.env.HUBOT_GIPHY_DEFAULT_LIMIT or '5'
@defaultEndpoint = process.env.HUBOT_GIPHY_DEFAULT_ENDPOINT or Giphy.SearchEndpointName
@helpText = """
giphy [endpoint] [options...] [args]
#{@robot.name} giphy [endpoint] [options...] [args]
endpoints: search, id, translate, random, trending
options: rating, limit, offset, api
default endpoint is '#{@defaultEndpoint}' if none is specified
options can be specified using /option:value
rating can be one of y,g, pg, pg-13, or r
Example:
giphy search /limit:100 /offset:50 /rating:pg something to search for
#{@robot.name} giphy search /limit:100 /offset:50 /rating:pg something to search for
""".trim()

### istanbul ignore next ###
Expand Down Expand Up @@ -252,7 +255,7 @@ module.exports = (robot) ->
apiKey: process.env.HUBOT_GIPHY_API_KEY
})

giphy = new Giphy api
giphy = new Giphy robot, api

robot.respond /giphy\s*(.*?)\s*$/, (msg) ->
giphy.respond msg
Expand Down
34 changes: 25 additions & 9 deletions test/giphy.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ describe 'giphy', ->
# clone the original environment so we can inject variables
process.env = extend { }, @env

# create a fake robot
robot = { name: 'robot' }
# create a new test giphy api
@api = giphyApi()
# create a new test giphy instance
@giphy = new Giphy @api
@giphy = new Giphy robot, @api

# protect against any real XHR attempts
@fakes.stub @api, '_request', (options, callback) -> callback 'XHR Attempted', null
Expand Down Expand Up @@ -202,33 +204,42 @@ describe 'giphy', ->

describe 'class', ->
describe '.constructor', ->
it 'assigns the provided robot', ->
giphyInstance = new Giphy 'robot', 'api'
should.exist giphyInstance.robot
giphyInstance.robot.should.eql 'robot'

it 'assigns the provided api', ->
giphyInstance = new Giphy 'api'
giphyInstance = new Giphy 'robot', 'api'
should.exist giphyInstance.api
giphyInstance.api.should.eql 'api'

it 'assigns a default endpoint', ->
giphyInstance = new Giphy 'api'
giphyInstance = new Giphy 'robot', 'api'
should.exist giphyInstance.defaultEndpoint
giphyInstance.defaultEndpoint.should.eql Giphy.SearchEndpointName

it 'assigns a default limit', ->
giphyInstance = new Giphy 'api'
giphyInstance = new Giphy 'robot', 'api'
should.exist giphyInstance.defaultLimit
giphyInstance.defaultEndpoint.should.have.length.greaterThan.zero

it 'allows default endpoint override via HUBOT_GIPHY_DEFAULT_ENDPOINT', ->
process.env.HUBOT_GIPHY_DEFAULT_ENDPOINT = 'testing'
giphyInstance = new Giphy 'api'
giphyInstance = new Giphy 'robot', 'api'
giphyInstance.defaultEndpoint.should.eql 'testing'

it 'allows default limit override via HUBOT_GIPHY_DEFAULT_LIMIT', ->
process.env.HUBOT_GIPHY_DEFAULT_LIMIT = '123'
giphyInstance = new Giphy 'api'
giphyInstance = new Giphy 'robot', 'api'
giphyInstance.defaultLimit.should.eql '123'

it 'throws an error if no api is provided', ->
it 'throws an error if no robot is provided', ->
should.throw -> new Giphy()
should.throw -> new Giphy null, 'api'

it 'throws an error if no api is provided', ->
should.throw -> new Giphy 'robot'

describe '.error', ->
beforeEach ->
Expand Down Expand Up @@ -948,7 +959,7 @@ describe 'giphy', ->
msg.send.should.have.callCount 0

beforeEach ->
robot = { respond: @fakes.spy() }
robot = { name: 'robot', respond: @fakes.spy() }
msg = { send: @fakes.spy() }
giphyPluginInstance = hubotGiphy robot
[ regex, callback ] = robot.respond.lastCall.args
Expand All @@ -963,7 +974,7 @@ describe 'giphy', ->
testInput done, @fakes, 'giphy search test1 test2', sampleCollectionResult, { api: 'gifs', endpoint: 'search', query: { q: 'test1 test2' } }

it 'sends a response for "giphy id"', (done) ->
testInput done, @fakes, 'giphy id', sampleCollectionResult, ->
testInput done, @fakes, 'giphy id', null, ->
msg.send.should.have.been.calledWith 'No Id Provided'

it 'sends a response for "giphy id test"', (done) ->
Expand Down Expand Up @@ -1019,3 +1030,8 @@ describe 'giphy', ->

it 'sends a response for "giphy search /limit:123 /offset:25 test"', (done) ->
testInput done, @fakes, 'giphy search /limit:123 /offset:25 test', sampleCollectionResult, { api: 'gifs', endpoint: 'search', query: { limit: '123', offset: '25', q: 'test' } }

it 'sends help text for "giphy help"', (done) ->
testInput done, @fakes, 'giphy help', null, ->
msg.send.should.have.been.called
console.log msg.send.lastCall.args[0]

0 comments on commit 80ec1c9

Please sign in to comment.