Class: FourRuby::OAuth2

Inherits:
Object
  • Object
show all
Defined in:
lib/four_ruby/oauth2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret = nil, options = {}) ⇒ OAuth2

Returns a new instance of OAuth2.



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

def initialize(id, secret=nil, options={})
  if secret
    @id = id
    @secret = secret
  else
    require 'yaml'
    yml = YAML::load(File.open(id))
    raise StandardError, "Foursquare config file does not exist" if yml['foursquare'].nil? 
    @id = yml['foursquare']['client_id']
    @secret = yml['foursquare']['client_secret']
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/four_ruby/oauth2.rb', line 10

def client
  @client
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/four_ruby/oauth2.rb', line 10

def id
  @id
end

#secretObject

Returns the value of attribute secret.



10
11
12
# File 'lib/four_ruby/oauth2.rb', line 10

def secret
  @secret
end

Instance Method Details

#access_token(code, options = {}) ⇒ Object



30
31
32
# File 'lib/four_ruby/oauth2.rb', line 30

def access_token(code, options={})
  @access_token ||= client.get_access_token(code, options)
end

#authorize_url(redirect_uri) ⇒ Object



34
35
36
37
38
39
# File 'lib/four_ruby/oauth2.rb', line 34

def authorize_url(redirect_uri)
  client.web_server.authorize_url(
    :redirect_uri => redirect_uri,
    :scope => 'email,offline_access'
  )
end

#get(url) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/four_ruby/oauth2.rb', line 41

def get(url)
  if @access_token
    @access_token.get(url)
  else
    HTTParty.get(url)
  end
end

#post(url, body) ⇒ Object



49
50
51
# File 'lib/four_ruby/oauth2.rb', line 49

def post(url, body)
  HTTParty.post(url, body)
end