Class: Geoloqi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/geoloqi/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/geoloqi/session.rb', line 6

def initialize(opts={})
  opts[:config] = Geoloqi::Config.new opts[:config] if opts[:config].is_a? Hash
  @config = opts[:config] || (Geoloqi.config || Geoloqi::Config.new)
  self.auth = opts[:auth] || {}
  self.auth[:access_token] = opts[:access_token] if opts[:access_token]

  @connection = Faraday.new(:url => API_URL) do |builder|
    builder.response :logger if @config.enable_logging
    builder.adapter  @config.adapter || :net_http
  end
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



3
4
5
# File 'lib/geoloqi/session.rb', line 3

def auth
  @auth
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/geoloqi/session.rb', line 4

def config
  @config
end

Instance Method Details

#access_tokenObject



22
23
24
# File 'lib/geoloqi/session.rb', line 22

def access_token
  @auth[:access_token]
end

#access_token?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/geoloqi/session.rb', line 26

def access_token?
  !access_token.nil?
end

#authorize_url(redirect_uri = @config.redirect_uri, opts = {}) ⇒ Object



30
31
32
# File 'lib/geoloqi/session.rb', line 30

def authorize_url(redirect_uri=@config.redirect_uri, opts={})
  Geoloqi.authorize_url @config.client_id, redirect_uri, opts
end

#establish(opts = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/geoloqi/session.rb', line 76

def establish(opts={})
  require 'client_id and client_secret are required to get access token' unless @config.client_id? && @config.client_secret?
  auth = post 'oauth/token', {:client_id => @config.client_id,
                              :client_secret => @config.client_secret}.merge!(opts)

  # expires_at is likely incorrect. I'm chopping 5 seconds
  # off to allow for a more graceful failover.
  auth['expires_at'] = auth_expires_at auth['expires_in']
  self.auth = auth
  self.auth
end

#execute(meth, path, query = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/geoloqi/session.rb', line 63

def execute(meth, path, query=nil)
  query = Rack::Utils.parse_query query if query.is_a?(String)
  raw = @connection.send(meth) do |req|
    req.url "/#{API_VERSION.to_s}/#{path.gsub(/^\//, '')}"
    req.headers = headers

    if query
      meth == :get ? req.params = query : req.body = query.to_json
    end
  end
  Response.new raw.status, raw.headers, raw.body
end

#get(path, query = nil) ⇒ Object



34
35
36
# File 'lib/geoloqi/session.rb', line 34

def get(path, query=nil)
  run :get, path, query
end

#get_auth(code, redirect_uri = @config.redirect_uri) ⇒ Object



92
93
94
# File 'lib/geoloqi/session.rb', line 92

def get_auth(code, redirect_uri=@config.redirect_uri)
  establish :grant_type => 'authorization_code', :code => code, :redirect_uri => redirect_uri
end

#post(path, query = nil) ⇒ Object



38
39
40
# File 'lib/geoloqi/session.rb', line 38

def post(path, query=nil)
  run :post, path, query
end

#renew_access_token!Object



88
89
90
# File 'lib/geoloqi/session.rb', line 88

def renew_access_token!
  establish :grant_type => 'refresh_token', :refresh_token => self.auth[:refresh_token]
end

#run(meth, path, query = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/geoloqi/session.rb', line 42

def run(meth, path, query=nil)
  renew_access_token! if auth[:expires_at] && Time.rfc2822(auth[:expires_at]) <= Time.now && !(path =~ /^\/?oauth\/token$/)
  retry_attempt = 0

  begin
    response = execute meth, path, query
    hash = JSON.parse response.body
    raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
  rescue Geoloqi::ApiError
    raise Error.new('Unable to procure fresh access token from API on second attempt') if retry_attempt > 0
    if hash['error'] == 'expired_token'
      renew_access_token!
      retry_attempt += 1
      retry
    else
      fail
    end
  end
  @config.use_hashie_mash ? Hashie::Mash.new(hash) : hash
end