Class: Hyperpublic::OAuth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hyperpublic/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctoken, csecret, options = {}) ⇒ OAuth

Returns a new instance of OAuth.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hyperpublic/oauth.rb', line 12

def initialize(ctoken, csecret, options={})
  @ctoken, @csecret, @consumer_options = ctoken, csecret, {}
  @api_endpoint = options[:api_endpoint] || 'https://api.hyperpublic.com/api/v1'
  @consumer = ::OAuth::Consumer.new(ctoken, csecret, :site => @api_endpiont)
  @token = ::OAuth::AccessToken.new(@consumer)
  @signing_endpoint = options[:signing_endpoint] || 'https://api.hyperpublic.com/api/v1'
  if options[:sign_in]
    @consumer_options[:authorize_path] =  '/oauth/authenticate'
  end
  @auth_params = "client_id=#{@ctoken}&client_secret=#{@csecret}" + (@token.token.empty? ? "" : "&access_token=#{@token.token}")
  @auth_params_hash = {"client_id" => @ctoken, "client_secret" => @csecret}
  @auth_params_hash["access_token"] = @token.token unless @token.token.empty?
end

Instance Attribute Details

#csecretObject (readonly)

Returns the value of attribute csecret.



10
11
12
# File 'lib/hyperpublic/oauth.rb', line 10

def csecret
  @csecret
end

#ctokenObject (readonly)

Returns the value of attribute ctoken.



10
11
12
# File 'lib/hyperpublic/oauth.rb', line 10

def ctoken
  @ctoken
end

Instance Method Details

#access_tokenObject



47
48
49
# File 'lib/hyperpublic/oauth.rb', line 47

def access_token
  @access_token ||= ::OAuth::AccessToken.new(signing_consumer, @atoken, @asecret)
end

#authorize_from_access(atoken, asecret) ⇒ Object



51
52
53
# File 'lib/hyperpublic/oauth.rb', line 51

def authorize_from_access(atoken, asecret)
  @atoken, @asecret = atoken, asecret
end

#delete(uri, options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/hyperpublic/oauth.rb', line 41

def delete(uri, options={})
  path = @api_endpoint + uri
  sep = (path =~ /\/.*\?/) ? "&" : "?"
  @token.delete(path + sep + @auth_params)
end

#get(uri, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/hyperpublic/oauth.rb', line 26

def get(uri, options={})
  path = @api_endpoint + uri
  sep = (path =~ /\/.*\?/) ? "&" : "?"
  @token.get(path + sep + @auth_params)
end

#post(uri, body, options = {}) ⇒ Object



32
33
34
35
# File 'lib/hyperpublic/oauth.rb', line 32

def post(uri, body, options={})
  body.merge!("client_id" => @ctoken, "client_secret" => @csecret)
  @token.post(@api_endpoint + uri, body.to_json, {"Content-Type" => 'application/json'})
end

#put(uri, body, options = {}) ⇒ Object



37
38
39
# File 'lib/hyperpublic/oauth.rb', line 37

def put(uri, body, options={})
  @token.put(@api_endpoint + uri, body.merge(@auth_params_hash).to_json, {'Content-Type' => 'application/json'})
end