adn-cli 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/adn-cli.gemspec CHANGED
@@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
16
16
  gem.version = ADN::CLI::VERSION
17
17
 
18
18
  gem.add_runtime_dependency('adn', '~> 0.3')
19
+ gem.add_runtime_dependency('ansi', '~> 1.4.3')
19
20
  end
data/bin/adn CHANGED
@@ -1,41 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- require 'webrick'
3
- require 'pp'
2
+ require_relative '../lib/adn/cli'
4
3
 
5
- TOKEN_FILE = File.expand_path("~/.adn-token")
6
- AUTH_URL = "https://alpha.app.net/oauth/authenticate?client_id=AWqdakvYjCn5vkcRtBzBLQxgkXGY4HSf&response_type=token&redirect_uri=http://localhost:9229/authorize/&scope=stream"
7
- PUBLIC = File.expand_path(File.dirname(__FILE__)) + "/../public"
8
-
9
- class TokenSaver < WEBrick::HTTPServlet::AbstractServlet
10
- def do_GET(request, response)
11
- Auth.save_token(request.query['access_token'])
12
- Auth.server.stop
13
- end
14
- end
15
-
16
- module Auth
17
- def self.server
18
- if @server.nil?
19
- @server = WEBrick::HTTPServer.new(:Port => 9229)
20
- @server.mount "/authorize", WEBrick::HTTPServlet::FileHandler, PUBLIC
21
- @server.mount "/save-token", TokenSaver
22
- end
23
- @server
24
- end
25
-
26
- def self.save_token(token)
27
- File.open(TOKEN_FILE, 'w') {|f| f.write(token) }
28
- @token = token
29
- end
30
-
31
- def self.token
32
- @token ||= IO.read(TOKEN_FILE) if File.exists?(TOKEN_FILE)
33
- if @token.nil?
34
- system("open \"#{AUTH_URL}\"")
35
- server.start
36
- end
37
- @token
38
- end
39
- end
40
-
41
- pp Auth.token
4
+ ADN::CLI.run
data/lib/adn/auth.rb ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'webrick'
4
+
5
+ module ADN
6
+ class TokenSaver < WEBrick::HTTPServlet::AbstractServlet
7
+
8
+ def save_token(token)
9
+ File.open(ADN::Auth::TOKEN_FILE, 'w') {|f| f.write(token) }
10
+ end
11
+
12
+ def do_GET(request, response)
13
+ save_token(request.query['access_token'])
14
+ ADN::Auth.server.stop
15
+ end
16
+ end
17
+
18
+ module Auth
19
+ TOKEN_FILE = File.expand_path("~/.adn-cli-token")
20
+ CLIENT_ID = 'bQQrxrhNXGVNrnZ6dTs6LDHfSfUFSX9Q'
21
+ AUTH_URL = "https://alpha.app.net/oauth/authenticate?client_id=#{CLIENT_ID}&response_type=token&redirect_uri=http://localhost:9229/authorize/&scope=stream"
22
+ PUBLIC = File.expand_path(File.dirname(__FILE__)) + "/../../public"
23
+
24
+ class << self
25
+ def has_token?
26
+ File.exists?(TOKEN_FILE)
27
+ end
28
+
29
+ def token
30
+ @token ||= IO.read(TOKEN_FILE)
31
+ end
32
+
33
+ def retrieve_token
34
+ puts "Retrieve OAuth2 token"
35
+ launch_browser(AUTH_URL)
36
+ server.start
37
+ end
38
+
39
+ def launch_browser(url)
40
+ system "open \"#{url}\""
41
+ end
42
+
43
+ def server
44
+ @server ||= begin
45
+ httpd = WEBrick::HTTPServer.new(
46
+ Port: 9229,
47
+ Logger: WEBrick::Log.new("/dev/null"),
48
+ AccessLog: [nil, nil]
49
+ )
50
+
51
+ httpd.tap { |s|
52
+ s.mount "/authorize", WEBrick::HTTPServlet::FileHandler, ADN::Auth::PUBLIC
53
+ s.mount "/save-token", ADN::TokenSaver
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,7 @@
1
+ # encoding: UTF-8
2
+
1
3
  module ADN
2
4
  class CLI
3
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
4
6
  end
5
7
  end
data/lib/adn/cli.rb ADDED
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'adn'
4
+ require 'ansi/core'
5
+ require 'ansi/table'
6
+
7
+ require_relative "cli/version"
8
+ require_relative "auth"
9
+
10
+ module ADN
11
+ class CLI
12
+ include ANSI::Code
13
+
14
+ def self.run
15
+ ADN::Auth.retrieve_token unless ADN::Auth.has_token?
16
+ new(ADN::Auth.token)
17
+ end
18
+
19
+ def initialize(token)
20
+ ADN.token = token
21
+
22
+ trap("INT"){ exit }
23
+
24
+ loop {
25
+ sleep 4
26
+ show_global_feed
27
+ }
28
+ end
29
+
30
+ def show_global_feed
31
+ get_global_feed.tap do |r|
32
+ show_posts(r)
33
+ update_since_id(r)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def get_global_feed
40
+ url = 'https://alpha-api.app.net/' +
41
+ 'stream/0/posts/stream/global?' +
42
+ 'count=10&include_directed_posts=1'
43
+
44
+ url += "&since_id=#{@since_id}" unless @since_id.nil?
45
+
46
+ ADN::API.get(url)
47
+ end
48
+
49
+ def update_since_id(response)
50
+ if response['data'].any?
51
+ @since_id = response['data'].first['id']
52
+ end
53
+ end
54
+
55
+ def show_posts(response)
56
+ response['data'].reverse.each do |p|
57
+ line
58
+
59
+ user_str = "#{p['user']['username']}".ansi(:blue) +
60
+ " (#{p['user']['name'].strip})".ansi(:yellow)
61
+
62
+ id_str = "id: #{p['id'].ansi(:cyan)}"
63
+
64
+ spaces = ANSI::Terminal.terminal_width -
65
+ ANSI.unansi(user_str + id_str).length
66
+
67
+ puts "#{user_str}#{" " * spaces}#{id_str}\n\n#{p['text']}"
68
+ end
69
+ end
70
+
71
+ def line(char = '—')
72
+ puts "\n"
73
+ puts "#{char * ANSI::Terminal.terminal_width}".ansi(:black)
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adn-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -28,6 +28,22 @@ dependencies:
28
28
  - - ~>
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: ansi
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 1.4.3
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.4.3
31
47
  description: App.net command line
32
48
  email:
33
49
  - git@justinbalthrop.com
@@ -44,7 +60,8 @@ files:
44
60
  - Rakefile
45
61
  - adn-cli.gemspec
46
62
  - bin/adn
47
- - lib/adn.rb
63
+ - lib/adn/auth.rb
64
+ - lib/adn/cli.rb
48
65
  - lib/adn/cli/version.rb
49
66
  - public/index.html
50
67
  homepage: https://github.com/adn-rb/adn-cli
data/lib/adn.rb DELETED
@@ -1,5 +0,0 @@
1
- require "adn/cli/version"
2
-
3
- module ADN
4
- # Your code goes here...
5
- end