├── .gitignore ├── LICENSE ├── README.md ├── dist ├── index.js ├── littleBitsAPI.js └── littleBitsAPI.min.js ├── lib ├── customizations.js ├── index.js ├── make-method.js ├── method-process-signature.js ├── route-utils.js ├── routes.js └── utils.js ├── package.json ├── scripts └── download-routes.js ├── test ├── _globals.coffee ├── api-activate.coffee ├── api-deactivate.coffee ├── api-defaults.coffee ├── api-device.coffee ├── api-device_update.coffee ├── api-devices.coffee ├── api-light.coffee ├── api-output.coffee ├── api-subscribe.coffee ├── api-subscriptions.coffee ├── api-unsubscribe.coffee ├── http-errors-to-runtime-errors.coffee └── method-process-signature.coffee └── testem.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | components 27 | build -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cloud-api-http-client [![Circle CI](https://circleci.com/gh/littlebits/cloud-client-api-http.svg?style=svg)](https://circleci.com/gh/littlebits/cloud-client-api-http) 2 | 3 | Lightweight wrapper for littleBits Cloud HTTP API 4 | 5 | 6 | 7 | ## Installation 8 | 9 | ``` 10 | npm install --save @littlebits/cloud-http 11 | ``` 12 | 13 | 14 | 15 | ## Example 16 | 17 | ```js 18 | var api = require('littlebits-cloud-http') 19 | .defaults({ access_token: 'askdjfldaksjfkdlsjfdkl234324' }) 20 | // We've created a new instance of our api with an access_token to be used by all requests. 21 | 22 | api = api.defaults({ device_id: 'foobar' }) 23 | // All api functions will now default to using device_id 24 | // 'foobar' for those abstracting HTTP endpoints that use 25 | // the {device_id} path-param (e.g. /devices/{device_id}/output). 26 | 27 | api.output() 28 | // -> 29 | // Neat; By default functions log to console 30 | // which is great for playing around. 31 | 32 | api.output('foobar2') 33 | // The device_id can also be specified as the 34 | // first argument (overriding defaults, not mutating), 35 | // allowing the second argument to focus on 36 | // your payload... 37 | 38 | api.output('foobar2', { percent: 50, duration_ms: 5000 }) 39 | // like so :). Of course you can work with objects only 40 | // as needed/desired... 41 | 42 | api.output({device_id: 'foobar2', percent: 50, duration_ms: 5000 }) 43 | // Hm, these arguments are getting repetitive, lets stop this. 44 | 45 | var output = api.output.defaults({device_id: 'foobar2', percent: 50, duration_ms: 5000 }) 46 | // Better. Now we have a output function with new defaults. 47 | // and we can always call .defaults again should we wish. 48 | // Lets use it now. 49 | 50 | output(function(err, result){ 51 | if (err) ... 52 | 53 | ... 54 | }); 55 | // Pass your own callback to handle the 56 | // the io error and/or return value. This of course 57 | // elliminates the default logging seen above. 58 | 59 | 60 | 61 | // Helpful notes about .defaults() 62 | 63 | // 1. it does not mutate 64 | // - .defaults() on the api object returns an entirely new api 65 | // - .defaults() on a function returns an entirely new function 66 | assert api.defaults({...}) !== api 67 | 68 | // 2. it is infinitly recrusive so this is possible (but psychotic) 69 | api.defaults({...}).defaults({...})/* ad infinitum*/.output.defaults({...}).defaults({...}) /* ad infinitum */ 70 | 71 | // 3. defaults can be introspected by passing no arguments, e.g. given a stock api instnace 72 | api.defaults() 73 | // -> { host: 'https://api-http.littlebitscloud.cc', version: '2' } 74 | 75 | 76 | 77 | // Fin. 78 | 79 | // You've seen several different function signatures. 80 | // They are at your disposal to choose as desired. 81 | // See API docs for more details and Have fun! 82 | ``` 83 | 84 | 85 | 86 | ## API Functions 87 | 88 | Abbreviated information. See [*API Function Signatures*](#api-function-signatures) for details about the argument pattern uniformly accepted. 89 | 90 | ---- 91 | #### .defaults() 92 | 93 | See [*Defaults System*](#defaults-system). 94 | 95 | Key Points: 96 | - Supports snake_case or camelCase key names 97 | - Returns new instances, does not mutate existing defaults 98 | - Returns current defaults if invoked with no arguments 99 | 100 | 101 | ---- 102 | #### .devices() 103 | 104 | Get information for all devices. 105 | 106 | `callback` result argument: `[Device]` 107 | 108 | 109 | ---- 110 | #### .device() 111 | 112 | Get information for one device. 113 | 114 | `options` arguments 115 | 116 | - `device_id :: String` 117 | 118 | `callback` result argument: `Device` 119 | 120 | 121 | ---- 122 | #### .activate() 123 | 124 | Activate one device. (FYI: associates device to user; User id is attained from the provided access_token) 125 | 126 | `options` arguments 127 | 128 | - `device_id :: String` 129 | 130 | `callback` result argument: `Device` 131 | 132 | 133 | ---- 134 | #### .deactivate() 135 | 136 | Dectivate one device. 137 | 138 | `options` arguments 139 | 140 | - `device_id :: String` 141 | 142 | `callback` result argument: `Device` 143 | 144 | 145 | ---- 146 | #### .output() 147 | 148 | Send amplitude out of the device. 149 | 150 | `options` arguments: 151 | 152 | - `percent :: Float | >= 0, <= 100` 153 | - `duration_ms :: Integer | >= 0` 154 | - `device_id :: String` 155 | 156 | `callback` result argument: ***TODO*** 157 | 158 | 159 | ---- 160 | #### .light() 161 | 162 | Activate device light. 163 | 164 | `options` arguments: 165 | 166 | - `color :: String | "green" "yellow" "red" "blue" "purple" "white" "cyan" "clownbarf"` 167 | - `mode :: String | "blink" "hold"` 168 | - `duration_ms :: Integer | >= 0` 169 | - `device_id :: String` 170 | 171 | `callback` result argument: ***TODO*** 172 | 173 | 174 | ---- 175 | #### .subscriptions() 176 | 177 | Get the subscriptions for given sub/pub. 178 | 179 | `options` arguments: 180 | 181 | - `?` `subscriber_id :: device_id || uri` 182 | Filter subscriptions to those where `subscriber_id` matches. 183 | - `?` `publisher_id :: device_id` 184 | Filter subscriptions to those where `publisher_id` matches. 185 | 186 | `callback` result argument: `[Subscription]` 187 | 188 | 189 | ---- 190 | #### .unsubscribe() 191 | 192 | Delete the subscription for given sub/pub. 193 | 194 | `options` arguments: 195 | 196 | - `subscriber_id :: device_id || uri` 197 | - `publisher_id :: device_id` 198 | 199 | `callback` result argument: ***TODO*** 200 | 201 | 202 | ---- 203 | #### .subscribe() 204 | 205 | Create a new subscription for given sub/pub. 206 | 207 | `options` arguments: 208 | 209 | - `subscriber_id :: device_id || uri` 210 | - `publisher_id :: device_id` 211 | - `?` `publisher_events :: [Event]` 212 | Defaults to: `["amplitude:delta:ignite"]` 213 | 214 | `callback` result argument: ***TODO*** 215 | 216 | 217 | 218 | ## Types 219 | 220 | ##### Event 221 | ``` 222 | A String of any following value: 223 | 224 | "amplitude" 225 | "amplitude:delta:sustain" 226 | "amplitude:delta:ignite" 227 | "amplitude:delta:release" 228 | "amplitude:delta:nap" 229 | "amplitude:level:active" 230 | "amplitude:level:idle" 231 | ``` 232 | 233 | ##### Subscription 234 | ``` 235 | { 236 | subscriber_id :: device_id || URI 237 | publisher_id :: device_id 238 | publisher_events :: [Event] 239 | } 240 | ``` 241 | 242 | ##### Device 243 | ``` 244 | { 245 | id :: String 246 | user_id :: Integer 247 | label :: String 248 | subscribers :: [Subscription] 249 | subscriptions :: [Subscription] 250 | } 251 | ``` 252 | 253 | 254 | 255 | ## API Function Signatures 256 | 257 | All API functions (***except `.defaults`***) are variadic-polymorphic, following these defintions. 258 | 259 | ##### Arity 0 260 | ``` 261 | f :: -> void 262 | ``` 263 | ##### e.g.: 264 | ```js 265 | api.output() 266 | ``` 267 | 268 | 269 | ---- 270 | ##### Arity 1 271 | ``` 272 | f :: (err, * -> void) -> void 273 | ``` 274 | ``` 275 | f :: ID -> void 276 | ``` 277 | ``` 278 | f :: Options -> void 279 | ``` 280 | ##### e.g.: 281 | ```js 282 | api.output(function(err, result){ /*...*/ }) 283 | api.output('foobar-device-id') 284 | api.output({ device_id: 'foobar-device-id', duration_ms: 500 }) 285 | ``` 286 | 287 | 288 | ---- 289 | ##### Arity 2 290 | ``` 291 | f :: ID, (err, * -> void) -> void 292 | ``` 293 | ``` 294 | f :: Options, (err, * -> void) -> void 295 | ``` 296 | ``` 297 | f :: ID, Options -> void 298 | ``` 299 | ##### e.g.: 300 | ```js 301 | api.output('foobar-device-id', function(err, result){ /*...*/ }) 302 | api.output({ device_id: 'foobar-device-id', duration_ms: 500 }, function(err, result){ /*...*/ }) 303 | api.output('foobar-device-id', { duration_ms: 500 }) 304 | ``` 305 | 306 | 307 | ---- 308 | ##### Arity 3 309 | ``` 310 | f :: ID, Options, (err, * -> void) -> void 311 | ``` 312 | ##### e.g.: 313 | ```js 314 | api.output('foobar-device-id', { duration_ms: 500 }, function(err, result){ /*...*/ }) 315 | ``` 316 | For all arity cases: 317 | 318 | - `Options` arguments that you omit are patched by defaults. You may customize defaults (see next section). 319 | 320 | - `ID` refers to `device_id` which can alternatively be argued in `Options` as `device_id`. Certain functions do not need a `device_id`, such as `subscribe` in which case it is useless (but harmless), to argue this. 321 | 322 | - `callback` is patched by `console.log`. This is useful for rapid manual tests, playing around, etc. It also helps keep track of unhandled `callbacks` which you *should* be handling in non-trivial work. If you really want a `noop` then just argue one. 323 | 324 | 325 | ## Defaults system 326 | 327 | #### .defaults() 328 | 329 | ##### get 330 | ``` 331 | defaults :: -> Options 332 | ``` 333 | Get the current defaults. 334 | 335 | ##### set 336 | 337 | ``` 338 | defaults :: Options -> API 339 | ``` 340 | Create a new api with new defaults. Existing api instance is not mutated. All functions of new api instance will read from these new defaults. 341 | 342 | This is a good place to argue `access_token` etc. 343 | 344 | `options` arguments: 345 | 346 | - `?` `host :: URI` 347 | The HTTP server to make requests against. By default equals `'api-http.littlebitscloud.cc'` but you may override as desired, e.g. work against local development servers. 348 | - `?` `version :: String` 349 | The littleBits Cloud HTTP API version to use. By default equals `'2'`. 350 | - `?` `access_token :: String` 351 | The OAuth access_token that will be used by the server to authorize your requsts. 352 | 353 | 354 | 355 | #### .{api_function}.defaults() 356 | ##### get 357 | ``` 358 | defaults :: -> Options 359 | ``` 360 | Get the current defaults. 361 | 362 | ##### set 363 | 364 | ``` 365 | defaults :: Options -> API_Function 366 | ``` 367 | Create a new api function (f) with new defaults. Existing f is not mutated. 368 | 369 | `options` arguments: See docs for each function respectively. 370 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | window.littleBitsAPI = require('../lib/index') 2 | -------------------------------------------------------------------------------- /dist/littleBitsAPI.min.js: -------------------------------------------------------------------------------- 1 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;olength?0:length+start}end=end===undefined||end>length?length:+end||0;if(end<0){end+=length}length=start>end?0:end-start>>>0;start>>>=0;var result=Array(length);while(++indexarrLength)){return false}while(++index-1&&value%1==0&&value<=MAX_SAFE_INTEGER}function isArguments(value){return isObjectLike(value)&&isArrayLike(value)&&hasOwnProperty.call(value,"callee")&&!propertyIsEnumerable.call(value,"callee")}module.exports=isArguments},{}],18:[function(require,module,exports){var arrayTag="[object Array]",funcTag="[object Function]";var reIsHostCtor=/^\[object .+?Constructor\]$/;function isObjectLike(value){return!!value&&typeof value=="object"}var objectProto=Object.prototype;var fnToString=Function.prototype.toString;var hasOwnProperty=objectProto.hasOwnProperty;var objToString=objectProto.toString;var reIsNative=RegExp("^"+fnToString.call(hasOwnProperty).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");var nativeIsArray=getNative(Array,"isArray");var MAX_SAFE_INTEGER=9007199254740991;function getNative(object,key){var value=object==null?undefined:object[key];return isNative(value)?value:undefined}function isLength(value){return typeof value=="number"&&value>-1&&value%1==0&&value<=MAX_SAFE_INTEGER}var isArray=nativeIsArray||function(value){return isObjectLike(value)&&isLength(value.length)&&objToString.call(value)==arrayTag};function isFunction(value){return isObject(value)&&objToString.call(value)==funcTag}function isObject(value){var type=typeof value;return!!value&&(type=="object"||type=="function")}function isNative(value){if(value==null){return false}if(isFunction(value)){return reIsNative.test(fnToString.call(value))}return isObjectLike(value)&&reIsHostCtor.test(value)}module.exports=isArray},{}],19:[function(require,module,exports){var funcTag="[object Function]";var objectProto=Object.prototype;var objToString=objectProto.toString;function isFunction(value){return isObject(value)&&objToString.call(value)==funcTag}function isObject(value){var type=typeof value;return!!value&&(type=="object"||type=="function")}module.exports=isFunction},{}],20:[function(require,module,exports){var argsTag="[object Arguments]",arrayTag="[object Array]",boolTag="[object Boolean]",dateTag="[object Date]",errorTag="[object Error]",funcTag="[object Function]",mapTag="[object Map]",numberTag="[object Number]",objectTag="[object Object]",regexpTag="[object RegExp]",setTag="[object Set]",stringTag="[object String]",weakMapTag="[object WeakMap]";var arrayBufferTag="[object ArrayBuffer]",float32Tag="[object Float32Array]",float64Tag="[object Float64Array]",int8Tag="[object Int8Array]",int16Tag="[object Int16Array]",int32Tag="[object Int32Array]",uint8Tag="[object Uint8Array]",uint8ClampedTag="[object Uint8ClampedArray]",uint16Tag="[object Uint16Array]",uint32Tag="[object Uint32Array]";var typedArrayTags={};typedArrayTags[float32Tag]=typedArrayTags[float64Tag]=typedArrayTags[int8Tag]=typedArrayTags[int16Tag]=typedArrayTags[int32Tag]=typedArrayTags[uint8Tag]=typedArrayTags[uint8ClampedTag]=typedArrayTags[uint16Tag]=typedArrayTags[uint32Tag]=true;typedArrayTags[argsTag]=typedArrayTags[arrayTag]=typedArrayTags[arrayBufferTag]=typedArrayTags[boolTag]=typedArrayTags[dateTag]=typedArrayTags[errorTag]=typedArrayTags[funcTag]=typedArrayTags[mapTag]=typedArrayTags[numberTag]=typedArrayTags[objectTag]=typedArrayTags[regexpTag]=typedArrayTags[setTag]=typedArrayTags[stringTag]=typedArrayTags[weakMapTag]=false;function isObjectLike(value){return!!value&&typeof value=="object"}var objectProto=Object.prototype;var objToString=objectProto.toString;var MAX_SAFE_INTEGER=9007199254740991;function isLength(value){return typeof value=="number"&&value>-1&&value%1==0&&value<=MAX_SAFE_INTEGER}function isTypedArray(value){return isObjectLike(value)&&isLength(value.length)&&!!typedArrayTags[objToString.call(value)]}module.exports=isTypedArray},{}],21:[function(require,module,exports){var getNative=require("lodash._getnative"),isArguments=require("lodash.isarguments"),isArray=require("lodash.isarray");var reIsUint=/^\d+$/;var objectProto=Object.prototype;var hasOwnProperty=objectProto.hasOwnProperty;var nativeKeys=getNative(Object,"keys");var MAX_SAFE_INTEGER=9007199254740991;function baseProperty(key){return function(object){return object==null?undefined:object[key]}}var getLength=baseProperty("length");function isArrayLike(value){return value!=null&&isLength(getLength(value))}function isIndex(value,length){value=typeof value=="number"||reIsUint.test(value)?+value:-1;length=length==null?MAX_SAFE_INTEGER:length;return value>-1&&value%1==0&&value-1&&value%1==0&&value<=MAX_SAFE_INTEGER}function shimKeys(object){var props=keysIn(object),propsLength=props.length,length=propsLength&&object.length;var allowIndexes=!!length&&isLength(length)&&(isArray(object)||isArguments(object));var index=-1,result=[];while(++index0;while(++index=200&&res.status<300){return self.callback(err,res)}var new_err=new Error(res.statusText||"Unsuccessful HTTP response");new_err.original=err;new_err.response=res;new_err.status=res.status;self.callback(new_err,res)})}Emitter(Request.prototype);Request.prototype.use=function(fn){fn(this);return this};Request.prototype.timeout=function(ms){this._timeout=ms;return this};Request.prototype.clearTimeout=function(){this._timeout=0;clearTimeout(this._timer);return this};Request.prototype.abort=function(){if(this.aborted)return;this.aborted=true;this.xhr.abort();this.clearTimeout();this.emit("abort");return this};Request.prototype.set=function(field,val){if(isObject(field)){for(var key in field){this.set(key,field[key])}return this}this._header[field.toLowerCase()]=val;this.header[field]=val;return this};Request.prototype.unset=function(field){delete this._header[field.toLowerCase()];delete this.header[field];return this};Request.prototype.getHeader=function(field){return this._header[field.toLowerCase()]};Request.prototype.type=function(type){this.set("Content-Type",request.types[type]||type);return this};Request.prototype.accept=function(type){this.set("Accept",request.types[type]||type);return this};Request.prototype.auth=function(user,pass){var str=btoa(user+":"+pass);this.set("Authorization","Basic "+str);return this};Request.prototype.query=function(val){if("string"!=typeof val)val=serialize(val);if(val)this._query.push(val);return this};Request.prototype.field=function(name,val){if(!this._formData)this._formData=new root.FormData;this._formData.append(name,val);return this};Request.prototype.attach=function(field,file,filename){if(!this._formData)this._formData=new root.FormData;this._formData.append(field,file,filename);return this};Request.prototype.send=function(data){var obj=isObject(data);var type=this.getHeader("Content-Type");if(obj&&isObject(this._data)){for(var key in data){this._data[key]=data[key]}}else if("string"==typeof data){if(!type)this.type("form");type=this.getHeader("Content-Type");if("application/x-www-form-urlencoded"==type){this._data=this._data?this._data+"&"+data:data}else{this._data=(this._data||"")+data}}else{this._data=data}if(!obj||isHost(data))return this;if(!type)this.type("json");return this};Request.prototype.callback=function(err,res){var fn=this._callback;this.clearTimeout();fn(err,res)};Request.prototype.crossDomainError=function(){var err=new Error("Origin is not allowed by Access-Control-Allow-Origin");err.crossDomain=true;this.callback(err)};Request.prototype.timeoutError=function(){var timeout=this._timeout;var err=new Error("timeout of "+timeout+"ms exceeded");err.timeout=timeout;this.callback(err)};Request.prototype.withCredentials=function(){this._withCredentials=true;return this};Request.prototype.end=function(fn){var self=this;var xhr=this.xhr=request.getXHR();var query=this._query.join("&");var timeout=this._timeout;var data=this._formData||this._data;this._callback=fn||noop;xhr.onreadystatechange=function(){if(4!=xhr.readyState)return;var status;try{status=xhr.status}catch(e){status=0}if(0==status){if(self.timedout)return self.timeoutError();if(self.aborted)return;return self.crossDomainError()}self.emit("end")};var handleProgress=function(e){if(e.total>0){e.percent=e.loaded/e.total*100}self.emit("progress",e)};if(this.hasListeners("progress")){xhr.onprogress=handleProgress}try{if(xhr.upload&&this.hasListeners("progress")){xhr.upload.onprogress=handleProgress}}catch(e){}if(timeout&&!this._timer){this._timer=setTimeout(function(){self.timedout=true;self.abort()},timeout)}if(query){query=request.serializeObject(query);this.url+=~this.url.indexOf("?")?"&"+query:"?"+query}xhr.open(this.method,this.url,true);if(this._withCredentials)xhr.withCredentials=true;if("GET"!=this.method&&"HEAD"!=this.method&&"string"!=typeof data&&!isHost(data)){var contentType=this.getHeader("Content-Type");var serialize=request.serialize[contentType?contentType.split(";")[0]:""];if(serialize)data=serialize(data)}for(var field in this.header){if(null==this.header[field])continue;xhr.setRequestHeader(field,this.header[field])}this.emit("request",this);xhr.send(data);return this};Request.prototype.then=function(fulfill,reject){return this.end(function(err,res){err?reject(err):fulfill(res)})};request.Request=Request;function request(method,url){if("function"==typeof url){return new Request("GET",method).end(url)}if(1==arguments.length){return new Request("GET",method)}return new Request(method,url)}request.get=function(url,data,fn){var req=request("GET",url);if("function"==typeof data)fn=data,data=null;if(data)req.query(data);if(fn)req.end(fn);return req};request.head=function(url,data,fn){var req=request("HEAD",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.del=function(url,fn){var req=request("DELETE",url);if(fn)req.end(fn);return req};request.patch=function(url,data,fn){var req=request("PATCH",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.post=function(url,data,fn){var req=request("POST",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};request.put=function(url,data,fn){var req=request("PUT",url);if("function"==typeof data)fn=data,data=null;if(data)req.send(data);if(fn)req.end(fn);return req};module.exports=request},{emitter:9,reduce:25}],27:[function(require,module,exports){module.exports=toNoCase;var hasSpace=/\s/;var hasCamel=/[a-z][A-Z]/;var hasSeparator=/[\W_]/;function toNoCase(string){if(hasSpace.test(string))return string.toLowerCase();if(hasSeparator.test(string))string=unseparate(string);if(hasCamel.test(string))string=uncamelize(string);return string.toLowerCase()}var separatorSplitter=/[\W_]+(.|$)/g;function unseparate(string){return string.replace(separatorSplitter,function(m,next){return next?" "+next:""})}var camelSplitter=/(.)([A-Z]+)/g;function uncamelize(string){return string.replace(camelSplitter,function(m,previous,uppers){return previous+" "+uppers.toLowerCase().split("").join(" ")})}},{}],28:[function(require,module,exports){var toSpace=require("to-space-case");module.exports=toSnakeCase;function toSnakeCase(string){return toSpace(string).replace(/\s/g,"_")}},{"to-space-case":29}],29:[function(require,module,exports){var clean=require("to-no-case");module.exports=toSpaceCase;function toSpaceCase(string){return clean(string).replace(/[\W_]+(.|$)/g,function(matches,match){return match?" "+match:""})}},{"to-no-case":27}]},{},[1]); 3 | -------------------------------------------------------------------------------- /lib/customizations.js: -------------------------------------------------------------------------------- 1 | var F = require('./utils') 2 | 3 | 4 | 5 | exports.opt_to_pp = opt_to_pp; 6 | 7 | 8 | 9 | var table = exports.table = { 10 | '/devices': { 11 | get: { 12 | fname: 'devices' 13 | } 14 | }, 15 | '/devices/{device_id}': { 16 | get: { 17 | fname: 'device' 18 | }, 19 | 'delete': { 20 | fname: 'deactivate' 21 | }, 22 | 'post': { 23 | fname: 'activate' 24 | }, 25 | 'put': { 26 | fname: 'device_update' 27 | } 28 | }, 29 | '/devices/{device_id}/output': { 30 | post: { 31 | fname: 'output' 32 | } 33 | }, 34 | '/devices/{device_id}/light': { 35 | post: { 36 | fname: 'light' 37 | } 38 | }, 39 | '/subscriptions': { 40 | post: { 41 | fname: 'subscribe' 42 | }, 43 | 'delete': { 44 | fname: 'unsubscribe' 45 | }, 46 | get: { 47 | fname: 'subscriptions' 48 | } 49 | } 50 | }; 51 | 52 | 53 | 54 | function opt_to_pp(path, method, pp_name, opts) { 55 | method = method.toLowerCase(); 56 | var cust = (table[path] && table[path][method] && table[path][method][pp_name]) || {}; 57 | var name_used = opts[cust.rename] ? cust.rename : pp_name; 58 | var value = opts[name_used] 59 | return F.isExist(value) ? (cust.trans ? cust.trans(value, name_used) : value) : null ; 60 | } 61 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var merge = require('merge') 2 | var make_method = require('./make-method') 3 | var routes = require('./routes') 4 | var rutils = require('./route-utils') 5 | 6 | var root_defaults = { 7 | host: 'https://api-http.littlebitscloud.cc', 8 | version: '2' 9 | } 10 | 11 | 12 | 13 | module.exports = API(root_defaults) 14 | 15 | function API(base_defaults) { 16 | 17 | var api = routes.reduce(function(acc, route) { 18 | var name = rutils.calc_fname(route) 19 | if (name) { 20 | acc[name] = make_method(base_defaults, rutils.functionify(route)) 21 | } 22 | return acc 23 | }, {}) 24 | 25 | api.defaults = function defaults(new_defaults){ 26 | if (arguments.length === 0) return base_defaults 27 | return API(merge({}, base_defaults, new_defaults)) 28 | } 29 | 30 | return api 31 | } 32 | 33 | 34 | 35 | // Private 36 | 37 | module.exports._process_signature = require('./method-process-signature') 38 | -------------------------------------------------------------------------------- /lib/make-method.js: -------------------------------------------------------------------------------- 1 | var process_signature = require('./method-process-signature') 2 | var merge = require('merge') 3 | 4 | 5 | 6 | module.exports = make_method 7 | 8 | function make_method(base_defaults, f) { 9 | // Give this function a better name. We could 10 | // accept a name parameter and create the function 11 | // using Function constructor. 12 | function todo_name_me() { 13 | var args = process_signature(arguments) 14 | var opts = merge({}, base_defaults, args.overrides) 15 | return f.apply(null, [opts, args.cb]) 16 | } 17 | 18 | todo_name_me.defaults = function(base_defaults_method) { 19 | if (arguments.length === 0) return base_defaults 20 | return make_method(merge({}, base_defaults, base_defaults_method), f) 21 | } 22 | 23 | return todo_name_me 24 | } 25 | -------------------------------------------------------------------------------- /lib/method-process-signature.js: -------------------------------------------------------------------------------- 1 | var snakeify = require('to-snake-case') 2 | var transform = require('lodash.transform') 3 | var merge = require('merge') 4 | var default_cb = (console && console.log && console.log.bind(console)) || function noop(){}; 5 | 6 | 7 | 8 | module.exports = process_signature 9 | 10 | function process_signature(args) { 11 | var args_ = {}; 12 | 13 | if (typeof args[0] === 'function') { 14 | 15 | args_.overrides = {}; 16 | args_.cb = args[0]; 17 | 18 | } else if (typeof args[0] === 'string') { 19 | 20 | var overrides = typeof args[1] === 'object' ? args[1] : null 21 | args_.overrides = merge({device_id: args[0]}, normalize_key_case(overrides || {})); 22 | args_.cb = overrides 23 | ? (args[2] || default_cb) 24 | : (args[1] || default_cb) 25 | 26 | } else { 27 | 28 | args_.overrides = normalize_key_case(args[0] || {}) 29 | args_.cb = args[1] || default_cb 30 | 31 | } 32 | 33 | return args_ 34 | } 35 | 36 | 37 | 38 | function normalize_key_case(options) { 39 | return transform(options, do_snakeify, {}) 40 | 41 | function do_snakeify(o, v, k) { 42 | o[snakeify(k)] = v 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lib/route-utils.js: -------------------------------------------------------------------------------- 1 | var F = require('./utils') 2 | var cust = require('./customizations'); 3 | var sa = require('superagent'); 4 | 5 | 6 | 7 | exports.calc_fname = route_calc_fname; 8 | exports.path_interpolate = route_path_interpolate; 9 | exports.functionify = route_functionify; 10 | 11 | 12 | 13 | function route_functionify(route){ 14 | return function(opts, cb){ 15 | var uri = route_join(opts.host, route_path_interpolate(route, [opts.device_id])); 16 | 17 | // Instantiate a http request 18 | var r = sa(route.method, uri); 19 | if (route.auth && opts.access_token) r.set('Authorization', 'Bearer '+ opts.access_token); 20 | r.set('Accept', 'application/vnd.littlebits.v'+ opts.version +'+json'); 21 | 22 | // Custom payload data can be provided by the user under the payload key 23 | if (opts.payload) r.set(opts.payload); 24 | 25 | // For each known valid endpoint payload/query param, 26 | // look for provided data in opts. 27 | var add_params = param_adder(route.path, route.method, r, opts); 28 | if (route.payloadParams.children) { 29 | route.payloadParams.children.forEach(add_params); 30 | } 31 | if (route.queryParams.children) { 32 | route.queryParams.children.forEach(add_params); 33 | } 34 | 35 | // Do the HTTP request 36 | r.end(function(err, response){ 37 | if (err) { 38 | var error 39 | try { 40 | error = JSON.parse(err.response.error.text) 41 | error = error.message ? error : err.response.error 42 | } catch (e) { 43 | error = err.response.error 44 | } 45 | return cb(error); 46 | } 47 | if (response.error) return cb(extract_err(response)); 48 | cb(null, response.body); 49 | }); 50 | }; 51 | } 52 | 53 | 54 | 55 | function route_path_interpolate(route, path_args){ 56 | return path_args.reduce(function(path, arg){ 57 | return path.replace(/{[^}]*}/, arg); 58 | }, route.path); 59 | } 60 | 61 | 62 | 63 | function route_calc_fname(route){ 64 | var custom_name = cust.table[route.path] && 65 | cust.table[route.path][route.method.toLowerCase()] && 66 | cust.table[route.path][route.method.toLowerCase()].fname ; 67 | return custom_name ? custom_name : null ; 68 | } 69 | 70 | 71 | 72 | // Private 73 | 74 | function param_adder(path, method, request, request_opts){ 75 | return function(param){ 76 | var value = cust.opt_to_pp(path, method, param.name, request_opts); 77 | if (F.isExist(value)) { 78 | var o = {}; 79 | o[param.name] = value; 80 | request[method === 'GET' ? 'query' : 'send'](o); 81 | } 82 | }; 83 | } 84 | 85 | 86 | 87 | function route_join(){ 88 | var path = []; 89 | for (var i = 0; i < arguments.length; i++) { 90 | path.push(arguments[i].replace(/^\//, '').replace(/\/$/, '')); 91 | } 92 | return path.join('/'); 93 | } 94 | 95 | 96 | 97 | function extract_err(res){ 98 | var err = res.error; 99 | if (res.body && res.body.message) err.message = res.body.message; 100 | return err; 101 | } 102 | -------------------------------------------------------------------------------- /lib/routes.js: -------------------------------------------------------------------------------- 1 | 2 | // Generated from scripts/download-routes.js on Wed Jun 11 2014 00:22:37 GMT-0400 (EDT) 3 | 4 | module.exports = [ 5 | { 6 | "path": "/ping", 7 | "method": "GET", 8 | "auth": false, 9 | "pathParams": { 10 | "type": "any", 11 | "required": false, 12 | "allowedValues": null, 13 | "disallowedValues": null 14 | }, 15 | "queryParams": { 16 | "type": "any", 17 | "required": false, 18 | "allowedValues": null, 19 | "disallowedValues": null 20 | }, 21 | "payloadParams": { 22 | "type": "any", 23 | "required": false, 24 | "allowedValues": null, 25 | "disallowedValues": null 26 | }, 27 | "responseParams": null, 28 | "version": null 29 | }, 30 | { 31 | "path": "/devices", 32 | "method": "GET", 33 | "auth": [ 34 | "bearer" 35 | ], 36 | "pathParams": { 37 | "type": "any", 38 | "required": false, 39 | "allowedValues": null, 40 | "disallowedValues": null 41 | }, 42 | "queryParams": { 43 | "type": "any", 44 | "required": false, 45 | "allowedValues": null, 46 | "disallowedValues": null 47 | }, 48 | "payloadParams": { 49 | "type": "any", 50 | "required": false, 51 | "allowedValues": null, 52 | "disallowedValues": null 53 | }, 54 | "responseParams": null, 55 | "version": "2" 56 | }, 57 | { 58 | "path": "/devices/{device_id}", 59 | "method": "GET", 60 | "auth": [ 61 | "bearer" 62 | ], 63 | "pathParams": { 64 | "type": "any", 65 | "required": false, 66 | "allowedValues": null, 67 | "disallowedValues": null 68 | }, 69 | "queryParams": { 70 | "type": "any", 71 | "required": false, 72 | "allowedValues": null, 73 | "disallowedValues": null 74 | }, 75 | "payloadParams": { 76 | "type": "any", 77 | "required": false, 78 | "allowedValues": null, 79 | "disallowedValues": null 80 | }, 81 | "responseParams": null, 82 | "version": "2" 83 | }, 84 | { 85 | "path": "/devices/{device_id}", 86 | "method": "POST", 87 | "auth": [ 88 | "bearer" 89 | ], 90 | "pathParams": { 91 | "type": "any", 92 | "required": false, 93 | "allowedValues": null, 94 | "disallowedValues": null 95 | }, 96 | "queryParams": { 97 | "type": "any", 98 | "required": false, 99 | "allowedValues": null, 100 | "disallowedValues": null 101 | }, 102 | "payloadParams": { 103 | "type": "object", 104 | "required": null, 105 | "allowedValues": null, 106 | "disallowedValues": null, 107 | "children": [ 108 | { 109 | "name": "device_hash", 110 | "type": "string", 111 | "required": true, 112 | "allowedValues": null, 113 | "disallowedValues": null 114 | }, 115 | { 116 | "name": "update", 117 | "type": "object", 118 | "required": null, 119 | "allowedValues": null, 120 | "disallowedValues": null, 121 | "children": [ 122 | { 123 | "name": "label", 124 | "type": "string", 125 | "required": true, 126 | "allowedValues": null, 127 | "disallowedValues": null 128 | } 129 | ] 130 | } 131 | ] 132 | }, 133 | "responseParams": null, 134 | "version": "2" 135 | }, 136 | { 137 | "path": "/devices/{device_id}", 138 | "method": "PUT", 139 | "auth": [ 140 | "bearer" 141 | ], 142 | "pathParams": { 143 | "type": "any", 144 | "required": false, 145 | "allowedValues": null, 146 | "disallowedValues": null 147 | }, 148 | "queryParams": { 149 | "type": "any", 150 | "required": false, 151 | "allowedValues": null, 152 | "disallowedValues": null 153 | }, 154 | "payloadParams": { 155 | "type": "object", 156 | "required": null, 157 | "allowedValues": null, 158 | "disallowedValues": null, 159 | "children": [ 160 | { 161 | "name": "update", 162 | "type": "object", 163 | "required": true, 164 | "allowedValues": null, 165 | "disallowedValues": null, 166 | "children": [ 167 | { 168 | "name": "label", 169 | "type": "string", 170 | "required": null, 171 | "allowedValues": null, 172 | "disallowedValues": null 173 | } 174 | ] 175 | } 176 | ] 177 | }, 178 | "responseParams": null, 179 | "version": "2" 180 | }, 181 | { 182 | "path": "/devices/{device_id}", 183 | "method": "DELETE", 184 | "auth": [ 185 | "bearer" 186 | ], 187 | "pathParams": { 188 | "type": "any", 189 | "required": false, 190 | "allowedValues": null, 191 | "disallowedValues": null 192 | }, 193 | "queryParams": { 194 | "type": "any", 195 | "required": false, 196 | "allowedValues": null, 197 | "disallowedValues": null 198 | }, 199 | "payloadParams": { 200 | "type": "any", 201 | "required": false, 202 | "allowedValues": null, 203 | "disallowedValues": null 204 | }, 205 | "responseParams": null, 206 | "version": "2" 207 | }, 208 | { 209 | "path": "/devices/{device_id}/light", 210 | "method": "POST", 211 | "auth": [ 212 | "bearer" 213 | ], 214 | "pathParams": { 215 | "type": "any", 216 | "required": false, 217 | "allowedValues": null, 218 | "disallowedValues": null 219 | }, 220 | "queryParams": { 221 | "type": "any", 222 | "required": false, 223 | "allowedValues": null, 224 | "disallowedValues": null 225 | }, 226 | "payloadParams": { 227 | "type": "object", 228 | "required": null, 229 | "allowedValues": null, 230 | "disallowedValues": null, 231 | "children": [ 232 | { 233 | "name": "mode", 234 | "type": "string", 235 | "required": false, 236 | "allowedValues": [ 237 | "blink", 238 | "hold" 239 | ], 240 | "disallowedValues": null 241 | }, 242 | { 243 | "name": "color", 244 | "type": "string", 245 | "required": false, 246 | "allowedValues": [ 247 | "green", 248 | "yellow", 249 | "red", 250 | "blue", 251 | "purple", 252 | "white", 253 | "cyan", 254 | "clownbarf" 255 | ], 256 | "disallowedValues": null 257 | }, 258 | { 259 | "name": "duration_ms", 260 | "type": "number", 261 | "required": null, 262 | "allowedValues": null, 263 | "disallowedValues": null 264 | } 265 | ] 266 | }, 267 | "responseParams": null, 268 | "version": "2" 269 | }, 270 | { 271 | "path": "/devices/{device_id}/output", 272 | "method": "POST", 273 | "auth": [ 274 | "bearer" 275 | ], 276 | "pathParams": { 277 | "type": "any", 278 | "required": false, 279 | "allowedValues": null, 280 | "disallowedValues": null 281 | }, 282 | "queryParams": { 283 | "type": "any", 284 | "required": false, 285 | "allowedValues": null, 286 | "disallowedValues": null 287 | }, 288 | "payloadParams": { 289 | "type": "object", 290 | "required": null, 291 | "allowedValues": null, 292 | "disallowedValues": null, 293 | "children": [ 294 | { 295 | "name": "percent", 296 | "type": "number", 297 | "required": null, 298 | "allowedValues": null, 299 | "disallowedValues": null 300 | }, 301 | { 302 | "name": "duration_ms", 303 | "type": "number", 304 | "required": null, 305 | "allowedValues": null, 306 | "disallowedValues": null 307 | } 308 | ] 309 | }, 310 | "responseParams": null, 311 | "version": "2" 312 | }, 313 | { 314 | "path": "/subscriptions", 315 | "method": "GET", 316 | "auth": [ 317 | "bearer" 318 | ], 319 | "pathParams": { 320 | "type": "any", 321 | "required": false, 322 | "allowedValues": null, 323 | "disallowedValues": null 324 | }, 325 | "queryParams": { 326 | "type": "object", 327 | "required": null, 328 | "allowedValues": null, 329 | "disallowedValues": null, 330 | "children": [ 331 | { 332 | "name": "publisher_id", 333 | "type": "string", 334 | "required": null, 335 | "allowedValues": null, 336 | "disallowedValues": null 337 | }, 338 | { 339 | "name": "subscriber_id", 340 | "type": "string", 341 | "required": null, 342 | "allowedValues": null, 343 | "disallowedValues": null 344 | } 345 | ] 346 | }, 347 | "payloadParams": { 348 | "type": "any", 349 | "required": false, 350 | "allowedValues": null, 351 | "disallowedValues": null 352 | }, 353 | "responseParams": null, 354 | "version": "2" 355 | }, 356 | { 357 | "path": "/subscriptions", 358 | "method": "POST", 359 | "auth": [ 360 | "bearer" 361 | ], 362 | "pathParams": { 363 | "type": "any", 364 | "required": false, 365 | "allowedValues": null, 366 | "disallowedValues": null 367 | }, 368 | "queryParams": { 369 | "type": "any", 370 | "required": false, 371 | "allowedValues": null, 372 | "disallowedValues": null 373 | }, 374 | "payloadParams": { 375 | "type": "object", 376 | "required": null, 377 | "allowedValues": null, 378 | "disallowedValues": null, 379 | "children": [ 380 | { 381 | "name": "subscriber_id", 382 | "type": "string", 383 | "required": true, 384 | "allowedValues": null, 385 | "disallowedValues": null 386 | }, 387 | { 388 | "name": "publisher_id", 389 | "type": "string", 390 | "required": true, 391 | "allowedValues": null, 392 | "disallowedValues": null 393 | }, 394 | { 395 | "name": "publisher_events", 396 | "type": "array", 397 | "required": null, 398 | "allowedValues": null, 399 | "disallowedValues": null, 400 | "rules": { 401 | "Includes": [ 402 | { 403 | "type": "any", 404 | "required": false, 405 | "allowedValues": [ 406 | "amplitude", 407 | "amplitude:delta:sustain", 408 | "amplitude:delta:ignite", 409 | "amplitude:delta:release", 410 | "amplitude:delta:nap", 411 | "amplitude:level:active", 412 | "amplitude:level:idle" 413 | ], 414 | "disallowedValues": null 415 | } 416 | ] 417 | } 418 | } 419 | ] 420 | }, 421 | "responseParams": null, 422 | "version": "2" 423 | }, 424 | { 425 | "path": "/subscriptions", 426 | "method": "DELETE", 427 | "auth": [ 428 | "bearer" 429 | ], 430 | "pathParams": { 431 | "type": "any", 432 | "required": false, 433 | "allowedValues": null, 434 | "disallowedValues": null 435 | }, 436 | "queryParams": { 437 | "type": "any", 438 | "required": false, 439 | "allowedValues": null, 440 | "disallowedValues": null 441 | }, 442 | "payloadParams": { 443 | "type": "object", 444 | "required": null, 445 | "allowedValues": null, 446 | "disallowedValues": null, 447 | "children": [ 448 | { 449 | "name": "publisher_id", 450 | "type": "string", 451 | "required": true, 452 | "allowedValues": null, 453 | "disallowedValues": null 454 | }, 455 | { 456 | "name": "subscriber_id", 457 | "type": "string", 458 | "required": true, 459 | "allowedValues": null, 460 | "disallowedValues": null 461 | } 462 | ] 463 | }, 464 | "responseParams": null, 465 | "version": "2" 466 | } 467 | ]; 468 | // Generated from scripts/download-routes.js on Wed Jun 11 2014 00:22:37 GMT-0400 (EDT) 469 | 470 | module.exports = [ 471 | { 472 | "path": "/ping", 473 | "method": "GET", 474 | "auth": false, 475 | "pathParams": { 476 | "type": "any", 477 | "required": false, 478 | "allowedValues": null, 479 | "disallowedValues": null 480 | }, 481 | "queryParams": { 482 | "type": "any", 483 | "required": false, 484 | "allowedValues": null, 485 | "disallowedValues": null 486 | }, 487 | "payloadParams": { 488 | "type": "any", 489 | "required": false, 490 | "allowedValues": null, 491 | "disallowedValues": null 492 | }, 493 | "responseParams": null, 494 | "version": null 495 | }, 496 | { 497 | "path": "/devices", 498 | "method": "GET", 499 | "auth": [ 500 | "bearer" 501 | ], 502 | "pathParams": { 503 | "type": "any", 504 | "required": false, 505 | "allowedValues": null, 506 | "disallowedValues": null 507 | }, 508 | "queryParams": { 509 | "type": "any", 510 | "required": false, 511 | "allowedValues": null, 512 | "disallowedValues": null 513 | }, 514 | "payloadParams": { 515 | "type": "any", 516 | "required": false, 517 | "allowedValues": null, 518 | "disallowedValues": null 519 | }, 520 | "responseParams": null, 521 | "version": "2" 522 | }, 523 | { 524 | "path": "/devices/{device_id}", 525 | "method": "GET", 526 | "auth": [ 527 | "bearer" 528 | ], 529 | "pathParams": { 530 | "type": "any", 531 | "required": false, 532 | "allowedValues": null, 533 | "disallowedValues": null 534 | }, 535 | "queryParams": { 536 | "type": "any", 537 | "required": false, 538 | "allowedValues": null, 539 | "disallowedValues": null 540 | }, 541 | "payloadParams": { 542 | "type": "any", 543 | "required": false, 544 | "allowedValues": null, 545 | "disallowedValues": null 546 | }, 547 | "responseParams": null, 548 | "version": "2" 549 | }, 550 | { 551 | "path": "/devices/{device_id}", 552 | "method": "POST", 553 | "auth": [ 554 | "bearer" 555 | ], 556 | "pathParams": { 557 | "type": "any", 558 | "required": false, 559 | "allowedValues": null, 560 | "disallowedValues": null 561 | }, 562 | "queryParams": { 563 | "type": "any", 564 | "required": false, 565 | "allowedValues": null, 566 | "disallowedValues": null 567 | }, 568 | "payloadParams": { 569 | "type": "object", 570 | "required": null, 571 | "allowedValues": null, 572 | "disallowedValues": null, 573 | "children": [ 574 | { 575 | "name": "device_hash", 576 | "type": "string", 577 | "required": true, 578 | "allowedValues": null, 579 | "disallowedValues": null 580 | }, 581 | { 582 | "name": "update", 583 | "type": "object", 584 | "required": null, 585 | "allowedValues": null, 586 | "disallowedValues": null, 587 | "children": [ 588 | { 589 | "name": "label", 590 | "type": "string", 591 | "required": true, 592 | "allowedValues": null, 593 | "disallowedValues": null 594 | } 595 | ] 596 | } 597 | ] 598 | }, 599 | "responseParams": null, 600 | "version": "2" 601 | }, 602 | { 603 | "path": "/devices/{device_id}", 604 | "method": "PUT", 605 | "auth": [ 606 | "bearer" 607 | ], 608 | "pathParams": { 609 | "type": "any", 610 | "required": false, 611 | "allowedValues": null, 612 | "disallowedValues": null 613 | }, 614 | "queryParams": { 615 | "type": "any", 616 | "required": false, 617 | "allowedValues": null, 618 | "disallowedValues": null 619 | }, 620 | "payloadParams": { 621 | "type": "object", 622 | "required": null, 623 | "allowedValues": null, 624 | "disallowedValues": null, 625 | "children": [ 626 | { 627 | "name": "update", 628 | "type": "object", 629 | "required": true, 630 | "allowedValues": null, 631 | "disallowedValues": null, 632 | "children": [ 633 | { 634 | "name": "label", 635 | "type": "string", 636 | "required": null, 637 | "allowedValues": null, 638 | "disallowedValues": null 639 | } 640 | ] 641 | } 642 | ] 643 | }, 644 | "responseParams": null, 645 | "version": "2" 646 | }, 647 | { 648 | "path": "/devices/{device_id}", 649 | "method": "DELETE", 650 | "auth": [ 651 | "bearer" 652 | ], 653 | "pathParams": { 654 | "type": "any", 655 | "required": false, 656 | "allowedValues": null, 657 | "disallowedValues": null 658 | }, 659 | "queryParams": { 660 | "type": "any", 661 | "required": false, 662 | "allowedValues": null, 663 | "disallowedValues": null 664 | }, 665 | "payloadParams": { 666 | "type": "any", 667 | "required": false, 668 | "allowedValues": null, 669 | "disallowedValues": null 670 | }, 671 | "responseParams": null, 672 | "version": "2" 673 | }, 674 | { 675 | "path": "/devices/{device_id}/light", 676 | "method": "POST", 677 | "auth": [ 678 | "bearer" 679 | ], 680 | "pathParams": { 681 | "type": "any", 682 | "required": false, 683 | "allowedValues": null, 684 | "disallowedValues": null 685 | }, 686 | "queryParams": { 687 | "type": "any", 688 | "required": false, 689 | "allowedValues": null, 690 | "disallowedValues": null 691 | }, 692 | "payloadParams": { 693 | "type": "object", 694 | "required": null, 695 | "allowedValues": null, 696 | "disallowedValues": null, 697 | "children": [ 698 | { 699 | "name": "mode", 700 | "type": "string", 701 | "required": false, 702 | "allowedValues": [ 703 | "blink", 704 | "hold" 705 | ], 706 | "disallowedValues": null 707 | }, 708 | { 709 | "name": "color", 710 | "type": "string", 711 | "required": false, 712 | "allowedValues": [ 713 | "green", 714 | "yellow", 715 | "red", 716 | "blue", 717 | "purple", 718 | "white", 719 | "cyan", 720 | "clownbarf" 721 | ], 722 | "disallowedValues": null 723 | }, 724 | { 725 | "name": "duration_ms", 726 | "type": "number", 727 | "required": null, 728 | "allowedValues": null, 729 | "disallowedValues": null 730 | } 731 | ] 732 | }, 733 | "responseParams": null, 734 | "version": "2" 735 | }, 736 | { 737 | "path": "/devices/{device_id}/output", 738 | "method": "POST", 739 | "auth": [ 740 | "bearer" 741 | ], 742 | "pathParams": { 743 | "type": "any", 744 | "required": false, 745 | "allowedValues": null, 746 | "disallowedValues": null 747 | }, 748 | "queryParams": { 749 | "type": "any", 750 | "required": false, 751 | "allowedValues": null, 752 | "disallowedValues": null 753 | }, 754 | "payloadParams": { 755 | "type": "object", 756 | "required": null, 757 | "allowedValues": null, 758 | "disallowedValues": null, 759 | "children": [ 760 | { 761 | "name": "percent", 762 | "type": "number", 763 | "required": null, 764 | "allowedValues": null, 765 | "disallowedValues": null 766 | }, 767 | { 768 | "name": "duration_ms", 769 | "type": "number", 770 | "required": null, 771 | "allowedValues": null, 772 | "disallowedValues": null 773 | } 774 | ] 775 | }, 776 | "responseParams": null, 777 | "version": "2" 778 | }, 779 | { 780 | "path": "/subscriptions", 781 | "method": "GET", 782 | "auth": [ 783 | "bearer" 784 | ], 785 | "pathParams": { 786 | "type": "any", 787 | "required": false, 788 | "allowedValues": null, 789 | "disallowedValues": null 790 | }, 791 | "queryParams": { 792 | "type": "object", 793 | "required": null, 794 | "allowedValues": null, 795 | "disallowedValues": null, 796 | "children": [ 797 | { 798 | "name": "publisher_id", 799 | "type": "string", 800 | "required": null, 801 | "allowedValues": null, 802 | "disallowedValues": null 803 | }, 804 | { 805 | "name": "subscriber_id", 806 | "type": "string", 807 | "required": null, 808 | "allowedValues": null, 809 | "disallowedValues": null 810 | } 811 | ] 812 | }, 813 | "payloadParams": { 814 | "type": "any", 815 | "required": false, 816 | "allowedValues": null, 817 | "disallowedValues": null 818 | }, 819 | "responseParams": null, 820 | "version": "2" 821 | }, 822 | { 823 | "path": "/subscriptions", 824 | "method": "POST", 825 | "auth": [ 826 | "bearer" 827 | ], 828 | "pathParams": { 829 | "type": "any", 830 | "required": false, 831 | "allowedValues": null, 832 | "disallowedValues": null 833 | }, 834 | "queryParams": { 835 | "type": "any", 836 | "required": false, 837 | "allowedValues": null, 838 | "disallowedValues": null 839 | }, 840 | "payloadParams": { 841 | "type": "object", 842 | "required": null, 843 | "allowedValues": null, 844 | "disallowedValues": null, 845 | "children": [ 846 | { 847 | "name": "subscriber_id", 848 | "type": "string", 849 | "required": true, 850 | "allowedValues": null, 851 | "disallowedValues": null 852 | }, 853 | { 854 | "name": "publisher_id", 855 | "type": "string", 856 | "required": true, 857 | "allowedValues": null, 858 | "disallowedValues": null 859 | }, 860 | { 861 | "name": "publisher_events", 862 | "type": "array", 863 | "required": null, 864 | "allowedValues": null, 865 | "disallowedValues": null, 866 | "rules": { 867 | "Includes": [ 868 | { 869 | "type": "any", 870 | "required": false, 871 | "allowedValues": [ 872 | "amplitude", 873 | "amplitude:delta:sustain", 874 | "amplitude:delta:ignite", 875 | "amplitude:delta:release", 876 | "amplitude:delta:nap", 877 | "amplitude:level:active", 878 | "amplitude:level:idle" 879 | ], 880 | "disallowedValues": null 881 | } 882 | ] 883 | } 884 | } 885 | ] 886 | }, 887 | "responseParams": null, 888 | "version": "2" 889 | }, 890 | { 891 | "path": "/subscriptions", 892 | "method": "DELETE", 893 | "auth": [ 894 | "bearer" 895 | ], 896 | "pathParams": { 897 | "type": "any", 898 | "required": false, 899 | "allowedValues": null, 900 | "disallowedValues": null 901 | }, 902 | "queryParams": { 903 | "type": "any", 904 | "required": false, 905 | "allowedValues": null, 906 | "disallowedValues": null 907 | }, 908 | "payloadParams": { 909 | "type": "object", 910 | "required": null, 911 | "allowedValues": null, 912 | "disallowedValues": null, 913 | "children": [ 914 | { 915 | "name": "publisher_id", 916 | "type": "string", 917 | "required": true, 918 | "allowedValues": null, 919 | "disallowedValues": null 920 | }, 921 | { 922 | "name": "subscriber_id", 923 | "type": "string", 924 | "required": true, 925 | "allowedValues": null, 926 | "disallowedValues": null 927 | } 928 | ] 929 | }, 930 | "responseParams": null, 931 | "version": "2" 932 | } 933 | ]; 934 | // Generated from scripts/download-routes.js on Wed Jun 11 2014 00:22:37 GMT-0400 (EDT) 935 | 936 | module.exports = [ 937 | { 938 | "path": "/ping", 939 | "method": "GET", 940 | "auth": false, 941 | "pathParams": { 942 | "type": "any", 943 | "required": false, 944 | "allowedValues": null, 945 | "disallowedValues": null 946 | }, 947 | "queryParams": { 948 | "type": "any", 949 | "required": false, 950 | "allowedValues": null, 951 | "disallowedValues": null 952 | }, 953 | "payloadParams": { 954 | "type": "any", 955 | "required": false, 956 | "allowedValues": null, 957 | "disallowedValues": null 958 | }, 959 | "responseParams": null, 960 | "version": null 961 | }, 962 | { 963 | "path": "/devices", 964 | "method": "GET", 965 | "auth": [ 966 | "bearer" 967 | ], 968 | "pathParams": { 969 | "type": "any", 970 | "required": false, 971 | "allowedValues": null, 972 | "disallowedValues": null 973 | }, 974 | "queryParams": { 975 | "type": "any", 976 | "required": false, 977 | "allowedValues": null, 978 | "disallowedValues": null 979 | }, 980 | "payloadParams": { 981 | "type": "any", 982 | "required": false, 983 | "allowedValues": null, 984 | "disallowedValues": null 985 | }, 986 | "responseParams": null, 987 | "version": "2" 988 | }, 989 | { 990 | "path": "/devices/{device_id}", 991 | "method": "GET", 992 | "auth": [ 993 | "bearer" 994 | ], 995 | "pathParams": { 996 | "type": "any", 997 | "required": false, 998 | "allowedValues": null, 999 | "disallowedValues": null 1000 | }, 1001 | "queryParams": { 1002 | "type": "any", 1003 | "required": false, 1004 | "allowedValues": null, 1005 | "disallowedValues": null 1006 | }, 1007 | "payloadParams": { 1008 | "type": "any", 1009 | "required": false, 1010 | "allowedValues": null, 1011 | "disallowedValues": null 1012 | }, 1013 | "responseParams": null, 1014 | "version": "2" 1015 | }, 1016 | { 1017 | "path": "/devices/{device_id}", 1018 | "method": "POST", 1019 | "auth": [ 1020 | "bearer" 1021 | ], 1022 | "pathParams": { 1023 | "type": "any", 1024 | "required": false, 1025 | "allowedValues": null, 1026 | "disallowedValues": null 1027 | }, 1028 | "queryParams": { 1029 | "type": "any", 1030 | "required": false, 1031 | "allowedValues": null, 1032 | "disallowedValues": null 1033 | }, 1034 | "payloadParams": { 1035 | "type": "object", 1036 | "required": null, 1037 | "allowedValues": null, 1038 | "disallowedValues": null, 1039 | "children": [ 1040 | { 1041 | "name": "device_hash", 1042 | "type": "string", 1043 | "required": true, 1044 | "allowedValues": null, 1045 | "disallowedValues": null 1046 | }, 1047 | { 1048 | "name": "update", 1049 | "type": "object", 1050 | "required": null, 1051 | "allowedValues": null, 1052 | "disallowedValues": null, 1053 | "children": [ 1054 | { 1055 | "name": "label", 1056 | "type": "string", 1057 | "required": true, 1058 | "allowedValues": null, 1059 | "disallowedValues": null 1060 | } 1061 | ] 1062 | } 1063 | ] 1064 | }, 1065 | "responseParams": null, 1066 | "version": "2" 1067 | }, 1068 | { 1069 | "path": "/devices/{device_id}", 1070 | "method": "PUT", 1071 | "auth": [ 1072 | "bearer" 1073 | ], 1074 | "pathParams": { 1075 | "type": "any", 1076 | "required": false, 1077 | "allowedValues": null, 1078 | "disallowedValues": null 1079 | }, 1080 | "queryParams": { 1081 | "type": "any", 1082 | "required": false, 1083 | "allowedValues": null, 1084 | "disallowedValues": null 1085 | }, 1086 | "payloadParams": { 1087 | "type": "object", 1088 | "required": null, 1089 | "allowedValues": null, 1090 | "disallowedValues": null, 1091 | "children": [ 1092 | { 1093 | "name": "update", 1094 | "type": "object", 1095 | "required": true, 1096 | "allowedValues": null, 1097 | "disallowedValues": null, 1098 | "children": [ 1099 | { 1100 | "name": "label", 1101 | "type": "string", 1102 | "required": null, 1103 | "allowedValues": null, 1104 | "disallowedValues": null 1105 | } 1106 | ] 1107 | } 1108 | ] 1109 | }, 1110 | "responseParams": null, 1111 | "version": "2" 1112 | }, 1113 | { 1114 | "path": "/devices/{device_id}", 1115 | "method": "DELETE", 1116 | "auth": [ 1117 | "bearer" 1118 | ], 1119 | "pathParams": { 1120 | "type": "any", 1121 | "required": false, 1122 | "allowedValues": null, 1123 | "disallowedValues": null 1124 | }, 1125 | "queryParams": { 1126 | "type": "any", 1127 | "required": false, 1128 | "allowedValues": null, 1129 | "disallowedValues": null 1130 | }, 1131 | "payloadParams": { 1132 | "type": "any", 1133 | "required": false, 1134 | "allowedValues": null, 1135 | "disallowedValues": null 1136 | }, 1137 | "responseParams": null, 1138 | "version": "2" 1139 | }, 1140 | { 1141 | "path": "/devices/{device_id}/light", 1142 | "method": "POST", 1143 | "auth": [ 1144 | "bearer" 1145 | ], 1146 | "pathParams": { 1147 | "type": "any", 1148 | "required": false, 1149 | "allowedValues": null, 1150 | "disallowedValues": null 1151 | }, 1152 | "queryParams": { 1153 | "type": "any", 1154 | "required": false, 1155 | "allowedValues": null, 1156 | "disallowedValues": null 1157 | }, 1158 | "payloadParams": { 1159 | "type": "object", 1160 | "required": null, 1161 | "allowedValues": null, 1162 | "disallowedValues": null, 1163 | "children": [ 1164 | { 1165 | "name": "mode", 1166 | "type": "string", 1167 | "required": false, 1168 | "allowedValues": [ 1169 | "blink", 1170 | "hold" 1171 | ], 1172 | "disallowedValues": null 1173 | }, 1174 | { 1175 | "name": "color", 1176 | "type": "string", 1177 | "required": false, 1178 | "allowedValues": [ 1179 | "green", 1180 | "yellow", 1181 | "red", 1182 | "blue", 1183 | "purple", 1184 | "white", 1185 | "cyan", 1186 | "clownbarf" 1187 | ], 1188 | "disallowedValues": null 1189 | }, 1190 | { 1191 | "name": "duration_ms", 1192 | "type": "number", 1193 | "required": null, 1194 | "allowedValues": null, 1195 | "disallowedValues": null 1196 | } 1197 | ] 1198 | }, 1199 | "responseParams": null, 1200 | "version": "2" 1201 | }, 1202 | { 1203 | "path": "/devices/{device_id}/output", 1204 | "method": "POST", 1205 | "auth": [ 1206 | "bearer" 1207 | ], 1208 | "pathParams": { 1209 | "type": "any", 1210 | "required": false, 1211 | "allowedValues": null, 1212 | "disallowedValues": null 1213 | }, 1214 | "queryParams": { 1215 | "type": "any", 1216 | "required": false, 1217 | "allowedValues": null, 1218 | "disallowedValues": null 1219 | }, 1220 | "payloadParams": { 1221 | "type": "object", 1222 | "required": null, 1223 | "allowedValues": null, 1224 | "disallowedValues": null, 1225 | "children": [ 1226 | { 1227 | "name": "percent", 1228 | "type": "number", 1229 | "required": null, 1230 | "allowedValues": null, 1231 | "disallowedValues": null 1232 | }, 1233 | { 1234 | "name": "duration_ms", 1235 | "type": "number", 1236 | "required": null, 1237 | "allowedValues": null, 1238 | "disallowedValues": null 1239 | } 1240 | ] 1241 | }, 1242 | "responseParams": null, 1243 | "version": "2" 1244 | }, 1245 | { 1246 | "path": "/subscriptions", 1247 | "method": "GET", 1248 | "auth": [ 1249 | "bearer" 1250 | ], 1251 | "pathParams": { 1252 | "type": "any", 1253 | "required": false, 1254 | "allowedValues": null, 1255 | "disallowedValues": null 1256 | }, 1257 | "queryParams": { 1258 | "type": "object", 1259 | "required": null, 1260 | "allowedValues": null, 1261 | "disallowedValues": null, 1262 | "children": [ 1263 | { 1264 | "name": "publisher_id", 1265 | "type": "string", 1266 | "required": null, 1267 | "allowedValues": null, 1268 | "disallowedValues": null 1269 | }, 1270 | { 1271 | "name": "subscriber_id", 1272 | "type": "string", 1273 | "required": null, 1274 | "allowedValues": null, 1275 | "disallowedValues": null 1276 | } 1277 | ] 1278 | }, 1279 | "payloadParams": { 1280 | "type": "any", 1281 | "required": false, 1282 | "allowedValues": null, 1283 | "disallowedValues": null 1284 | }, 1285 | "responseParams": null, 1286 | "version": "2" 1287 | }, 1288 | { 1289 | "path": "/subscriptions", 1290 | "method": "POST", 1291 | "auth": [ 1292 | "bearer" 1293 | ], 1294 | "pathParams": { 1295 | "type": "any", 1296 | "required": false, 1297 | "allowedValues": null, 1298 | "disallowedValues": null 1299 | }, 1300 | "queryParams": { 1301 | "type": "any", 1302 | "required": false, 1303 | "allowedValues": null, 1304 | "disallowedValues": null 1305 | }, 1306 | "payloadParams": { 1307 | "type": "object", 1308 | "required": null, 1309 | "allowedValues": null, 1310 | "disallowedValues": null, 1311 | "children": [ 1312 | { 1313 | "name": "subscriber_id", 1314 | "type": "string", 1315 | "required": true, 1316 | "allowedValues": null, 1317 | "disallowedValues": null 1318 | }, 1319 | { 1320 | "name": "publisher_id", 1321 | "type": "string", 1322 | "required": true, 1323 | "allowedValues": null, 1324 | "disallowedValues": null 1325 | }, 1326 | { 1327 | "name": "publisher_events", 1328 | "type": "array", 1329 | "required": null, 1330 | "allowedValues": null, 1331 | "disallowedValues": null, 1332 | "rules": { 1333 | "Includes": [ 1334 | { 1335 | "type": "any", 1336 | "required": false, 1337 | "allowedValues": [ 1338 | "amplitude", 1339 | "amplitude:delta:sustain", 1340 | "amplitude:delta:ignite", 1341 | "amplitude:delta:release", 1342 | "amplitude:delta:nap", 1343 | "amplitude:level:active", 1344 | "amplitude:level:idle" 1345 | ], 1346 | "disallowedValues": null 1347 | } 1348 | ] 1349 | } 1350 | } 1351 | ] 1352 | }, 1353 | "responseParams": null, 1354 | "version": "2" 1355 | }, 1356 | { 1357 | "path": "/subscriptions", 1358 | "method": "DELETE", 1359 | "auth": [ 1360 | "bearer" 1361 | ], 1362 | "pathParams": { 1363 | "type": "any", 1364 | "required": false, 1365 | "allowedValues": null, 1366 | "disallowedValues": null 1367 | }, 1368 | "queryParams": { 1369 | "type": "any", 1370 | "required": false, 1371 | "allowedValues": null, 1372 | "disallowedValues": null 1373 | }, 1374 | "payloadParams": { 1375 | "type": "object", 1376 | "required": null, 1377 | "allowedValues": null, 1378 | "disallowedValues": null, 1379 | "children": [ 1380 | { 1381 | "name": "publisher_id", 1382 | "type": "string", 1383 | "required": true, 1384 | "allowedValues": null, 1385 | "disallowedValues": null 1386 | }, 1387 | { 1388 | "name": "subscriber_id", 1389 | "type": "string", 1390 | "required": true, 1391 | "allowedValues": null, 1392 | "disallowedValues": null 1393 | } 1394 | ] 1395 | }, 1396 | "responseParams": null, 1397 | "version": "2" 1398 | } 1399 | ] 1400 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 2 | function isExist (value) { 3 | return typeof value !== 'undefined' && value !== null 4 | } 5 | 6 | 7 | 8 | module.exports = { 9 | isExist: isExist 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "license": "GPL-3.0", 3 | "scripts": { 4 | "test": "testem ci", 5 | "start": "mocha --compilers 'coffee:coffee-script/register' --watch", 6 | "test-debug": "DEBUG=littlebits* npm start", 7 | "dev": "npm start", 8 | "devall": "testem", 9 | "dist": "browserify dist/index.js -o dist/littleBitsAPI.js && uglifyjs dist/littleBitsAPI.js > dist/littleBitsAPI.min.js", 10 | "download-routes": "node scripts/download-routes.js > lib/routes.js", 11 | "postversion": "git push && git push --tags && npm publish" 12 | }, 13 | "dependencies": { 14 | "lodash.transform": "^3.0.0", 15 | "merge": "^1.2.0", 16 | "superagent": "^1.4.0", 17 | "to-snake-case": "^0.1.2" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "git://github.com/littlebits/cloud-client-api-http.git" 22 | }, 23 | "author": "Jason Kuhrt (http://jasonkuhrt.com/)", 24 | "name": "@littlebits/cloud-http", 25 | "homepage": "https://github.com/littlebits/cloud-client-api-http", 26 | "version": "0.3.5", 27 | "main": "lib/index.js", 28 | "description": "Lightweight wrapper for littleBits Cloud HTTP API", 29 | "devDependencies": { 30 | "browserify": "~3.44.1", 31 | "chai": "^3.4.1", 32 | "coffee-script": "^1.7.1", 33 | "es5-shim": "~3.0.2", 34 | "mocha": "^2.1.0", 35 | "nock": "^2.17.0", 36 | "plat": "^0.1.0", 37 | "request": "~2.34.0", 38 | "tap": "^2.2.0" 39 | }, 40 | "bugs": { 41 | "url": "https://github.com/littlebits/cloud-client-api-http/issues" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /scripts/download-routes.js: -------------------------------------------------------------------------------- 1 | var r = require('request'); 2 | 3 | 4 | var uri = 'https://api-http.littlebitscloud.cc/options'; 5 | // var uri = 'http://localhost:8000/options'; 6 | 7 | r.get(uri, {json: true}, handle_response); 8 | 9 | 10 | 11 | function handle_response(err, res, body){ 12 | if (err && err.code === 'ECONNREFUSED') { 13 | console.log('Supposed server at %s is unreachable', uri); 14 | throw err; 15 | } 16 | process.stdout.write(prepare_source(body)); 17 | } 18 | 19 | function prepare_source(source){ 20 | return '\n// Generated from scripts/download-routes.js on ' + Date() + 21 | '\n\nmodule.exports = ' + JSON.stringify(source, null, 2) + ';'; 22 | } 23 | -------------------------------------------------------------------------------- /test/_globals.coffee: -------------------------------------------------------------------------------- 1 | {GLOBE, isClient} = require('plat') 2 | 3 | GLOBE.isClient = isClient 4 | GLOBE.a = require('chai').assert; 5 | GLOBE.API = if isClient then require('littlebits-cloud-http') else require('../') 6 | GLOBE.api = API; 7 | 8 | 9 | 10 | # Until there is a good mocking library in browser 11 | # skip mock testing in that environment. 12 | return if isClient 13 | 14 | 15 | 16 | GLOBE.nock = require('nock') 17 | GLOBE.fix = {} 18 | 19 | fix.request = -> 20 | nock('https://api-http.littlebitscloud.cc') 21 | 22 | a.successful_response = (server, done)-> 23 | (err, result)-> 24 | a !err, err?.message 25 | server.done() 26 | done() 27 | 28 | # TODO: need to make this make sense 29 | a.unauthorized_response = (server, done)-> 30 | (err, result)-> 31 | a err 32 | a err.status, 401 33 | server.done() 34 | done() 35 | -------------------------------------------------------------------------------- /test/api-activate.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | device_hash = '34b5eada90b51d4713a21db848b18ae4' 6 | 7 | 8 | 9 | describe '.activate', -> 10 | 11 | it 'can send a POST with only hash payload', (done)-> 12 | server = fix.request().post('/devices/a1', { device_hash }).reply(201, {}) 13 | api.activate 'a1', { device_hash }, a.successful_response server, done 14 | 15 | it 'cannot send a POST without a hash', (done)-> 16 | server = fix.request().post('/devices/a1').reply(401, {}) 17 | api.activate 'a1', a.unauthorized_response server, done 18 | 19 | it 'can send a POST with PUT-style payload optionally', (done)-> 20 | server = fix.request().post('/devices/a1', { device_hash, update: { label: 'foobar' } }).reply(201, {}) 21 | api.activate 'a1', { device_hash, update: { label: 'foobar' } }, a.successful_response server, done 22 | 23 | it 'works using conf-style', (done)-> 24 | server = fix.request().post('/devices/a1').reply(201, {}) 25 | api.activate { device_id: 'a1', device_hash }, a.successful_response server, done 26 | -------------------------------------------------------------------------------- /test/api-deactivate.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.deactivate', -> 8 | 9 | beforeEach -> 10 | @server = fix.request().delete('/devices/a1').reply(200, {}) 11 | 12 | it 'sends a DELETE with no payload', (done)-> 13 | api.deactivate 'a1', a.successful_response @server, done 14 | 15 | it 'works using conf-style', (done)-> 16 | api.deactivate { device_id: 'a1' }, a.successful_response @server, done 17 | -------------------------------------------------------------------------------- /test/api-defaults.coffee: -------------------------------------------------------------------------------- 1 | merge = require 'merge' 2 | 3 | 4 | 5 | describe '.defaults', -> 6 | defs1 = defs2 = undefined 7 | 8 | beforeEach -> 9 | defs1 = 10 | host: 'api-rest-stage.littlebitscloud.cc' 11 | access_token: 'dev' 12 | version: '1' 13 | 14 | defs2 = { version: '2' } 15 | 16 | 17 | 18 | describe 'on parent API object (.defaults)', -> 19 | 20 | it 'stores default options', -> 21 | test_defaults_store API, defs1, {} 22 | 23 | 24 | it 's a new api object, it does not mutate api', -> 25 | test_defaults_immutable API, defs1, defs2 26 | 27 | 28 | it 'is infinitely recursive', -> 29 | @slow 1000 # Tell mocha its ok for this test to be slow 30 | test_defaults_recursive API, defs1, defs2 31 | 32 | 33 | 34 | describe 'on api function (..defaults)', -> 35 | api = mdefs1 = undefined 36 | 37 | beforeEach -> 38 | api = API.defaults(defs1) 39 | mdefs1 = { ms: 3000 } 40 | 41 | it 'stores default options', -> 42 | test_defaults_store api.output, mdefs1, defs1 43 | 44 | it 's a new function, it does not mutate function', -> 45 | test_defaults_immutable api.output, defs1, defs2 46 | 47 | it 'is infinitely recursive', -> 48 | @slow 1000 # Tell mocha its ok for this test to be slow 49 | test_defaults_recursive api.output, defs1, defs2 50 | 51 | 52 | 53 | 54 | 55 | 56 | test_defaults_store = (o, adefs, bdefs) -> 57 | o2 = o.defaults(adefs) 58 | a.deepEqual o2.defaults(), merge({}, bdefs, adefs), 'defaults have been stored' 59 | 60 | test_defaults_immutable = (o, adefs, bdefs) -> 61 | o2 = o.defaults(adefs) 62 | o3 = o2.defaults(bdefs) 63 | a.deepEqual o3.defaults(), merge({}, adefs, bdefs), 'new defaults have been stored' 64 | a.deepEqual o2.defaults(), adefs, 'new api was created, old api not mutated' 65 | 66 | test_defaults_recursive = (o, adefs, bdefs) -> 67 | o2 = o.defaults(adefs) 68 | o3 = o2.defaults(bdefs) 69 | i = 0 70 | o3 = o3.defaults(bdefs) while i++ <= 10000 71 | a.deepEqual o3.defaults(), merge({}, adefs, bdefs), 'new defaults have been stored' 72 | a.deepEqual o2.defaults(), adefs, 'new api was created, old api not mutated' 73 | -------------------------------------------------------------------------------- /test/api-device.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.device', -> 8 | 9 | beforeEach -> 10 | @server = fix.request().get('/devices/a1').reply(200, {}) 11 | 12 | it 'sends a GET /devices/{device_id} to server', (done)-> 13 | api.device 'a1', a.successful_response @server, done 14 | -------------------------------------------------------------------------------- /test/api-device_update.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.device_update', -> 8 | payload = { update: { label: 'foobar' } } 9 | 10 | beforeEach -> 11 | @server = fix.request().put('/devices/a1', payload).reply(200, {}) 12 | 13 | it 'sends a PUT /devices/{device_id} to server', (done)-> 14 | api.device_update 'a1', payload, a.successful_response @server, done 15 | -------------------------------------------------------------------------------- /test/api-devices.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | devices = API.devices 6 | 7 | 8 | 9 | describe '.devices', -> 10 | 11 | beforeEach -> 12 | @server = fix.request().get('/devices').reply(200, []) 13 | 14 | it 'sends a GET /devices to server', (done)-> 15 | api.devices a.successful_response @server, done 16 | -------------------------------------------------------------------------------- /test/api-light.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.light', -> 8 | 9 | it 'sends a POST with no payload to server by default', (done)-> 10 | server = fix.request().post('/devices/a1/light').reply(200, {}) 11 | 12 | api.light 'a1', a.successful_response server, done 13 | 14 | 15 | 16 | it 'accepts color, duration_ms, mode', (done)-> 17 | conf = 18 | duration_ms: 50 19 | mode: 'blink' 20 | color: 'purple' 21 | 22 | server = fix.request().post('/devices/a1/light').reply(200, {}) 23 | 24 | api.light 'a1', conf, a.successful_response server, done 25 | -------------------------------------------------------------------------------- /test/api-output.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.output', -> 8 | 9 | it 'sends a POST with no payload to server by default', (done)-> 10 | server = fix.request().post('/devices/a1/output').reply(200, {}) 11 | 12 | api.output 'a1', a.successful_response server, done 13 | 14 | 15 | 16 | it 'accepts percent and duration_ms', (done)-> 17 | conf = 18 | duration_ms: 50 19 | percent: 99 20 | 21 | server = fix.request().post('/devices/a1/output', conf).reply(200, {}) 22 | api.output 'a1', conf, a.successful_response server, done 23 | 24 | 25 | it 'does not treat percent 0 as having not been given', (done)-> 26 | conf = 27 | duration_ms: 50 28 | percent: 0 29 | 30 | server = fix.request().post('/devices/a1/output', conf).reply(200, {}) 31 | api.output 'a1', conf, a.successful_response server, done 32 | -------------------------------------------------------------------------------- /test/api-subscribe.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.subscribe', -> 8 | 9 | it 'sends a POST /subscriptions {...} to the server', (done)-> 10 | conf = 11 | subscriber_id: 'a1' 12 | publisher_id: 'b1' 13 | publisher_events: ['amplitude'] 14 | 15 | server = fix.request() 16 | .post('/subscriptions', conf) 17 | .reply(200, {}) 18 | 19 | api.subscribe conf, a.successful_response server, done 20 | -------------------------------------------------------------------------------- /test/api-subscriptions.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.subscriptions', -> 8 | 9 | it 'sends a GET /subscriptions?... to the server', (done)-> 10 | server = fix.request() 11 | .get('/subscriptions?publisher_id=b1&subscriber_id=a1') 12 | .reply(200, {}) 13 | 14 | api.subscriptions { subscriber_id: 'a1', publisher_id: 'b1' }, a.successful_response server, done 15 | -------------------------------------------------------------------------------- /test/api-unsubscribe.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe '.unsubscribe', -> 8 | 9 | it 'sends a DELETE /subscriptions {...} to the server', (done)-> 10 | conf = 11 | subscriber_id: 'a1' 12 | publisher_id: 'b1' 13 | 14 | server = fix.request().delete('/subscriptions', conf).reply(200, {}) 15 | 16 | api.unsubscribe conf, a.successful_response server, done 17 | -------------------------------------------------------------------------------- /test/http-errors-to-runtime-errors.coffee: -------------------------------------------------------------------------------- 1 | # Until there is a good mocking library in browser 2 | # skip testing in that environment. 3 | return if isClient 4 | 5 | 6 | 7 | describe 'response errors are processed into client runtime errors', -> 8 | 9 | it 'including 401', (done)-> 10 | exp_err = 11 | statusCode: 401 12 | message: 'Token failed authorization' 13 | error: 'Unauthorized' 14 | 15 | convert_given_error 401, exp_err, done 16 | 17 | 18 | 19 | it 'including 500', (done)-> 20 | exp_err = 21 | statusCode: 500 22 | message: 'Nuclear meltdown, run away.' 23 | error: 'Internal Server Error' 24 | 25 | convert_given_error 500, exp_err, done 26 | 27 | 28 | 29 | it 'including error without response body', (done)-> 30 | convert_given_error 468, null, done 31 | 32 | 33 | 34 | it 'including error without object with message', (done)-> 35 | convert_given_error 468, { random: 'thing' }, done 36 | 37 | 38 | 39 | 40 | 41 | 42 | convert_given_error = (status_code, exp_err, done)-> 43 | server = fix.request().get('/devices').reply(status_code, exp_err) 44 | 45 | api.devices (err)-> 46 | if exp_err?.message 47 | # A custom server-supplied error message 48 | a.equal err?.message, exp_err.message 49 | else 50 | # A default superagent error message 51 | a err?.message?.match(/cannot /) 52 | done() -------------------------------------------------------------------------------- /test/method-process-signature.coffee: -------------------------------------------------------------------------------- 1 | noop = -> 2 | 3 | process_signature = API._process_signature 4 | 5 | describe 'process_signature', -> 6 | it 'foo()', -> 7 | r = process_signature({}) 8 | a.deepEqual {}, r.overrides 9 | a.typeOf r.cb, 'function' 10 | 11 | 12 | it 'foo(->)', -> 13 | r = process_signature(0: noop) 14 | a.deepEqual {}, r.overrides 15 | a r.cb is noop 16 | 17 | 18 | it 'foo()', -> 19 | r = process_signature(0: 'foo') 20 | a.deepEqual 21 | device_id: 'foo' 22 | , r.overrides 23 | a.typeOf r.cb, 'function' 24 | 25 | 26 | it 'foo(, {...})', -> 27 | r = process_signature({ 28 | 0: 'foo' 29 | 1: { host: 'foobar' } 30 | }) 31 | a.deepEqual 32 | device_id: 'foo' 33 | host: 'foobar' 34 | , r.overrides 35 | a.typeOf r.cb, 'function' 36 | 37 | 38 | it 'foo(, {...}, ->)', -> 39 | r = process_signature({ 40 | 0: 'foo' 41 | 1: { host: 'foobar' } 42 | 2: noop 43 | }) 44 | a.deepEqual 45 | device_id: 'foo' 46 | host: 'foobar' 47 | , r.overrides 48 | a r.cb is noop 49 | 50 | 51 | it 'foo({...})', -> 52 | r = process_signature(0: host: 'foobar') 53 | a.deepEqual 54 | host: 'foobar' 55 | , r.overrides 56 | a.typeOf r.cb, 'function' 57 | 58 | 59 | it 'foo({...}, ->)', -> 60 | r = process_signature({ 61 | 0: { host: 'foobar' } 62 | 1: noop 63 | }) 64 | a.deepEqual 65 | host: 'foobar' 66 | , r.overrides 67 | a r.cb is noop 68 | 69 | it 'converts camelCase into snake_case', -> 70 | r = process_signature({ 71 | 0: { 72 | accessToken: 'footok', 73 | durationMs: 5000, 74 | subscriberId: 'fooid', 75 | } 76 | }) 77 | 78 | a.deepEqual { 79 | access_token: 'footok', 80 | duration_ms: 5000, 81 | subscriber_id: 'fooid' 82 | } 83 | , r.overrides -------------------------------------------------------------------------------- /testem.yml: -------------------------------------------------------------------------------- 1 | framework: mocha 2 | serve_files: 3 | - build/**/*.js 4 | src_files: 5 | - test/*.coffee 6 | - lib/**/*.js 7 | launchers: 8 | Node: 9 | command: node_modules/.bin/mocha --compilers 'coffee:coffee-script/register' --reporter tap 10 | protocol: tap 11 | before_tests: node_modules/.bin/coffee -o build/test -c test 12 | on_exit: rm -rf build 13 | launch_in_ci: 14 | - node 15 | - phantomjs 16 | launch_in_dev: 17 | - node 18 | - phantomjs 19 | --------------------------------------------------------------------------------