PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/backbone-github.coffee

https://github.com/foresthz/backbone-github
CoffeeScript | 206 lines | 78 code | 15 blank | 113 comment | 9 complexity | a7c118f8af2ed0cec5db3b7de01a4cdf MD5 | raw file
  1. # Backbone GitHub is a library that connects to the [GitHub
  2. # API](http://developer.github.com) using Cross Origin Resource
  3. # Sharing (CORS). That means that you can utilize this library
  4. # to access the full GitHub API without any server-side code
  5. # whatsoever.
  6. #
  7. # ## Dependencies
  8. #
  9. # Backbone GitHub depends on Backbone, so before you can use
  10. # this library you will need to have included Backbone,
  11. # Underscore, and jQuery on the page.
  12. window.GitHub = {}
  13. # ## GitHub.token
  14. #
  15. # If `GitHub.token` is set you will be able to make
  16. # user-authenticated calls to the GitHub API.
  17. GitHub.token = null
  18. # ## GitHub.authenticate(username, password, options)
  19. #
  20. # Authenticate with GitHub via username and password. This
  21. # method will automatically set `GitHub.token`, which allows
  22. # you to make authenticated calls to the GitHub API.
  23. #
  24. # Note that this will authenticate the user with a generic
  25. # "GitHub API" application. In order to have an app-specific
  26. # token you need to use the redirect flow. Available options:
  27. #
  28. # * **success:** a callback function that takes arguments
  29. # `data`, `textStatus`, and `jqXHR` (standard jQuery
  30. # success callback)
  31. # * **error:** a callback function that takes arguments
  32. # `jqXHR`, `textStatus`, and `errorThrown`
  33. # * **scope:** provide comma-separated scopes. For example,
  34. # `repo,user`
  35. GitHub.authenticate = (username, password, options)->
  36. postData = {}
  37. postData.scope = options.scope if options.scope?
  38. $.ajax
  39. url: "https://api.github.com/authorizations"
  40. contentType: 'application/json'
  41. dataType: 'json'
  42. type: 'POST'
  43. data: JSON.stringify(postData)
  44. headers:
  45. 'Authorization': "Basic #{btoa("#{username}:#{password}")}"
  46. success: (d,s,x)->
  47. GitHub.token = d.token
  48. options.success(d,s,x) if options.success?
  49. error: options.error
  50. # ## GitHub.sync
  51. #
  52. # The GitHub sync method is simply a customized version of the
  53. # default Backbone sync mechanism with two improved properties:
  54. #
  55. # 1. It automatically sets an Accept header compatible with the
  56. # GitHub v3 API.
  57. # 2. It sets the OAuth 2.0 Authorization header if `GitHub.token`
  58. # exists.
  59. GitHub.sync = (method, model, options)->
  60. extendedOptions = _.extend {
  61. beforeSend: (xhr)->
  62. xhr.setRequestHeader 'Accept', 'application/vnd.github+json'
  63. xhr.setRequestHeader 'Authorization', "bearer #{GitHub.token}" if GitHub.token
  64. }, options
  65. Backbone.sync(method, model, extendedOptions)
  66. GitHub.Model = Backbone.Model.extend
  67. sync: GitHub.sync
  68. GitHub.Collection = Backbone.Collection.extend
  69. sync: GitHub.sync
  70. GitHub.Relations =
  71. ownedRepos: (options)->
  72. repos = new GitHub.Repos
  73. repos.url = if typeof @url == "function" then @url() else @url
  74. repos.url += "/repos"
  75. repos.fetch(options)
  76. repos
  77. ownedOrgs: (options)->
  78. orgs = new GitHub.Organizations
  79. orgs.url = if typeof @url == "function" then @url() else @url
  80. orgs.url += "/orgs"
  81. orgs.fetch(options)
  82. orgs
  83. # ## GitHub.User
  84. #
  85. # The GitHub User model. For information about specific attributes
  86. # that are available on this model, see GitHub's [Users API](http://developer.github.com/v3/users/)
  87. # documentation.
  88. GitHub.User = GitHub.Model.extend(
  89. urlRoot: 'https://api.github.com/users/'
  90. # ### user.repos(options)
  91. #
  92. # Fetch associated repositories for this user. Takes
  93. # a `success` and `error` callback as potential options.
  94. # Returns a `GitHub.Repos` collection.
  95. repos: GitHub.Relations.ownedRepos
  96. # ### user.organizations(options)
  97. #
  98. # Fetch associated organizations for this user. Takes
  99. # a `success` and `error` callback as potential options.
  100. organizations: GitHub.Relations.ownedOrgs
  101. ,
  102. # ### GitHub.User.fetch(name, options)
  103. #
  104. # Retrieve a user by username. Takes `success` and
  105. # `error` callbacks.
  106. #
  107. # GitHub.User.fetch('mbleigh', {success: function(u){
  108. # console.log(u.toJSON());
  109. # }});
  110. fetch: (name, options)->
  111. user = new GitHub.User(id: name)
  112. user.fetch(options)
  113. user
  114. )
  115. # ## GitHub.Organization
  116. #
  117. # Represents a GitHub organization. See the [Organization
  118. # API](http://developer.github.com/v3/orgs/) docs on GitHub
  119. # for additional information.
  120. GitHub.Organization = GitHub.Model.extend(
  121. urlRoot: 'https://api.github.com/orgs/'
  122. # ### org.repos(options)
  123. #
  124. # Fetch the repositories for the instantiated organization.
  125. # Takes `success` and `error` callbacks. Returns a
  126. # `GitHub.Repos` collection.
  127. repos: GitHub.Relations.ownedRepos
  128. ,
  129. # ### GitHub.Organization.fetch(name, options)
  130. #
  131. # Fetch an organization by name. Accepts `success`
  132. # and `error` callbacks.
  133. #
  134. # GitHub.Organization.fetch('opperator', {success: function(o){
  135. # console.log(o.toJSON());
  136. # }});
  137. fetch: (name, options)->
  138. org = new GitHub.Organization(id: name)
  139. org.fetch(options)
  140. org
  141. )
  142. # ## GitHub.Organizations
  143. #
  144. # Collection of multiple organizations. By default will
  145. # be associated to the current user's organizations (you
  146. # must set an `GitHub.token` for that to work.)
  147. GitHub.Organizations = GitHub.Collection.extend
  148. url: 'https://api.github.com/user/orgs'
  149. model: GitHub.Organization
  150. # ## GitHub.Repo
  151. #
  152. # Repository model. For more information about attributes
  153. # etc, see the [GitHub Repo](http://developer.github.com/v3/repos/)
  154. # API docs.
  155. GitHub.Repo = GitHub.Model.extend(
  156. url: ()->
  157. @get('url') || "https://api.github.com/repos/#{@get('path')}"
  158. ,
  159. # ### GitHub.Repo.fetch(owner, name, options)
  160. #
  161. # Retrieve a repository knowing its owner and name. Takes
  162. # `success` and `error` callbacks in options.
  163. #
  164. # GitHub.Repo.fetch('opperator', 'backbone-github', {success: function(r){
  165. # console.log(r.toJSON());
  166. # }});
  167. fetch: (owner, name, options)->
  168. repo = new GitHub.Repo(path: "#{owner}/#{name}")
  169. repo.fetch(options)
  170. repo
  171. )
  172. # ## GitHub.Repos
  173. #
  174. # Collection of Repo models. Defaults to the current user's
  175. # repositories. Must have set `GitHub.token` for current
  176. # user repo fetch to be successful.
  177. GitHub.Repos = GitHub.Collection.extend
  178. url: 'https://api.github.com/user/repos'
  179. model: GitHub.Repo
  180. # ## GitHub.currentUser
  181. #
  182. # A `GitHub.User` corresponding to the authenticated user.
  183. # Note that you must set `GitHub.token` to a valid OAuth 2.0
  184. # token to be able to utilize the current user.
  185. #
  186. # The `currentUser` is not fetched by default, you must run
  187. # `GitHub.currentUser.fetch()` before it will be populated
  188. # with data.
  189. GitHub.currentUser = new GitHub.User()
  190. GitHub.currentUser.url = "https://api.github.com/user"
  191. GitHub.currentUser.urlRoot = null