Module: Openplacos::Connection
- Included in:
- Connection_auth_code, Connection_password
- Defined in:
- lib/openplacos/libclient.rb
Instance Method Summary collapse
- #create_client ⇒ Object
- #get_grant_url(type_) ⇒ Object
-
#load_config ⇒ Object
restore config.
- #recreate_token ⇒ Object
-
#register ⇒ Object
register the client (automatic way).
-
#save_config ⇒ Object
save config with token, client_id and secret into a userspace directory needed to not regrant client every connection.
Instance Method Details
#create_client ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/openplacos/libclient.rb', line 81 def create_client @client = OAuth2::Client.new(@client_id, @client_secret, {:site => "#{@url}", :token_url => '/oauth/authorize' } ) end |
#get_grant_url(type_) ⇒ Object
90 91 92 |
# File 'lib/openplacos/libclient.rb', line 90 def get_grant_url(type_) @client.auth_code.(:redirect_uri => @redirect_uri, :scope => @scope.join(" ")) end |
#load_config ⇒ Object
restore config
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/openplacos/libclient.rb', line 107 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 ⇒ Object
119 120 121 |
# File 'lib/openplacos/libclient.rb', line 119 def recreate_token @token = OAuth2::AccessToken.from_hash(@client, @token_params[@url]) end |
#register ⇒ Object
register the client (automatic way)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/openplacos/libclient.rb', line 63 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 ⇒ Object
save config with token, client_id and secret into a userspace directory needed to not regrant client every connection
96 97 98 99 100 101 102 103 104 |
# File 'lib/openplacos/libclient.rb', line 96 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 |