Class: Foursquare::Merchant::Consumer

Inherits:
Base
  • Object
show all
Defined in:
lib/foursquare/consumer.rb

Constant Summary

Constants included from Requests

Requests::API, Requests::OAUTH

Instance Attribute Summary collapse

Attributes inherited from Base

#consumer

Instance Method Summary collapse

Methods included from Requests

#get, #post

Constructor Details

#initialize(*args) ⇒ Consumer

Returns a new instance of Consumer.



8
9
10
11
12
13
14
15
16
# File 'lib/foursquare/consumer.rb', line 8

def initialize(*args)
  if args.size == 1
    @oauth_token = args.first
  elsif args.size == 2
    @client_id, @client_secret = args
  else
    raise ArgumentError, "Please include either an access token or client id & secret"
  end
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/foursquare/consumer.rb', line 7

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/foursquare/consumer.rb', line 7

def client_secret
  @client_secret
end

#oauth_tokenObject (readonly)

Returns the value of attribute oauth_token.



7
8
9
# File 'lib/foursquare/consumer.rb', line 7

def oauth_token
  @oauth_token
end

Instance Method Details

#access_token(code, callback_url) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/foursquare/consumer.rb', line 32

def access_token(code, callback_url)
  raise "Please ensure you've defined the client_id" if @client_id.nil?
  raise "Please ensure you've defined the client_secret" if @client_secret.nil?
  raise "No code was provided" if code.nil?
  raise "Invalid callback url" if (callback_url.nil? or callback_url.scan(/http\:\/\//).empty?)

  query = {
    :client_id => @client_id,
    :client_secret => @client_secret,
    :grant_type => 'authorization_code',
    :redirect_uri => callback_url,
    :code => code
  }
  request = self.class.get("/access_token", {:query => query})
  if request.code == 200
    request.parsed_response['access_token']
  else
    raise Errors::OAuthError, request.parsed_response['error']
  end
end

#authorize_url(callback_url) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/foursquare/consumer.rb', line 18

def authorize_url(callback_url)
  raise ArgumentError.new("Please use a valid client_id") unless @client_id
  if callback_url =~ /http/
    query = {
      :client_id => @client_id,
      :response_type => 'code',
      :redirect_uri => callback_url
    }.map { |k,v| "#{k}=#{v}"}.join('&')
    "#{OAUTH}/authenticate?#{query}"
  else
    raise ArgumentError, "Please ensure you specify a valid callback url, including the protocol (http://)"
  end
end