Class: Gyaazle::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gyaazle/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/gyaazle/client.rb', line 5

def initialize(config)
  @config = config
  @agent = HTTPClient.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



3
4
5
# File 'lib/gyaazle/client.rb', line 3

def agent
  @agent
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/gyaazle/client.rb', line 3

def config
  @config
end

Instance Method Details

#authorization_header_valueObject



95
96
97
# File 'lib/gyaazle/client.rb', line 95

def authorization_header_value
  "#{credentials[:token_type]} #{credentials[:access_token]}"
end

#authorize_urlObject



10
11
12
13
# File 'lib/gyaazle/client.rb', line 10

def authorize_url
  url = "https://accounts.google.com/o/oauth2/auth?client_id=#{config.id}&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive"
  Nokogiri::HTML.parse(agent.get(url).body).at('a').attributes["href"].to_s
end

#create_folder(name) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gyaazle/client.rb', line 111

def create_folder(name)
  json = agent.post_content(
    "https://www.googleapis.com/drive/v2/files",
    MultiJson.dump({
      :title => name,
      :mimeType => "application/vnd.google-apps.folder",
      :parents => [{:id => "root"}],
    }),
    {
      "Authorization" => authorization_header_value,
      'Content-Type' => 'application/json;charset=utf-8',
    }
  )
  folder = MultiJson.load(json, :symbolize_keys => true)
  config.update(:folder_id => folder[:id])
  folder[:id]
end

#credentialsObject



129
130
131
# File 'lib/gyaazle/client.rb', line 129

def credentials
  config.load
end

#folder_idObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gyaazle/client.rb', line 99

def folder_id
  id = credentials[:folder_id]
  return create_folder("Gyaazle") unless id

  folder = get_file_info(id)
  if !folder[:id] || folder[:labels][:trashed]
    create_folder("Gyaazle") 
  else
    id
  end
end

#get_file_info(file_id) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gyaazle/client.rb', line 83

def get_file_info(file_id)
  json = agent.get(
    "https://www.googleapis.com/drive/v2/files/#{file_id}",
    nil,
    {
      "Authorization" => authorization_header_value,
    }
  ).body

  MultiJson.load(json, :symbolize_keys => true)
end

#get_tokens(verifier) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/gyaazle/client.rb', line 15

def get_tokens(verifier)
  agent.post_content("https://accounts.google.com/o/oauth2/token",{
    :code => verifier,
    :client_id => config.id,
    :client_secret => config.secret,
    :redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
    :grant_type => "authorization_code",
  })
end

#refresh_token!Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/gyaazle/client.rb', line 25

def refresh_token!
  json = agent.post("https://accounts.google.com/o/oauth2/token", {
    :refresh_token => credentials[:refresh_token],
    :client_id => config.id,
    :client_secret => config.secret,
    :grant_type => "refresh_token",
  }).body
  config.update(:access_token => MultiJson.load(json)["access_token"])
  config.load
end

#set_permissions(file_id, permissions = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gyaazle/client.rb', line 63

def set_permissions(file_id, permissions = nil)
  json = MultiJson.dump(
    permissions || credentials[:permissions] || {
      :role => "reader",
      :type => "#{"anyone"}",
      :value => "#{"me"}",
      :withLink => "true",
      :additionalRoles => ["commenter"],
    }
  )
  agent.post_content(
    "https://www.googleapis.com/drive/v2/files/#{file_id}/permissions",
    json,
    {
      "Authorization" => authorization_header_value,
      'Content-Type' => 'application/json;charset=utf-8',
    }
  )
end

#upload(file, metadata = nil) ⇒ Object



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
# File 'lib/gyaazle/client.rb', line 36

def upload(file,  = nil)
  body = [
    {
      'Content-Type' => 'application/json;charset=utf-8',
      :content => MultiJson.dump( || {
        :title => File.basename(file),
        :shared => "true",
        :parents => [:id => folder_id]
      })
    },
    {
      :content => File.read(file),
    },
  ]

  response = agent.post(
    'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart',
    body,
    {
      "Authorization" => authorization_header_value,
      "Content-Type" =>  "multipart/related; boundary=___#{Time.now.to_f}___",
    }
  )
  # Note: deeplink is https://drive.google.com/uc?export=view&id={fileId}
  MultiJson.load(response.body, :symbolize_keys => true)
end