       base64Data +
       close_delim
 
-    request = gapi.client.request({
-      'path': '/upload/drive/v2/files',
-      'method': 'POST',
-      'params': {'uploadType': 'multipart'},
-      'headers': {
-        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
-      },
-      'body': multipartRequestBody})
-    if (!callback)
-      callback = (file) ->
-        console.log(file)
-    request.execute(callback)
+    return multipartRequestBody
 
   utf8_to_b64: (str) ->
     window.btoa( unescape(encodeURIComponent( str ) ) )
 
+
 GDriveModel = Backbone.Model.extend({
   initialize: ()->
-    this.CLIENT_ID = '966231612988-cmob8calt2b646p4sddlb4410q2eekmq'
-    + '.apps.googleusercontent.com'
-    this.SCOPES = 'https://www.googleapis.com/auth/drive'
+    this.gdrive = new GDrive()
     this.authResult = this.authorize()
+    this.file = null
 
   authorize: ()->
     that = this
     return null unless gapi.auth
 
     gapi.auth.authorize({
-      'client_id': this.CLIENT_ID,
-      'scope': this.SCOPES,
+      'client_id': this.gdrive.CLIENT_ID,
+      'scope': this.gdrive.SCOPES,
       'immediate': true },
       (authResult)->
         # immediate: trueが失敗したときはflaseで再チャレンジ

  CLIENT_ID: '966231612988-cmob8calt2b646p4sddlb4410q2eekmq' +
  '.apps.googleusercontent.com'
  SCOPES: 'https://www.googleapis.com/auth/drive'

   # Start the file upload.
   # @param {Object} evt Arguments from the file selector.
  uploadFile: (evt) ->
    that = this
    console.log('upload file')

    content = $('#markdown > textarea').val()
    title = 'test_file.txt'
    console.log(content)
    gapi.client.load('drive', 'v2', ->
      that.insertFile(title, content)
    )

   # Insert new file.
   # @param {File} fileData File object to read data from.
   # @param {Function} callback Function to call when the request is complete.
  insertFile: (title, content, callback) ->
    that = GDrive.prototype
    console.log('insert file')
    boundary = '-------314159265358979323846'
    delimiter = "\r\n--" + boundary + "\r\n"
    close_delim = "\r\n--" + boundary + "--"

    contentType = 'text/plain'
    metadata = {
      'title': title,
      'mimeType': contentType
    }
    console.log(metadata)

    base64Data = that.utf8_to_b64(content)
    multipartRequestBody =
      delimiter +
      'Content-Type: application/json\r\n\r\n' +

        else
          console.log("immediate false")
          gapi.auth.authorize({
            'client_id': this.gdrive.CLIENT_ID,
            'scope': this.gdrive.SCOPES,
            'immediate': false },
            (authResult)->
              that.authResult = authResult
          )
    )

  upload: (title, content) ->
    that = this
    console.log('upload file')

    console.log(content)
    gapi.client.load('drive', 'v2', ->
      that.insert(title, content)
    )

  insert: (title, content, callback) ->
    that = this
    console.log('insert file')
    multipartRequestBody = this.gdrive.requestBody(title, content)
    request = gapi.client.request({
      'path': '/upload/drive/v2/files',
      'method': 'POST',
      'params': {'uploadType': 'multipart'},
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' +
        this.gdrive.BOUNDARY + '"'
      },
      'body': multipartRequestBody})
    if (!callback)
      callback = (file) ->
        console.log(file)
        that.file = file
    request.execute(callback)
})
GDriveView = Backbone.View.extend({
  el: '#gdrive'
  events:
    'click [name=authorize-button]': 'gapi_authorize'
    'click [name=upload-button]': 'upload'
  initialize: ()->

  gapi_authorize: ()->
    this.model.authorize()
  upload: ()->
    content = $('#markdown > textarea').val()
    title = 'test_file.txt'
    this.model.upload(title, content)
})

gdriveModel = new GDriveModel()
