module Openplacos::Connection

Public Instance Methods

create_client() click to toggle source
# File lib/openplacos/libclient.rb, line 80
def create_client
  @client = OAuth2::Client.new(@client_id,
                               @client_secret,
                               {:site => "#{@url}", 
                                 :token_url => '/oauth/authorize'
                               }
                               ) 
end
get_grant_url(type_) click to toggle source
# File lib/openplacos/libclient.rb, line 89
def get_grant_url(type_)
  @client.auth_code.authorize_url(:redirect_uri => @redirect_uri, :scope => @scope.join(" "))
end
load_config() click to toggle source

restore config

# File lib/openplacos/libclient.rb, line 106
def load_config
  if File.exists?(@file_config)
    @token_params  = YAML::load(File.read(@file_config))
    if @token_params[@url]
      @client_id     = @token_params[@url][:client_id] 
      @client_secret = @token_params[@url][:client_secret]
    end
  else
    @token_params = Hash.new
  end
end
recreate_token() click to toggle source
# File lib/openplacos/libclient.rb, line 118
def recreate_token
  @token = OAuth2::AccessToken.from_hash(@client, @token_params[@url])
end
register( ) click to toggle source

register the client (automatic way)

# File lib/openplacos/libclient.rb, line 62
def register( )
  postData = Net::HTTP.post_form(URI.parse("#{@url}/oauth/apps"), 
                                 { 'name'        =>"#{@name}",
                                   'redirect_uri'=>@redirect_uri,
                                   'format'      =>'json'
                                 }
                                 )
  if postData.code == "200"            # Check the status code
    client_param = JSON.parse( postData.body)
  else
    puts "error code"
    exit 0
  end

  @client_id = client_param["client_id"]
  @client_secret = client_param["client_secret"]
end
save_config() click to toggle source

save config with token, client_id and secret into a userspace directory needed to not regrant client every connection

# File lib/openplacos/libclient.rb, line 95
def save_config
  @token_params[@url]                 = @token ? @token.to_hash : {}
  @token_params[@url][:client_id]     = @client_id
  @token_params[@url][:client_secret] = @client_secret

  File.open(@file_config, 'w') do |out|
    YAML::dump( @token_params, out )
  end
end