backbone-github /src/backbone-github.coffee

Language CoffeeScript Lines 206
MD5 Hash a7c118f8af2ed0cec5db3b7de01a4cdf
Repository https://github.com/foresthz/backbone-github.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Backbone GitHub is a library that connects to the [GitHub
# API](http://developer.github.com) using Cross Origin Resource 
# Sharing (CORS). That means that you can utilize this library 
# to access the full GitHub API without any server-side code 
# whatsoever.
#
# ## Dependencies
# 
# Backbone GitHub depends on Backbone, so before you can use
# this library you will need to have included Backbone, 
# Underscore, and jQuery on the page.
window.GitHub = {}

# ## GitHub.token
#
# If `GitHub.token` is set you will be able to make 
# user-authenticated calls to the GitHub API.
GitHub.token = null

# ## GitHub.authenticate(username, password, options)
#
# Authenticate with GitHub via username and password. This 
# method will automatically set `GitHub.token`, which allows
# you to make authenticated calls to the GitHub API.
#
# Note that this will authenticate the user with a generic
# "GitHub API" application. In order to have an app-specific
# token you need to use the redirect flow. Available options:
#
# * **success:** a callback function that takes arguments
#   `data`, `textStatus`, and `jqXHR` (standard jQuery
#   success callback)
# * **error:** a callback function that takes arguments
#   `jqXHR`, `textStatus`, and `errorThrown`
# * **scope:** provide comma-separated scopes. For example,
#   `repo,user`
GitHub.authenticate = (username, password, options)->
  postData = {}
  postData.scope = options.scope if options.scope?

  $.ajax
    url: "https://api.github.com/authorizations"
    contentType: 'application/json'
    dataType: 'json'
    type: 'POST'
    data: JSON.stringify(postData)
    headers:
      'Authorization': "Basic #{btoa("#{username}:#{password}")}"
    success: (d,s,x)->
      GitHub.token = d.token
      options.success(d,s,x) if options.success?
    error: options.error


# ## GitHub.sync
#
# The GitHub sync method is simply a customized version of the
# default Backbone sync mechanism with two improved properties:
#
# 1. It automatically sets an Accept header compatible with the
#    GitHub v3 API.
# 2. It sets the OAuth 2.0 Authorization header if `GitHub.token`
#    exists.
GitHub.sync = (method, model, options)->
  extendedOptions = _.extend {
    beforeSend: (xhr)->
      xhr.setRequestHeader 'Accept', 'application/vnd.github+json'
      xhr.setRequestHeader 'Authorization', "bearer #{GitHub.token}" if GitHub.token
  }, options
  Backbone.sync(method, model, extendedOptions)

GitHub.Model = Backbone.Model.extend
  sync: GitHub.sync

GitHub.Collection = Backbone.Collection.extend
  sync: GitHub.sync

GitHub.Relations =
  ownedRepos: (options)->
    repos = new GitHub.Repos
    repos.url = if typeof @url == "function" then @url() else @url
    repos.url += "/repos"
    repos.fetch(options)
    repos
  ownedOrgs: (options)->
    orgs = new GitHub.Organizations
    orgs.url = if typeof @url == "function" then @url() else @url
    orgs.url += "/orgs"
    orgs.fetch(options)
    orgs

# ## GitHub.User
# 
# The GitHub User model. For information about specific attributes
# that are available on this model, see GitHub's [Users API](http://developer.github.com/v3/users/)
# documentation.
GitHub.User = GitHub.Model.extend(
  urlRoot: 'https://api.github.com/users/'
  # ### user.repos(options)
  #
  # Fetch associated repositories for this user. Takes
  # a `success` and `error` callback as potential options.
  # Returns a `GitHub.Repos` collection.
  repos: GitHub.Relations.ownedRepos
  # ### user.organizations(options)
  #
  # Fetch associated organizations for this user. Takes
  # a `success` and `error` callback as potential options.
  organizations: GitHub.Relations.ownedOrgs
,
  # ### GitHub.User.fetch(name, options)
  #
  # Retrieve a user by username. Takes `success` and
  # `error` callbacks.
  #
  #     GitHub.User.fetch('mbleigh', {success: function(u){
  #       console.log(u.toJSON());
  #     }});
  fetch: (name, options)->
    user = new GitHub.User(id: name)
    user.fetch(options)
    user
)

# ## GitHub.Organization
# 
# Represents a GitHub organization. See the [Organization
# API](http://developer.github.com/v3/orgs/) docs on GitHub
# for additional information.
GitHub.Organization = GitHub.Model.extend(
  urlRoot: 'https://api.github.com/orgs/'

  # ### org.repos(options)
  # 
  # Fetch the repositories for the instantiated organization.
  # Takes `success` and `error` callbacks. Returns a
  # `GitHub.Repos` collection.
  repos: GitHub.Relations.ownedRepos
,
  # ### GitHub.Organization.fetch(name, options)
  #
  # Fetch an organization by name. Accepts `success`
  # and `error` callbacks.
  #
  #     GitHub.Organization.fetch('opperator', {success: function(o){
  #       console.log(o.toJSON());
  #     }});
  fetch: (name, options)->
    org = new GitHub.Organization(id: name)
    org.fetch(options)
    org
)

# ## GitHub.Organizations
#
# Collection of multiple organizations. By default will
# be associated to the current user's organizations (you
# must set an `GitHub.token` for that to work.)
GitHub.Organizations = GitHub.Collection.extend
  url: 'https://api.github.com/user/orgs'
  model: GitHub.Organization

# ## GitHub.Repo
#
# Repository model. For more information about attributes
# etc, see the [GitHub Repo](http://developer.github.com/v3/repos/)
# API docs.
GitHub.Repo = GitHub.Model.extend(
  url: ()->
    @get('url') || "https://api.github.com/repos/#{@get('path')}"
,
  # ### GitHub.Repo.fetch(owner, name, options)
  #
  # Retrieve a repository knowing its owner and name. Takes
  # `success` and `error` callbacks in options.
  #
  #     GitHub.Repo.fetch('opperator', 'backbone-github', {success: function(r){
  #       console.log(r.toJSON());
  #     }});
  fetch: (owner, name, options)->
    repo = new GitHub.Repo(path: "#{owner}/#{name}")
    repo.fetch(options)
    repo
)

# ## GitHub.Repos
# 
# Collection of Repo models. Defaults to the current user's
# repositories. Must have set `GitHub.token` for current
# user repo fetch to be successful.
GitHub.Repos = GitHub.Collection.extend
  url: 'https://api.github.com/user/repos'
  model: GitHub.Repo

# ## GitHub.currentUser
#
# A `GitHub.User` corresponding to the authenticated user.
# Note that you must set `GitHub.token` to a valid OAuth 2.0
# token to be able to utilize the current user.
#
# The `currentUser` is not fetched by default, you must run
# `GitHub.currentUser.fetch()` before it will be populated
# with data.
GitHub.currentUser = new GitHub.User()
GitHub.currentUser.url = "https://api.github.com/user"
GitHub.currentUser.urlRoot = null
Back to Top