Class: Quora::Client

Inherits:
Object
  • Object
show all
Includes:
Auth
Defined in:
lib/quora/client.rb

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

Methods included from Auth

#login

Constructor Details

#initialize(params) ⇒ Client

Initialize the client. client = Client.new(valid_cookie) client = Client.new(=> valid_user, :password => valid_password)

Parameters:

  • User (required, string|Hash)

    identification. Can be either a valid cookie previously authenticated or an Hash with :user and :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)
      cookie = 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"
      cookie = (user, password)
    end
  end
  @cookie = 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.

Parameters:

  • supported (required, string)

    field (or multiple fields CSV) to retrieve

  • filter (optional, bool) (defaults to: true)

    if field is a key in result hash, only this value is returned



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_allObject

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

Returns:

  • (Boolean)


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