Class: PublisherSDK

Inherits:
SDKComponent show all
Defined in:
lib/publisher_sdk.rb

Constant Summary collapse

ALLOWED_FAILURES =
2
@@token_cache =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SDKComponent

#ensure_slash, #query_decode

Constructor Details

#initialize(pub_id, secret, is_test = false) ⇒ PublisherSDK

Returns a new instance of PublisherSDK.



8
9
10
11
12
13
14
# File 'lib/publisher_sdk.rb', line 8

def initialize pub_id, secret, is_test = false
	@pub_id = pub_id.to_s
	@secret = secret.to_s
	@cache_key = Digest::MD5.hexdigest @pub_id + '-' + @secret
	@endpoint = ""
	SDKHost::test? is_test
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/publisher_sdk.rb', line 16

def method_missing name, *args
	name = name.to_s
	if name=='pub' || name=='publisher'
		return extend_endpoint @pub_id
	else
		return extend_endpoint name
	end
	super name, *args
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



5
6
7
# File 'lib/publisher_sdk.rb', line 5

def endpoint
  @endpoint
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/publisher_sdk.rb', line 5

def token
  @token
end

Instance Method Details

#clear_token_failuresObject



105
106
107
# File 'lib/publisher_sdk.rb', line 105

def clear_token_failures
	self.set_stored_token self.get_stored_token
end

#construct_request(method, arg1, arg2) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/publisher_sdk.rb', line 69

def construct_request method, arg1, arg2
	if arg1.is_a? String
		@endpoint += self.ensure_slash arg1
		self.make_graph_request arg2, method
	else
		self.make_graph_request arg1, method
	end
end

#delete(arg1 = nil, arg2 = nil) ⇒ Object



65
66
67
# File 'lib/publisher_sdk.rb', line 65

def delete arg1=nil, arg2=nil
	self.construct_request 'delete', arg1, arg2
end

#extend_endpoint(endpoint) ⇒ Object



26
27
28
29
30
# File 'lib/publisher_sdk.rb', line 26

def extend_endpoint endpoint
	result =  PublisherSDK.new @pub_id,@secret
	result.endpoint = @endpoint + self.ensure_slash(endpoint)
	result
end

#fail_tokenObject



100
101
102
103
# File 'lib/publisher_sdk.rb', line 100

def fail_token
	t = self.get_stored_token_object
	self.set_stored_token t[:token], t[:failures]+1
end

#get(arg1 = nil, arg2 = nil) ⇒ Object



57
58
59
# File 'lib/publisher_sdk.rb', line 57

def get arg1=nil, arg2=nil
	self.construct_request 'get', arg1, arg2
end

#get_stored_tokenObject



43
44
45
# File 'lib/publisher_sdk.rb', line 43

def get_stored_token
	return self.get_stored_token_object[:token]
end

#get_stored_token_objectObject



47
48
49
# File 'lib/publisher_sdk.rb', line 47

def get_stored_token_object
	return @@token_cache[@cache_key] ? @@token_cache[@cache_key] : {:token => nil, :failures => 0}
end

#get_tokenObject



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

def get_token
	if !self.get_stored_token
		self.get_token_from_server
	end
	return get_stored_token
end

#get_token_failuresObject



109
110
111
112
# File 'lib/publisher_sdk.rb', line 109

def get_token_failures
	t = self.get_stored_token_object
	t[:failures]
end

#get_token_from_serverObject



51
52
53
54
55
# File 'lib/publisher_sdk.rb', line 51

def get_token_from_server
	req = GraphRequest.new 'oauth/access_token', {:client_id => @pub_id, :client_secret=>@secret, :grant_type=> 'publisher_credentials'}
	set_stored_token req.get_response.response['access_token']
	get_stored_token
end

#handle_response(response) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/publisher_sdk.rb', line 86

def handle_response response
	if !response.error.nil?
		if response.error.code==190
			return self.retry_request_with_new_token response
		else
			self.clear_token_failures
			raise response.error
		end
	end
	@endpoint = ""
	self.clear_token_failures
	response
end

#make_graph_request(params, method) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/publisher_sdk.rb', line 78

def make_graph_request params, method
	if params.nil?
		params = {}
	end
	params[:access_token] = self.get_token	
	self.handle_response(GraphRequest.new(@endpoint, params, method).get_response)
end

#post(arg1 = nil, arg2 = nil) ⇒ Object



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

def post arg1=nil, arg2=nil
	self.construct_request 'post', arg1, arg2
end

#retry_request_with_new_token(resp) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/publisher_sdk.rb', line 114

def retry_request_with_new_token resp
	self.fail_token
	if self.get_token_failures <= PublisherSDK::ALLOWED_FAILURES
		req = resp.request
		req.params[:access_token] = self.get_token_from_server
		return self.handle_response req.get_response
	end
	raise resp.error
end

#set_stored_token(token, failures = 0) ⇒ Object



39
40
41
# File 'lib/publisher_sdk.rb', line 39

def set_stored_token token, failures = 0
	@@token_cache[@cache_key] = {:token => token, :failures => failures}
end