Class: Sheep::User

Inherits:
Object
  • Object
show all
Defined in:
lib/sheep/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ User

Returns a new instance of User.



39
40
41
42
43
44
# File 'lib/sheep/user.rb', line 39

def initialize(data)
  @data = {}
  data.each do |key, value|
    self[key] = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attribute, value = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/sheep/user.rb', line 46

def method_missing(attribute, value = nil)
  if attribute =~/\=$/
    self[attribute.to_s.gsub(/\=$/, '')] = value
  else
    self[attribute]
  end
end

Class Method Details

.authorize!(username, password) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sheep/user.rb', line 19

def authorize!(username, password)
  status, headers, body = Sheep.connection.post('/v1/oauth/access_token') do |request|
    request.params['username'] = username
    request.params['password'] = password
    request.params['client_id'] = Sheep.client_id
    request.params['client_secret'] = Sheep.client_secret
    request.params['grant_type'] = 'password'
  end
  # TODO: access_token should be in user object, not global
  Sheep.access_token = body['access_token']
end

.create(attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/sheep/user.rb', line 8

def create(attributes)
  status, headers, body = Sheep.connection.post('/v1/users') do |request|
    request.params = { :user => attributes }
    request.params['client_id'] = Sheep.client_id
    request.params['client_secret'] = Sheep.client_secret
  end
  # TODO: refactor this to use Authorized when it will be finished
  Sheep.access_token = body['access_token']
  new(Sheep.connection.get(headers['Location'])[2]['user'])
end

.find(id) ⇒ Object



4
5
6
# File 'lib/sheep/user.rb', line 4

def find(id)
  new(Sheep.connection.get("/v1/users/#{id}")[2]['user'])
end

Instance Method Details

#[](attribute) ⇒ Object



54
55
56
# File 'lib/sheep/user.rb', line 54

def [](attribute)
  @data[attribute.to_s]
end

#[]=(attribute, value) ⇒ Object



58
59
60
# File 'lib/sheep/user.rb', line 58

def []=(attribute, value)
  @data[attribute.to_s] = value
end

#activitiesObject



32
33
34
35
36
37
# File 'lib/sheep/user.rb', line 32

def activities
  status, headers, body = Sheep.connection.get('/v1/activities', {}, {'Authorization' => "Bearer #{Sheep.access_token}"})
  body.map do |activity|
    Sheep::Activity.new(activity['activity'])
  end
end