Class: Boomloop::Authentication::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, store) ⇒ Client

Returns a new instance of Client.



32
33
34
35
# File 'lib/boomloop/authentication/client.rb', line 32

def initialize(user, store)
  self.user = user
  self.store = store        
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



21
22
23
# File 'lib/boomloop/authentication/client.rb', line 21

def access_token
  @access_token
end

#connectionObject

Returns the value of attribute connection.



22
23
24
# File 'lib/boomloop/authentication/client.rb', line 22

def connection
  @connection
end

#consumerObject

Returns the value of attribute consumer.



21
22
23
# File 'lib/boomloop/authentication/client.rb', line 21

def consumer
  @consumer
end

#request_tokenObject

Returns the value of attribute request_token.



21
22
23
# File 'lib/boomloop/authentication/client.rb', line 21

def request_token
  @request_token
end

#storeObject

Returns the value of attribute store.



20
21
22
# File 'lib/boomloop/authentication/client.rb', line 20

def store
  @store
end

#userObject

Returns the value of attribute user.



20
21
22
# File 'lib/boomloop/authentication/client.rb', line 20

def user
  @user
end

Instance Method Details

#activateObject



44
45
46
47
48
49
# File 'lib/boomloop/authentication/client.rb', line 44

def activate
  if self.request_token
    self.access_token = self.request_token.get_access_token
    self.store.find(user).update(:access_key => self.access_token.token, :access_secret => self.access_token.secret)
  end
end

#connectObject



28
29
30
# File 'lib/boomloop/authentication/client.rb', line 28

def connect
  @connection ||= create_accesstoken
end

#consume(key, secret) ⇒ Object



37
38
39
40
41
42
# File 'lib/boomloop/authentication/client.rb', line 37

def consume(key, secret)
  self.create_consumer(key, secret)
  self.request_token = self.consumer.get_request_token
  self.store.find(user).update(:consumer_key => key, :consumer_secret => secret)
  self.request_token
end

#create_accesstokenObject



55
56
57
58
59
60
# File 'lib/boomloop/authentication/client.rb', line 55

def create_accesstoken
  credentials = self.store.find(self.user)
  raise_error_if_not_authenticated(credentials)        
  self.consumer ||= create_consumer(credentials[:consumer_key], credentials[:consumer_secret])
  self.access_token ||= OAuth::AccessToken.new(self.consumer, credentials[:access_key], credentials[:access_secret])
end

#create_consumer(key, secret) ⇒ Object



51
52
53
# File 'lib/boomloop/authentication/client.rb', line 51

def create_consumer(key, secret)
  self.consumer = OAuth::Consumer.new(key, secret, { :site => Boomloop::Resources::Base.api_base })
end

#get(url, options = {}) ⇒ Object



62
63
64
# File 'lib/boomloop/authentication/client.rb', line 62

def get(url, options = {})
  connection.get(url, options.merge({ "X-Consumer-Key" => self.consumer.key }))
end

#post(url, params = "", headers = {}) ⇒ Object



66
67
68
# File 'lib/boomloop/authentication/client.rb', line 66

def post(url, params = "", headers = {})
  connection.post(url, params, { "X-Consumer-Key" => self.consumer.key, 'Accept'=>'application/xml', 'Content-Type' => 'application/xml' }.merge(headers))
end

#raise_error_if_not_authenticated(credentials) ⇒ Object

Raises:



70
71
72
# File 'lib/boomloop/authentication/client.rb', line 70

def raise_error_if_not_authenticated(credentials)
  raise Boomloop::ApiError.new("You need to authenticate the api before getting an access token. Do this by running the command 'boomloop'") unless credentials[:consumer_secret]
end