Class: Kounta::REST::Client

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

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/kounta/rest/client.rb', line 8

def initialize
	raise Kounta::Errors::MissingOauthDetails unless has_required_oauth_details?
	@logger = Yell.new STDOUT
	@conn = OAuth2::AccessToken.new(client, Kounta.client_token, {:refresh_token => Kounta.client_refresh_token})

end

Instance Method Details

#clientObject



15
16
17
18
19
20
21
# File 'lib/kounta/rest/client.rb', line 15

def client
	@oauth_client ||= OAuth2::Client.new(Kounta.client_id, Kounta.client_secret, {
		:site => Kounta::SITE_URI,
		:authorize_url => Kounta::AUTHORIZATION_URI,
		:token_url => Kounta::TOKEN_URI
	})
end

#object_from_response(klass, request_method, url_hash, options = {}) ⇒ Object



45
46
47
# File 'lib/kounta/rest/client.rb', line 45

def object_from_response(klass, request_method, url_hash, options={})
	klass.new( perform(url_hash, request_method, options) )
end

#objects_from_response(klass, request_method, url_hash, options = {}) ⇒ Object



41
42
43
# File 'lib/kounta/rest/client.rb', line 41

def objects_from_response(klass, request_method, url_hash, options={})
	perform(url_hash, request_method, options).map { |response| klass.new(response) }
end

#path_from_hash(url_hash) ⇒ Object



23
24
25
# File 'lib/kounta/rest/client.rb', line 23

def path_from_hash(url_hash)
	url_hash.map{ |key, value| value ? "#{key}/#{value}" : "#{key}" }.join('/')
end

#perform(url_hash, request_method, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kounta/rest/client.rb', line 27

def perform(url_hash, request_method, options={})
	begin
		log("#{request_method}: #{Kounta::SITE_URI}#{path_from_hash(url_hash)}.#{FORMAT.to_s} (#{options})")
		response = @conn.request(request_method, "#{path_from_hash(url_hash)}.#{FORMAT.to_s}", options)
	rescue OAuth2::Error => ex
		if ex.message.include? 'The access token provided has expired'
			log('oauth2 token expired, refreshing it...')
			@conn = refreshed_token
			retry
		end
	end
	response.parsed if response
end