Class: SoulPoints::Client
- Inherits:
-
Object
- Object
- SoulPoints::Client
show all
- Includes:
- Helpers
- Defined in:
- lib/soul_points/client.rb
Overview
A Ruby class to call the MySoulPoints REST API.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
#ask, #confirm, #confirm_command, #deprecate, #display, #error, #format_date, #home_directory, #redisplay, #retry_on_exception, #running_on_a_mac?, #running_on_windows?, #shell
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
28
29
30
|
# File 'lib/soul_points/client.rb', line 28
def initialize
@credentials = {}
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
26
27
28
|
# File 'lib/soul_points/client.rb', line 26
def host
@host
end
|
#password ⇒ Object
Returns the value of attribute password.
26
27
28
|
# File 'lib/soul_points/client.rb', line 26
def password
@password
end
|
#user ⇒ Object
Returns the value of attribute user.
26
27
28
|
# File 'lib/soul_points/client.rb', line 26
def user
@user
end
|
Class Method Details
.api_enpoint ⇒ Object
22
23
24
|
# File 'lib/soul_points/client.rb', line 22
def self.api_enpoint
"http://mysoulpoints.com"
end
|
.gem_version_string ⇒ Object
18
19
20
|
# File 'lib/soul_points/client.rb', line 18
def self.gem_version_string
"soul_points-gem/#{version}"
end
|
.version ⇒ Object
14
15
16
|
# File 'lib/soul_points/client.rb', line 14
def self.version
SoulPoints::VERSION
end
|
Instance Method Details
#couldnt_find_credentials ⇒ Object
108
109
110
|
# File 'lib/soul_points/client.rb', line 108
def couldnt_find_credentials
puts "Could not find your credentials. To save your credentials run: \n$ soul_points store_api_key YOUR_KEY_HERE\n"
end
|
#credentials_file ⇒ Object
112
113
114
|
# File 'lib/soul_points/client.rb', line 112
def credentials_file
"#{home_directory}/.soul_points"
end
|
#escape(value) ⇒ Object
143
144
145
146
|
# File 'lib/soul_points/client.rb', line 143
def escape(value) escaped = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
escaped.gsub('.', '%2E') end
|
#gain(args) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/soul_points/client.rb', line 75
def gain( args )
resp = RestClient.post 'http://mysoulpoints.com/events.json', :event => {
:value => args[0],
:description => args[1],
:post_to_campfire => ( ( @credentials[:always_post_to_campfire] && args.index('--no-campfire').nil? ) || !args.index('--campfire').nil? ) ? 1 : 0,
:post_to_jaconda => ( ( @credentials[:always_post_to_jaconda] && args.index('--no-jaconda').nil? ) || !args.index('--jaconda').nil? ) ? 1 : 0,
:post_to_twitter => ( ( @credentials[:always_post_to_twitter] && args.index('--no-twitter').nil? ) || !args.index('--twitter').nil? ) ? 1 : 0
}, :auth_token => @credentials[:api_key], :accept => :json
if args[0].to_i > 0
puts "You \033[1;32mgained\033[0m #{args[0].to_i.abs} soul points - \"#{args[1]}\"."
else
puts "You \033[1;31mlost\033[0m #{args[0].to_i.abs} soul points - \"#{args[1]}\"."
end
me(args)
end
|
#help(args) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/soul_points/client.rb', line 46
def help( args )
puts 'Usage:'
puts '$ soul_points show mcphat #Displays soul points for user mcphat'
puts '$ soul_points gain 10 "Something awesome happend" '
puts '$ soul_points lose 10 "Something terrible happend"'
puts '$ soul_points gain 10 "Something awesome happend" --twitter #Also posts to twitter if you have it enabled on the site '
puts '$ soul_points gain 10 "Something awesome happend" --no-twitter #Will override any default settings in your ~/.soul_points file and not post to twitter '
puts ''
puts 'Your ~/.soul_points file may contain the following. You can replace 1 with 0 to get the desired effect.'
puts 'The command line options override any defaults you may have in your ~/.soul_points file.'
puts ''
puts ':always_post_to_campfire: true'
puts ':always_post_to_twitter: false'
puts ':always_post_to_jaconda: true'
end
|
#load_credentials ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/soul_points/client.rb', line 100
def load_credentials
@credentials = read_credentials
if !@credentials || @credentials[:api_key].nil?
couldnt_find_credentials
exit
end
end
|
#lose(args) ⇒ Object
Pretty much just an alias for gain, with negative number
95
96
97
98
|
# File 'lib/soul_points/client.rb', line 95
def lose( args )
args[0] = args[0].to_i * -1
gain( args )
end
|
#me(args) ⇒ Object
69
70
71
72
73
|
# File 'lib/soul_points/client.rb', line 69
def me( args )
me = JSON.parse( RestClient.get 'http://mysoulpoints.com/me.json?auth_token=' + @credentials[:api_key], :accept => :json )
puts me['email']
puts '(' + me['soul_points']['current'].to_s + '/' + me['soul_points']['max'].to_s + ') ' + sprintf( "%0.1f", ( me['soul_points']['current'].to_f / me['soul_points']['max'].to_f ) * 100 ) + "%"
end
|
#read_credentials ⇒ Object
116
117
118
|
# File 'lib/soul_points/client.rb', line 116
def read_credentials
YAML::load( File.read(credentials_file) ) if File.exists?(credentials_file)
end
|
#run_command(command, args) ⇒ Object
32
33
34
35
36
|
# File 'lib/soul_points/client.rb', line 32
def run_command( command, args )
load_credentials if command != 'store_api_key' && command != 'help'
raise InvalidCommand unless self.respond_to?(command)
self.send(command, args)
end
|
#set_credentials_permissions ⇒ Object
127
128
129
130
|
# File 'lib/soul_points/client.rb', line 127
def set_credentials_permissions
FileUtils.chmod 0700, File.dirname(credentials_file)
FileUtils.chmod 0600, credentials_file
end
|
#show(args) ⇒ Object
Show your current soul points
63
64
65
66
67
|
# File 'lib/soul_points/client.rb', line 63
def show( args )
subdomain = args[0]
soul_points = JSON.parse( RestClient.get 'http://' + subdomain + '.mysoulpoints.com', :accept => :json )
puts '(' + soul_points['soul_point']['current'].to_s + '/' + soul_points['soul_point']['max'].to_s + ') ' + sprintf( "%0.1f", ( soul_points['soul_point']['current'].to_f / soul_points['soul_point']['max'].to_f ) * 100 ) + "%"
end
|
134
135
136
137
138
139
140
141
|
# File 'lib/soul_points/client.rb', line 134
def {
'X-SoulPoints-API-Version' => '1',
'User-Agent' => self.class.gem_version_string,
'X-Ruby-Version' => RUBY_VERSION,
'X-Ruby-Platform' => RUBY_PLATFORM
}
end
|
#store_api_key(args) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/soul_points/client.rb', line 38
def store_api_key( args )
@api_key = args[0]
@credentials = {
:api_key => @api_key
}
write_credentials
end
|
#write_credentials ⇒ Object
120
121
122
123
124
125
|
# File 'lib/soul_points/client.rb', line 120
def write_credentials
File.open(credentials_file, 'w') do |f|
f.puts @credentials.to_yaml
end
set_credentials_permissions
end
|