Class: RFGraph::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rfgraph/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ Request

Returns a new instance of Request.



9
10
11
# File 'lib/rfgraph/request.rb', line 9

def initialize(access_token = nil)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/rfgraph/request.rb', line 7

def access_token
  @access_token
end

Instance Method Details

#delete_object(id) ⇒ Object



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

def delete_object(id)
  post_request(id, {"method"=> "delete"})
end

#get_connections(id, connection_name, args = {}) ⇒ Object



22
23
24
# File 'lib/rfgraph/request.rb', line 22

def get_connections(id, connection_name, args = {})
  return request(id + "/" + connection_name, args)
end

#get_object(id, args = {}) ⇒ Object



13
14
15
# File 'lib/rfgraph/request.rb', line 13

def get_object(id, args = {})
  request(id, args)
end

#get_objects(ids, args = {}) ⇒ Object



17
18
19
20
# File 'lib/rfgraph/request.rb', line 17

def get_objects(ids, args = {})
  args["ids"] = ids.join(",")
  return request("", args)
end

#post_request(path, post_args = {}) ⇒ Object



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

def post_request(path, post_args = {})
  request(path, {}, post_args)
end

#put_comment(object_id, message) ⇒ Object



35
36
37
# File 'lib/rfgraph/request.rb', line 35

def put_comment(object_id, message)
  put_object(object_id, "comments", {"message" => message})
end

#put_like(object_id) ⇒ Object



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

def put_like(object_id)
  put_object(object_id, "likes")
end

#put_object(parent_object, connection_name, data = {}) ⇒ Object

Raises:



26
27
28
29
# File 'lib/rfgraph/request.rb', line 26

def put_object(parent_object, connection_name, data = {})
  raise RFGraphError.new(-1, "Access token required for put requests") if access_token.nil?
  post_request(parent_object + "/" + connection_name, data)
end

#put_wall_post(message, attachment = {}, profile_id = "me") ⇒ Object



31
32
33
# File 'lib/rfgraph/request.rb', line 31

def put_wall_post(message, attachment = {}, profile_id = "me")
  put_object(profile_id, "feed", attachment.merge({'message' => message}))
end

#request(path, url_args = {}, post_args = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rfgraph/request.rb', line 51

def request(path, url_args = {}, post_args = nil)
  if access_token
    if post_args
      post_args["access_token"] = access_token
    else
      url_args["access_token"] = access_token
    end
  end

  encoded_url_args = url_args.collect {|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join("&")
  url = URI.parse("#{BASE_URL}/#{path}?#{encoded_url_args}")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = if post_args
    encoded_post_args = post_args.collect {|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join("&")
    http.post("#{url.path}?#{url.query}", encoded_post_args)
  else
    http.get("#{url.path}?#{url.query}")
  end

  response = JSON.parse(response.body)

  if response["error"]
    raise RFGraphError.new(response["error"]["code"], response["error"]["message"])
  end

  response
end