Class: AvatarsIO

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/avatars.io.rb

Constant Summary collapse

@@client_id =
''
@@access_token =
''

Class Method Summary collapse

Class Method Details

.access_token=(access_token) ⇒ Object



18
19
20
# File 'lib/avatars.io.rb', line 18

def self.access_token=(access_token)
  @@access_token = access_token
end

.avatar_url(service = 'twitter', key) ⇒ Object



61
62
63
# File 'lib/avatars.io.rb', line 61

def self.avatar_url(service = 'twitter', key)
  "http://avatars.io/#{ service }/#{ key }"
end

.client_id=(client_id) ⇒ Object



14
15
16
# File 'lib/avatars.io.rb', line 14

def self.client_id=(client_id)
  @@client_id = client_id
end

.upload(filename, identifier = '') ⇒ Object



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
# File 'lib/avatars.io.rb', line 22

def self.upload(filename, identifier = '')
  file = File.read filename
  response = self.post '/v1/token', {
    :headers => {
      'x-client_id' => @@client_id,
      'Authorization' => "OAuth #{ @@access_token }"
    }, :body => {
      :data => {
        :filename => filename,
        :md5 => Digest::MD5::hexdigest(file),
        :size => File.size(filename),
        :path => identifier
      }
    }.to_json
  }
  return response["data"]["url"] if !response["data"]["upload_info"]
  
  info = response["data"]["upload_info"]
  AvatarsIOFile.put info["upload_url"].gsub('http://s3.amazonaws.com', ''), {
    :headers => {
      'Authorization' => info["signature"],
      'Date' => info["date"],
      'Content-Type' => info["content_type"],
      'x-amz-acl' => 'public-read'
    },
    :body => file
  }
  
  response = self.post "/v1/token/#{ response["data"]["id"] }/complete", {
    :headers => {
      'x-client_id' => @@client_id,
      'Authorization' => "OAuth #{ @@access_token }"
    },
    :body => ''
  }
  
  response["data"]
end