goplay-publisher-sdk 0.0.901

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjkyMWQ1YzY5NTc3ZWQwMjBlZDc4ODI2NTQwNTEyMmQxODcyNWU5NA==
5
+ data.tar.gz: !binary |-
6
+ ODEwZGQ2ZDg2ZDY5N2JkZmRkZDg0NTc5MjRjODRiOTU1ODFjY2Y1OA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NGNmMGIwZmQxZThmNjY3MDEyYzI0YzFkNzBjMzFmZmEyODhkMmExZjZkYTBl
10
+ MmUwNDQ3NmM1NDQ0NzY0MjJlMTQzYTA0N2Q0ZjhmNTE5OGRkZTEyNzQzYzEz
11
+ YjRjNThiMjhhN2VmMmU1MGJiNGMzZDM1YzIwODRlNGUwZGZjMjE=
12
+ data.tar.gz: !binary |-
13
+ NzQwNDU4Mzg1MGJiYzQzNDJiNzhjNzY2ODI5OTIxNzYwYjQyYTVkZTk2YTUy
14
+ YzdjYWUzMzVmM2ZjY2MxZmVkODY2MWM3NjU4YzhmY2ZkNTkyODhhMTFhOGQy
15
+ MWM3ZTkwNTRiMGRmYmI5Y2YyNzBkNjYzODQxOGZjY2YxNzA3OTY=
@@ -0,0 +1,82 @@
1
+ require_relative 'sdk_component.rb'
2
+ require 'json'
3
+ require 'cacert'
4
+ require 'net/https'
5
+
6
+ module SDKHost
7
+ attr_accessor 'test'
8
+ @@host = 'https://graph.goplay.com'
9
+ @@testhost = 'https://test-graph.goplay.com'
10
+ @@test = false
11
+
12
+ def SDKHost.test? val=nil
13
+ if val
14
+ @@test = val
15
+ end
16
+ @@test
17
+ end
18
+
19
+ def SDKHost.current_host
20
+ @@test ? @@testhost : @@host
21
+ end
22
+ end
23
+
24
+ class GraphRequest < SDKComponent
25
+ attr_accessor 'effective_url','test'
26
+
27
+ def initialize endpoint=nil, params={}, method='get'
28
+ @endpoint = self.ensure_slash endpoint
29
+ if params.empty?
30
+ @params = {}
31
+ else
32
+ @params = params
33
+ end
34
+ @method = method
35
+ @params[:method] = @method
36
+ end
37
+
38
+
39
+ def get_response
40
+ result = self.run
41
+ if result[0]=="{" || result[0] == "["
42
+ _data = JSON.parse result
43
+ else
44
+ _data = self.query_decode result
45
+ end
46
+ GraphResponse.new _data, self
47
+ end
48
+
49
+
50
+
51
+ def run
52
+ params = self.clean_params(@params)
53
+ url = self.make_uri SDKHost::current_host
54
+ client = Net::HTTP.new url.host, url.port
55
+ client.use_ssl = true
56
+ client.ca_file = Cacert.pem
57
+ req = Net::HTTP::Post.new @endpoint
58
+ req.set_form_data params
59
+ response = client.start {|http| http.request(req) }
60
+ @effective_url = make_uri(SDKHost::current_host, @endpoint, params).to_s
61
+ response.body
62
+ end
63
+
64
+ def make_uri host, endpoint="", params={}
65
+ url = URI host + endpoint
66
+ url.query = URI.encode_www_form params
67
+ url
68
+ end
69
+
70
+ def clean_params params
71
+ p = {}
72
+ params.each { |k,v|
73
+ if v.is_a?(String)
74
+ p[k.to_s] = v
75
+ else
76
+ p[k.to_s] = v.to_json.to_s
77
+ end
78
+ }
79
+ p
80
+ end
81
+
82
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'sdk_component.rb'
2
+ require_relative 'sdk_exception.rb'
3
+ require 'json'
4
+ require 'uri'
5
+ require 'rack'
6
+
7
+ class GraphResponse < SDKComponent
8
+ attr_accessor :request
9
+ attr_accessor :response
10
+ attr_accessor :data
11
+ attr_accessor :error
12
+ attr_accessor :paging
13
+
14
+ def initialize response, request
15
+ @request = request
16
+ @response = response
17
+ if !response.is_a?(Hash)
18
+ response = {"data" => response}
19
+ end
20
+
21
+ if response["data"].nil?
22
+ @data = response
23
+ else
24
+ @data = response["data"]
25
+ end
26
+ if !response["error"].nil?
27
+ @error = SDKException.new "Exception from "+request.effective_url+": "+response["error"]["message"]
28
+ end
29
+ @paging = response["paging"]
30
+ end
31
+
32
+ def next
33
+ self.iterate @paging['next']
34
+ end
35
+
36
+ def previous
37
+ self.iterate @paging['previous']
38
+ end
39
+
40
+ def iterate url
41
+ if url
42
+ url = URI(URI.encode url)
43
+ params = Rack::Utils.parse_nested_query(url.query)
44
+ GraphRequest.new(url.path, params).get_response
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,85 @@
1
+ require_relative 'graph_request'
2
+ require_relative 'graph_response'
3
+ require 'digest/md5'
4
+ class PublisherSDK < SDKComponent
5
+ attr_accessor :endpoint, :token
6
+ @@token_cache = {}
7
+ def initialize pub_id, secret
8
+ @pub_id = pub_id.to_s
9
+ @secret = secret.to_s
10
+ @cache_key = Digest::MD5.hexdigest @pub_id + '-' + @secret
11
+ @endpoint = ""
12
+ end
13
+
14
+ def method_missing name, *args
15
+ name = name.to_s
16
+ if name=='pub' || name=='publisher'
17
+ return extend_endpoint @pub_id
18
+ else
19
+ return extend_endpoint name
20
+ end
21
+ super name, *args
22
+ end
23
+
24
+ def extend_endpoint endpoint
25
+ result = PublisherSDK.new @pub_id,@secret
26
+ result.endpoint = @endpoint + self.ensure_slash(endpoint)
27
+ result.token = get_stored_token;
28
+ result
29
+ end
30
+
31
+ def get_token
32
+ if !self.get_stored_token
33
+ @@token_cache[@cache_key] = {:token => self.get_token_from_server, :failures => 0}
34
+ end
35
+ return @@token_cache[@cache_key]
36
+ end
37
+
38
+ def get_stored_token
39
+ return @@token_cache[@cache_key] ? @@token_cache[@cache_key][:token] : nil
40
+ end
41
+
42
+ def get_token_from_server
43
+ req = GraphRequest.new 'oauth/access_token', {:client_id => @pub_id, :client_secret=>@secret, :grant_type=> 'publisher_credentials'}
44
+ req.get_response.response["access_token"]
45
+ end
46
+
47
+ def get arg1=nil, arg2=nil
48
+ self.construct_request 'get', arg1, arg2
49
+ end
50
+
51
+ def post arg1=nil, arg2=nil
52
+ self.construct_request 'post', arg1, arg2
53
+ end
54
+
55
+ def delete arg1=nil, arg2=nil
56
+ self.construct_request 'delete', arg1, arg2
57
+ end
58
+
59
+ def construct_request method, arg1, arg2
60
+ if arg1.is_a? String
61
+ @endpoint = arg1
62
+ self.make_graph_request arg2, method
63
+ else
64
+ self.make_graph_request arg1, method
65
+ end
66
+ end
67
+
68
+ def make_graph_request params, method
69
+ if params.nil?
70
+ params = {}
71
+ end
72
+ params[:access_token] = self.get_token
73
+ endpoint = @endpoint
74
+ @endpoint = ""
75
+ resp = self.handle_response(GraphRequest.new(endpoint, params, method).get_response)
76
+ end
77
+
78
+ def handle_response response
79
+ if !response.error.nil?
80
+ raise response.error
81
+ end
82
+ return response
83
+ end
84
+
85
+ end
@@ -0,0 +1,22 @@
1
+ class SDKComponent
2
+
3
+ def ensure_slash s
4
+ s = s.to_s
5
+ if s.empty?
6
+ s = ""
7
+ elsif s[0]!='/'
8
+ s = "/"+s
9
+ else
10
+ s
11
+ end
12
+ end
13
+
14
+ def query_decode strs
15
+ _hash = {}
16
+ for str in strs.split("&")
17
+ s = str.split("=")
18
+ _hash[s[0]]= s[1]
19
+ end
20
+ _hash
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class SDKException < Exception
2
+
3
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goplay-publisher-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.901
5
+ platform: ruby
6
+ authors:
7
+ - Keith Nordstrom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A fluent SDK for retrieving Publisher information from the GoPlay graph.
14
+ email: keith@goplay.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/graph_request.rb
20
+ - lib/graph_response.rb
21
+ - lib/sdk_component.rb
22
+ - lib/publisher_sdk.rb
23
+ - lib/sdk_exception.rb
24
+ homepage: http://rubygems.org/gems/publisher-sdk
25
+ licenses: []
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A fluent SDK for retrieving Publisher information from the GoPlay graph.
47
+ test_files: []