class Rpx::RpxHelper

Attributes

api_key[R]
base_url[R]
realm[R]

Public Class Methods

new(api_key, base_url, realm) click to toggle source
# File lib/rpx.rb, line 19
def initialize(api_key, base_url, realm)
  @api_key = api_key
  @base_url = base_url.sub(/\/*$/, '')
  @realm = realm
end

Public Instance Methods

all_mappings() click to toggle source
# File lib/rpx.rb, line 40
def all_mappings
  data = api_call 'all_mappings'
  data['mappings']
end
auth_info(token, token_url) click to toggle source
# File lib/rpx.rb, line 25
def auth_info(token, token_url)
  data = api_call 'auth_info', :token => token, :tokenUrl => token_url
  data['profile']
end
auth_info_iphone(token) click to toggle source
# File lib/rpx.rb, line 30
def auth_info_iphone(token)
    data = api_call 'auth_info', :token => token
    data['profile']
end
map(identifier, key) click to toggle source
# File lib/rpx.rb, line 45
def map(identifier, key)
  api_call 'map', :primaryKey => key, :identifier => identifier
end
mappings(primary_key) click to toggle source
# File lib/rpx.rb, line 35
def mappings(primary_key)
  data = api_call 'mappings', :primaryKey => primary_key
  data['identifiers']
end
signin_url(dest) click to toggle source
# File lib/rpx.rb, line 53
def signin_url(dest)
  "#{rp_url}/openid/signin?token_url=#{CGI.escape(dest)}"
end
unmap(identifier, key) click to toggle source
# File lib/rpx.rb, line 49
def unmap(identifier, key)
  api_call 'unmap', :primaryKey => key, :identifier => identifier
end

Private Instance Methods

api_call(method_name, partial_query) click to toggle source
# File lib/rpx.rb, line 65
def api_call(method_name, partial_query)
  url = URI.parse("#{@base_url}/api/v2/#{method_name}")

  query = partial_query.dup
  query['format'] = 'json'
  query['apiKey'] = @api_key

  http = Net::HTTP.new(url.host, url.port)
  if url.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  data = query.map { |k,v|
    "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}"
  }.join('&')

  resp = http.post(url.path, data)

  if resp.code == '200'
    begin
      data = JSON.parse(resp.body)
    rescue JSON::ParserError => err
      raise RpxException.new(resp), 'Unable to parse JSON response'
    end
  else
    raise RpxException.new(resp), "Unexpected HTTP status code from server: #{resp.code}"
  end

  if data['stat'] != 'ok'
    raise RpxException.new(resp), 'Unexpected API error'
  end

  return data
end
rp_url() click to toggle source
# File lib/rpx.rb, line 59
def rp_url
  parts = @base_url.split('://', 2)
  parts = parts.insert(1, '://' + @realm + '.')
  return parts.join('')
end