Class: GraphRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SDKComponent

#ensure_slash, #query_decode

Constructor Details

#initialize(endpoint = nil, params = {}, method = 'get') ⇒ GraphRequest

Returns a new instance of GraphRequest.



28
29
30
31
32
33
34
35
36
37
# File 'lib/graph_request.rb', line 28

def initialize endpoint=nil, params={}, method='get'
	@endpoint = self.ensure_slash endpoint
	if params.empty?
		@params = {} 
	else 
		@params = params
	end
	@method = method
	@params[:method] = @method
end

Instance Attribute Details

#effective_urlObject

Returns the value of attribute effective_url.



26
27
28
# File 'lib/graph_request.rb', line 26

def effective_url
  @effective_url
end

#paramsObject

Returns the value of attribute params.



26
27
28
# File 'lib/graph_request.rb', line 26

def params
  @params
end

#testObject

Returns the value of attribute test.



26
27
28
# File 'lib/graph_request.rb', line 26

def test
  @test
end

Instance Method Details

#clean_params(params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graph_request.rb', line 69

def clean_params params
	p = {}
	params.each { |k,v|  
		if v.is_a?(String)
			p[k.to_s] = v
		else
			p[k.to_s] = v.to_json.to_s
		end
	}
	p
end

#get_responseObject



39
40
41
42
43
44
45
46
47
# File 'lib/graph_request.rb', line 39

def get_response
	result = self.run
	if result[0]=="{" || result[0] == "["
		_data = JSON.parse result
	else
		_data = self.query_decode result
	end
	GraphResponse.new _data, self
end

#make_uri(host, endpoint = "", params = {}) ⇒ Object



63
64
65
66
67
# File 'lib/graph_request.rb', line 63

def make_uri host, endpoint="", params={}
	url = URI host + endpoint		
	url.query = URI.encode_www_form params
	url
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graph_request.rb', line 49

def run	
	params = self.clean_params(@params)
	url = self.make_uri SDKHost::current_host  
	client = Net::HTTP.new url.host, url.port
	client.use_ssl = true
	client.ca_file = Cacert.pem
	req = Net::HTTP::Post.new @endpoint
	req.add_field 'User-Agent', 'goplay-ruby-'+GoplayPublisherSDK::VERSION
	req.set_form_data params
	response = client.start {|http| http.request(req) }
	@effective_url = make_uri(SDKHost::current_host, @endpoint, params).to_s
	response.body
end