Class: Pocketbeuter::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pocketbeuter/cli.rb

Constant Summary collapse

ADD_URL =
'https://getpocket.com/v3/add'
SEND_URL =
'https://getpocket.com/v3/send'
GET_URL =
'https://getpocket.com/v3/get'
OAUTH_URL =
'https://getpocket.com/v3/oauth/request'
AUTH_URL =
'https://getpocket.com/auth/authorize'
OAUTH_AUTH_URL =
'https://getpocket.com/v3/oauth/authorize'
POCKET_DEV =
'http://getpocket.com/developer/apps/new'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



20
21
22
23
# File 'lib/pocketbeuter/cli.rb', line 20

def initialize(*)
  @config = Pocketbeuter::ConfigFile.instance
  super
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/pocketbeuter/cli.rb', line 19

def config
  @config
end

Instance Method Details

#authorizeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pocketbeuter/cli.rb', line 55

def authorize
  @config.path ||= options['config'] if options['config']
  if @config.empty?
    createapp
  end
  say 'Getting request token ...'
  uri = URI.parse(OAUTH_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data("consumer_key" => @config.consumer_key, "redirect_uri" => @config.redirect_uri)
  http.use_ssl = true
  res = http.request(request)
  @config.code = URI.decode_www_form(res.body).first[1]
  auth_url = AUTH_URL + "?request_token=#{@config.code}&redirect_uri=#{@config.redirect_uri}"
  say "Go to: #{auth_url}"
  Launchy.open(auth_url)
  ask 'Press any key after authorization ...'
  uri = URI.parse(OAUTH_AUTH_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data("consumer_key" => @config.consumer_key, "code" => @config.code)
  http.use_ssl = true
  res = http.request(request)
  @config.access_token = URI.decode_www_form(res.body)[0][1]
  @config.username = URI.decode_www_form(res.body)[1][1]
  @config.save_config
end

#createappObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pocketbeuter/cli.rb', line 26

def createapp
  say "Good day Sir/Madam before you can use pocketbeuter, you'll first"
  say "need to register your application. Please follow below steps:"
  say "    1. Log in into Pocket site."
  say "    2. Create an Application - complete required fields and click"
  say "       \"CREATE APPLICATION\" button."
  say "       NOTE: application must have unique name (I recommend: <login>/pocketbeuter),"
  say "             add application description and give Add, Modify and Retrieve permissions,"
  say "             select platform for your application (i.e. Desktop (other)), and accept"
  say "             license terms"
  ask " Press any key to open Pocket Create an Application form"
  say
  Launchy.open(Pocketbeuter::CLI::POCKET_DEV)
   = ask "Enter account name [#{ENV['USER']}]:"
  if .empty?
    @config. = ENV['USER']
  else
    @config. = 
  end

  #TODO: handle empty consumer_key
  @config.consumer_key = ask 'Enter consumer key:'
  #TODO: handle empty redirect_uri
  redirect_uri = ask 'Enter redirect uri:'
  @config.redirect_uri = redirect_uri
  @config.save_config
end