Class: Geni::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/geni/client.rb

Constant Summary collapse

SITE =
'https://www.geni.com'
ACCESS_TOKEN_PATH =
'/oauth/token'
TYPES =
{
  'profile'  => Geni::Profile,
  'union'    => Geni::Union,
  'project'  => Geni::Project,
  'photo'    => Geni::Photo,
  'document' => Geni::Document,
  'video'    => Geni::Video
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/geni/client.rb', line 9

def initialize(params = {})

  @callback = params[:callback] ? params[:callback] : '/callback'

  @oauth_client = OAuth2::Client.new(params[:app_id], params[:app_secret],
    :site              => SITE,
    :parse_json        => true,
    :access_token_path => ACCESS_TOKEN_PATH
  )
  
  @access_token = OAuth2::AccessToken.new(oauth_client, params[:token])
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/geni/client.rb', line 7

def access_token
  @access_token
end

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/geni/client.rb', line 7

def callback
  @callback
end

#oauth_clientObject (readonly)

Returns the value of attribute oauth_client.



7
8
9
# File 'lib/geni/client.rb', line 7

def oauth_client
  @oauth_client
end

Instance Method Details

#[](entity_id) ⇒ Object



65
66
67
68
# File 'lib/geni/client.rb', line 65

def [](entity_id)
  entity_type = entity_id.split('-').first
  send("get_#{entity_type}", entity_id)
end

#authorize_url(request) ⇒ Object



77
78
79
80
81
# File 'lib/geni/client.rb', line 77

def authorize_url(request)
  oauth_client.web_server.authorize_url({
    :redirect_uri => redirect_uri(request)
  })
end

#get_profiles(ids) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/geni/client.rb', line 40

def get_profiles(ids)
  return [] if ids.empty?
  return [get_profile(ids.first)] if ids.one?
  
  numbers = ids.collect { |id| id.gsub(/profile-/, '') }.join(',')
  url = "/api/profile-#{numbers}"
  results = access_token.get(url)['results']
  
  results.collect do |profile_attrs|
    Geni::Profile.new({
      :client  => self,
      :attrs   => profile_attrs,
      :fetched => true
    })
  end
end

#get_token(code, request) ⇒ Object



83
84
85
86
87
# File 'lib/geni/client.rb', line 83

def get_token(code, request)
  oauth_client.web_server.get_access_token(code, {
    :redirect_uri => redirect_uri(request)
  }).token
end

#meObject



57
58
59
60
61
62
63
# File 'lib/geni/client.rb', line 57

def me
  Geni::Profile.new({
    :client  => self,
    :attrs   => access_token.get('/api/profile'),
    :fetched => true
  })
end

#redirect_uri(request) ⇒ Object



70
71
72
73
74
75
# File 'lib/geni/client.rb', line 70

def redirect_uri(request)
  uri = URI.parse(request.url)
  uri.path = @callback
  uri.query = nil
  uri.to_s
end