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

.auto_url(key, services = []) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/avatars.io.rb', line 66

def self.auto_url(key, services = [])
  if services.size.eql? 0
    "http://avatars.io/auto/#{ key }"
  else
    "http://avatars.io/auto/#{ key }?services=#{ services.join ',' }"
  end
end

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



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

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

def self.upload(filename, identifier = '')
  file = File.read filename
  response = post '/v1/token', {
    :headers => {
      'x-client_id' => @@client_id,
      'Authorization' => "OAuth #{ @@access_token }",
      'Content-Type' => 'application/json'
    }, :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 = post "/v1/token/#{ response["data"]["id"] }/complete", {
    :headers => {
      'x-client_id' => @@client_id,
      'Authorization' => "OAuth #{ @@access_token }"
    },
    :body => ''
  }
  response["data"]
end