soul_points 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ MySoulPoints API - Your morale, quantified.
2
+ ===========================================
3
+
4
+ Create your account at http://mysoulpoints.com
5
+
6
+ Code borrows heavily from the heroku gem. Thanks to the heroku team for that.
7
+
data/bin/soul_points ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require 'soul_points'
7
+
8
+ args = ARGV.dup
9
+ ARGV.clear
10
+ command = args.shift.strip rescue 'help'
11
+
12
+ client = SoulPoints::Client.new
13
+ client.run_command( command, args )
@@ -0,0 +1,118 @@
1
+ require 'rest_client'
2
+ require 'uri'
3
+ require 'time'
4
+ require 'soul_points/version'
5
+ require 'json'
6
+ require 'yaml'
7
+ require 'soul_points/helpers'
8
+
9
+ # A Ruby class to call the MySoulPoints REST API.
10
+ #
11
+ class SoulPoints::Client
12
+ include SoulPoints::Helpers
13
+
14
+ def self.version
15
+ SoulPoints::VERSION
16
+ end
17
+
18
+ def self.gem_version_string
19
+ "soul_points-gem/#{version}"
20
+ end
21
+
22
+ def self.api_enpoint
23
+ "http://mysoulpoints.com"
24
+ end
25
+
26
+ attr_accessor :host, :user, :password
27
+
28
+ def initialize
29
+ @credentials = {}
30
+ end
31
+
32
+ def run_command( command, args )
33
+ load_credentials if command != 'store_api_key' && command != 'help'
34
+ raise InvalidCommand unless self.respond_to?(command)
35
+ self.send(command, args)
36
+ end
37
+
38
+ def store_api_key( args )
39
+ @api_key = args[0]
40
+ @credentials = {
41
+ :api_key => @api_key
42
+ }
43
+ write_credentials
44
+ end
45
+
46
+ def help( args )
47
+ puts 'Usage:'
48
+ puts '$ soul_points show mcphat #Displays soul points for user mcphat'
49
+ end
50
+
51
+ # Show your current soul points
52
+ def show( args )
53
+ subdomain = args[0]
54
+ soul_points = JSON.parse( RestClient.get 'http://' + subdomain + '.mysoulpoints.com', :accept => :json )
55
+ puts soul_points['soul_point']['current'].to_s + '/' + soul_points['soul_point']['max'].to_s
56
+ end
57
+
58
+ def gain( args )
59
+ puts RestClient.post 'http://mysoulpoints.com/events', :event => { :value => args[0], :description => args[1] }, :auth_token => @credentials[:api_key], :accept => :json
60
+ end
61
+
62
+ # Pretty much just an alias for gain, with negative number
63
+ def lose( args )
64
+ args[0] = args[0].to_i * -1
65
+ gain( args )
66
+ end
67
+
68
+ def load_credentials
69
+ @credentials = read_credentials
70
+ if !@credentials || @credentials[:api_key].nil?
71
+ couldnt_find_credentials
72
+ exit
73
+ end
74
+ end
75
+
76
+ def couldnt_find_credentials
77
+ puts "Could not find your credentials. To save your credentials run: \n$ soul_points store_api_key YOUR_KEY_HERE\n"
78
+ end
79
+
80
+ def credentials_file
81
+ "#{home_directory}/.soul_points"
82
+ end
83
+
84
+ def read_credentials
85
+ YAML::load( File.read(credentials_file) ) if File.exists?(credentials_file)
86
+ end
87
+
88
+ def write_credentials
89
+ File.open(credentials_file, 'w') do |f|
90
+ f.puts @credentials.to_yaml
91
+ end
92
+ set_credentials_permissions
93
+ end
94
+
95
+ def set_credentials_permissions
96
+ FileUtils.chmod 0700, File.dirname(credentials_file)
97
+ FileUtils.chmod 0600, credentials_file
98
+ end
99
+
100
+ ##################
101
+
102
+ def soul_points_headers # :nodoc:
103
+ {
104
+ 'X-SoulPoints-API-Version' => '1',
105
+ 'User-Agent' => self.class.gem_version_string,
106
+ 'X-Ruby-Version' => RUBY_VERSION,
107
+ 'X-Ruby-Platform' => RUBY_PLATFORM
108
+ }
109
+ end
110
+
111
+ def escape(value) # :nodoc:
112
+ escaped = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
113
+ escaped.gsub('.', '%2E') # not covered by the previous URI.escape
114
+ end
115
+
116
+ private
117
+
118
+ end
@@ -0,0 +1,110 @@
1
+ module SoulPoints
2
+ module Helpers
3
+ def home_directory
4
+ running_on_windows? ? ENV['USERPROFILE'] : ENV['HOME']
5
+ end
6
+
7
+ def running_on_windows?
8
+ RUBY_PLATFORM =~ /mswin32|mingw32/
9
+ end
10
+
11
+ def running_on_a_mac?
12
+ RUBY_PLATFORM =~ /-darwin\d/
13
+ end
14
+
15
+ def display(msg, newline=true)
16
+ if newline
17
+ puts(msg)
18
+ else
19
+ print(msg)
20
+ STDOUT.flush
21
+ end
22
+ end
23
+
24
+ def redisplay(line, line_break = false)
25
+ display("\r\e[0K#{line}", line_break)
26
+ end
27
+
28
+ def deprecate(version)
29
+ display "!!! DEPRECATION WARNING: This command will be removed in version #{version}"
30
+ display ""
31
+ end
32
+
33
+ def error(msg)
34
+ STDERR.puts(msg)
35
+ exit 1
36
+ end
37
+
38
+ def confirm(message="Are you sure you wish to continue? (y/n)?")
39
+ display("#{message} ", false)
40
+ ask.downcase == 'y'
41
+ end
42
+
43
+ def confirm_command(app = app)
44
+ if extract_option('--force')
45
+ display("Warning: The --force switch is deprecated, and will be removed in a future release. Use --confirm #{app} instead.")
46
+ return true
47
+ end
48
+
49
+ raise(SoulPoints::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app
50
+
51
+ confirmed_app = extract_option('--confirm', false)
52
+ if confirmed_app
53
+ unless confirmed_app == app
54
+ raise(SoulPoints::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app}.")
55
+ end
56
+ return true
57
+ else
58
+ display ""
59
+ display " ! WARNING: Potentially Destructive Action"
60
+ display " ! This command will affect the app: #{app}"
61
+ display " ! To proceed, type \"#{app}\" or re-run this command with --confirm #{app}"
62
+ display ""
63
+ display "> ", false
64
+ if ask.downcase != app
65
+ display " ! Input did not match #{app}. Aborted."
66
+ false
67
+ else
68
+ true
69
+ end
70
+ end
71
+ end
72
+
73
+ def format_date(date)
74
+ date = Time.parse(date) if date.is_a?(String)
75
+ date.strftime("%Y-%m-%d %H:%M %Z")
76
+ end
77
+
78
+ def ask
79
+ gets.strip
80
+ end
81
+
82
+ def shell(cmd)
83
+ FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`}
84
+ end
85
+
86
+ def run_command(command, args=[])
87
+ SoulPoints::Command.run_internal(command, args)
88
+ end
89
+
90
+ def retry_on_exception(*exceptions)
91
+ retry_count = 0
92
+ begin
93
+ yield
94
+ rescue *exceptions => ex
95
+ raise ex if retry_count >= 3
96
+ sleep 3
97
+ retry_count += 1
98
+ retry
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ unless String.method_defined?(:shellescape)
105
+ class String
106
+ def shellescape
107
+ empty? ? "''" : gsub(/([^A-Za-z0-9_\-.,:\/@\n])/n, '\\\\\\1').gsub(/\n/, "'\n'")
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ module SoulPoints
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ module SoulPoints; end
2
+
3
+ require 'soul_points/client'
4
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soul_points
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Forge Apps
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-19 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rest-client
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: Client library and CLI to the mysoulpoints.com API.
39
+ email: support@mysoulpoints.com
40
+ executables:
41
+ - soul_points
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - bin/soul_points
48
+ - lib/soul_points/client.rb
49
+ - lib/soul_points/helpers.rb
50
+ - lib/soul_points/version.rb
51
+ - lib/soul_points.rb
52
+ - README.md
53
+ has_rdoc: true
54
+ homepage: http://mysoulpoints.com/
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.5.2
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Client library and CLI to the mysoulpoints.com API.
81
+ test_files: []
82
+