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 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

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



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)
      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



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.

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



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_allObject

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

Returns:

  • (Boolean)


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