Class: Quora::Client
Overview
This is the main class to interact with Quora API.
Actually there’s no API security mechanism so interaction with API is based on authentication cookie or web authentication (providing user and password). If you don’t want to expose user credentials You should get all the Quora cookies value from you browser and use it as argument while creating the Quora client:
cookie = “m-b=<m-b-value>; m-f=<m-f-value>; m-s=<m-s-value>; …”
client = Quora::Client.new(cookie)
Or using user credentials:
client = Quora::Client.new(=> valid_user, :password => valid_password)
values = client.get_all
Constant Summary collapse
- QUORA_URI =
"http://api.quora.com"
- RESP_PREFIX =
"while(1);"
- BASEPATH =
"/api/logged_in_user"
- SUPPORTED_FIELDS =
%W{inbox followers following notifs}
Instance Method Summary collapse
-
#get(field, filter = true) ⇒ Object
Base method to send a request to Quora API.
-
#get_all ⇒ Object
Get all the user information available.
-
#initialize(params) ⇒ Client
constructor
Initialize the client.
-
#method_missing(method_id, *arguments, &block) ⇒ Object
Override method_missing so any method that starts with “get_” will be defined.
-
#respond_to?(method_id, include_private = false) ⇒ Boolean
Any method that starts with “get_” will be defined so if new fields are supported there’s no need to fix the client.
Methods included from Auth
Constructor Details
#initialize(params) ⇒ Client
Initialize the client. client = Client.new(valid_cookie) client = Client.new(=> valid_user, :password => valid_password)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/quora/client.rb', line 53 def initialize(params) if params.nil? raise ArgumentError, "Cookie value must be provided" else if params.instance_of?(String) = params elsif params.instance_of?(Hash) user = params[:user] password = params[:password] user.nil? or password.nil? and raise ArgumentError, "user and password must be provided" = login(user, password) end end @cookie = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
Override method_missing so any method that starts with “get_” will be defined.
i.e. client.get_profile
will generate =>
def get_profile
get("profile")
end
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/quora/client.rb', line 131 def method_missing(method_id, *arguments, &block) if method_id.to_s =~ /^get_[\w]+/ self.class.send :define_method, method_id do field = method_id.to_s[4..-1] get(field) end self.send(method_id) else super end end |
Instance Method Details
#get(field, filter = true) ⇒ Object
Base method to send a request to Quora API.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/quora/client.rb', line 83 def get(field, filter = true) if field.nil? or !field.instance_of?(String) raise ArgumentError, "Field value must be a string" end resp = http.get("#{BASEPATH}?fields=#{field}", headers) data = resp.body[RESP_PREFIX.length..-1] data = JSON.parse(data) if filter && data.has_key?(field) data[field] else data end end |
#get_all ⇒ Object
Get all the user information available
72 73 74 75 |
# File 'lib/quora/client.rb', line 72 def get_all fields = SUPPORTED_FIELDS.join(",") get(fields) end |
#respond_to?(method_id, include_private = false) ⇒ Boolean
Any method that starts with “get_” will be defined so if new fields are supported there’s no need to fix the client
110 111 112 113 114 115 116 |
# File 'lib/quora/client.rb', line 110 def respond_to?(method_id, include_private = false) if method_id.to_s =~ /^get_[\w]+/ true else super end end |