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. 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)
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)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/quora/client.rb', line 47 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
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/quora/client.rb', line 125 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quora/client.rb', line 77 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
66 67 68 69 |
# File 'lib/quora/client.rb', line 66 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
104 105 106 107 108 109 110 |
# File 'lib/quora/client.rb', line 104 def respond_to?(method_id, include_private = false) if method_id.to_s =~ /^get_[\w]+/ true else super end end |