api.js.coffee 1.66 KB
Newer Older
1 2 3 4
@Api =
  users_path: "/api/:version/users.json"
  user_path: "/api/:version/users/:id.json"
  notes_path: "/api/:version/projects/:id/notes.json"
5
  namespaces_path: "/api/:version/namespaces.json"
6

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  # Get 20 (depends on api) recent notes
8 9 10 11
  # and sort the ascending from oldest to newest
  notes: (project_id, callback) ->
    url = Api.buildUrl(Api.notes_path)
    url = url.replace(':id', project_id)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
    $.ajax(
      url: url,
      data:
        private_token: gon.api_token
        gfm: true
        recent: true
      dataType: "json"
    ).done (notes) ->
      notes.sort (a, b) ->
        return a.id - b.id
      callback(notes)

  user: (user_id, callback) ->
    url = Api.buildUrl(Api.user_path)
    url = url.replace(':id', user_id)

    $.ajax(
      url: url
      data:
        private_token: gon.api_token
      dataType: "json"
    ).done (user) ->
      callback(user)

  # Return users list. Filtered by query
  # Only active users retrieved
  users: (query, callback) ->
    url = Api.buildUrl(Api.users_path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41

42 43 44 45 46 47 48 49 50 51 52
    $.ajax(
      url: url
      data:
        private_token: gon.api_token
        search: query
        per_page: 20
        active: true
      dataType: "json"
    ).done (users) ->
      callback(users)

53 54 55 56 57 58 59 60 61 62 63 64 65 66
  # Return namespaces list. Filtered by query
  namespaces: (query, callback) ->
    url = Api.buildUrl(Api.namespaces_path)

    $.ajax(
      url: url
      data:
        private_token: gon.api_token
        search: query
        per_page: 20
      dataType: "json"
    ).done (namespaces) ->
      callback(namespaces)

67
  buildUrl: (url) ->
Jim's avatar
Jim committed
68
    url = gon.relative_url_root + url if gon.relative_url_root?
69
    return url.replace(':version', gon.api_version)