Class: OauthCli

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth_cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile = nil) ⇒ OauthCli

Returns a new instance of OauthCli.



18
19
20
21
# File 'lib/oauth_cli.rb', line 18

def initialize(profile = nil)
  @options = {}
  connect(profile)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/oauth_cli.rb', line 16

def options
  @options
end

Class Method Details

.add_profile(name, values) ⇒ Object



109
110
111
112
# File 'lib/oauth_cli.rb', line 109

def self.add_profile(name, values)
  @profiles[name] = values
  name
end

.inializeObject

Static Setup Mehtods



92
93
94
# File 'lib/oauth_cli.rb', line 92

def self.inialize
  @profiles || {}
end

.load_profiles(cfg_file, tmp_file) ⇒ Object



96
97
98
99
100
# File 'lib/oauth_cli.rb', line 96

def self.load_profiles(cfg_file, tmp_file)
  @cfg_file = cfg_file #keep so we can save back to file
  @profiles = load(cfg_file)
  @templates = load(tmp_file)
end

.parse_args(args, opt = {}, last_arg = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/oauth_cli.rb', line 122

def self.parse_args(args, opt = {}, last_arg = nil)
  @profiles ||= {}
  method, uri, body  = args.clone.delete_if do |kv|
    next opt[$1.to_sym] = $2  if kv =~ /-?-([^=]+)=(.+)$/   #catches --param=value
    next opt[last_arg] = kv   if last_arg && !opt[last_arg] #catches value
    next last_arg = $1.to_sym if kv =~ /^-?-(.+)$/          #catches --param
    false
  end

  profile = opt.delete(:profile) || opt.delete(:p)

  if !@profiles[profile] && opt.any?
    profile ||= 'commandline'
    @profiles[profile] = opt
    save_profiles unless profile == 'commandline'
  end

  if !@profiles[profile] && @default_profile
    profile = @default_profile
    say "Using default profile: #{profile}"
  end

  if profile && !@profiles[profile]
    say_error "Profile #{profile} not found"
    profile = nil
  end

  [method, uri, body, profile]
end

.profilesObject



114
115
116
# File 'lib/oauth_cli.rb', line 114

def self.profiles
  @profiles || {}
end

.save_profilesObject



102
103
104
105
106
107
# File 'lib/oauth_cli.rb', line 102

def self.save_profiles
  return unless @cfg_file
  File.open(@cfg_file, 'w') do |out|
     YAML.dump(@profiles, out)
  end
end

.templatesObject



118
119
120
# File 'lib/oauth_cli.rb', line 118

def self.templates
  @templates || {}
end

Instance Method Details

#access_request_urlObject



47
48
49
50
51
# File 'lib/oauth_cli.rb', line 47

def access_request_url
  @request_token = @consumer.get_request_token({}, "oauth_callback" => "oob")
  @options[:auth_url] ||= "#{@options[:host].gsub('api.', 'www.').gsub('v1/', '')}/mobile/authorize" #That's for Qype only!!
  url = "#{@options[:auth_url]}?oauth_token=#{@request_token.token}"
end

#access_token(verifier) ⇒ Object



53
54
55
56
# File 'lib/oauth_cli.rb', line 53

def access_token(verifier)
  request_url unless @request_token
  @access_token = @request_token.get_access_token({}, "oauth_verifier" => verifier)
end

#auth?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/oauth_cli.rb', line 43

def auth?
  @access_token
end

#connect(profile) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oauth_cli.rb', line 23

def connect(profile)
  return false if !profile.is_a?(Hash) && !OauthCli.profiles[profile]
  @options = OauthCli.profiles[profile] || profile

  #add http if missing
  [:host, :reg_url, :auth_url].each do |key|
    @options[key] = "http://#{@options[key]}" unless @options[key] =~ /^http/
  end

  @consumer     = OAuth::Consumer.new(@options[:consumer_key], @options[:consumer_secret], :site => @options[:host])
  @access_token = OAuth::AccessToken.new(@consumer, @options[:token], @options[:token_secret]) if @options[:token]

  connected?
end

#connected?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/oauth_cli.rb', line 38

def connected?
  # TODO check if host available?
  @options[:consumer_key] && @options[:consumer_secret] && @options[:host]
end

#request(method, uri, body = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oauth_cli.rb', line 58

def request(method, uri, body = nil)
  unless %w(get post put delete).include? method.to_s
    say_message "Wrong HTTP Method: #{method}  - calling #{@options[:host]}#{uri}", 'RED'
    return
  end

  uri  = ask_prompt "request uri" if !uri
  body = ask_prompt "request body" if !body && (method == "post" || method == "put")

  url = @options[:host] + uri.match(/^\/?(.+)/)[1] #removes trailing slash

  @options[:mime_type]    ||= (url =~ /\.json/) ? "application/json" : "application/xml"
  @options[:content_type] ||= (url =~ /\.json/) ? "application/json" : "application/xml"

  response = @consumer.request(method, url, @access_token, {}, body, { 'Accept' => @options[:mime_type], 'Content-Type' => @options[:content_type] })

  header = response.header

  color = (response.code.to_i < 400) ? 'GREEN' : 'RED'
  say_message "Status: #{header.code} #{header.message}  - calling #{url}", color

  body = response.body
  if header.content_type =~ /json/
    body = JSON.parse(body)
    ap(body) rescue say(JSON.pretty_generate(body))
    return
  end

  body = " <%= color('# #{$1.gsub("'", "")} ', RED) %>" if body =~ /<pre>(.+)<\/pre>/
  say body
end