dailymile-ruby 0.1.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 lostinpatterns
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = dailymile-ruby
2
+
3
+ dailymile ruby client
4
+
5
+ This gem wraps the dailymile API that's available at http://www.dailymile.com/api
6
+
7
+ == Getting started
8
+
9
+ The best way to install dailymile-ruby is via RubyGems:
10
+
11
+ sudo gem install dailymile-ruby
12
+
13
+ Get entries nearby a given latitude and longitude:
14
+
15
+ require 'dailymile'
16
+ client = Dailymile::Client.new
17
+ client.entries :nearby, 37.77916, -122.420049
18
+
19
+ To access protected resources you'll need to use OAuth 2.
20
+
21
+ 1. Register your client at http://www.dailymile.com/api/consumers/new
22
+ 2. Get an access token on behalf of a user, see instructions here http://www.dailymile.com/api/documentation/oauth
23
+
24
+ Using your access token:
25
+
26
+ require 'dailymile'
27
+ Dailymile::Client.set_client_credentials CLIENT_ID, CLIENT_SECRET
28
+ client = Dailymile::Client.new ACCESS_TOKEN
29
+ current_user = client.get '/people/me'
30
+
31
+ == Note on Patches/Pull Requests
32
+
33
+ * Fork the project.
34
+ * Make your feature addition or bug fix.
35
+ * Add tests for it. This is important so I don't break it in a
36
+ future version unintentionally.
37
+ * Commit, do not mess with rakefile, version, or history.
38
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2010 lostinpatterns. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dailymile-ruby"
8
+ gem.summary = %Q{dailymile ruby client}
9
+ gem.description = %Q{This gem wraps the dailymile API that's available at http://www.dailymile.com/api}
10
+ gem.email = "blweiner@gmail.com"
11
+ gem.homepage = "http://github.com/dailymile/dailymile-ruby"
12
+ gem.authors = ["lostinpatterns"]
13
+
14
+ gem.add_dependency("oauth2", "0.0.13")
15
+ gem.add_dependency("json_pure")
16
+
17
+ # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/test_*.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "dailymile-ruby #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dailymile-ruby}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["lostinpatterns"]
12
+ s.date = %q{2010-10-11}
13
+ s.description = %q{This gem wraps the dailymile API that's available at http://www.dailymile.com/api}
14
+ s.email = %q{blweiner@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "dailymile-ruby.gemspec",
27
+ "lib/dailymile.rb",
28
+ "lib/dailymile/client.rb",
29
+ "lib/dailymile/token.rb",
30
+ "lib/oauth2_monkey_patches.rb",
31
+ "test/helper.rb",
32
+ "test/test_dailymile-ruby.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/dailymile/dailymile-ruby}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{dailymile ruby client}
39
+ s.test_files = [
40
+ "test/helper.rb",
41
+ "test/test_dailymile-ruby.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<oauth2>, ["= 0.0.13"])
50
+ s.add_runtime_dependency(%q<json_pure>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<oauth2>, ["= 0.0.13"])
53
+ s.add_dependency(%q<json_pure>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<oauth2>, ["= 0.0.13"])
57
+ s.add_dependency(%q<json_pure>, [">= 0"])
58
+ end
59
+ end
60
+
data/lib/dailymile.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'oauth2'
2
+ require 'json'
3
+
4
+ require 'oauth2_monkey_patches'
5
+ require 'dailymile/token'
6
+ require 'dailymile/client'
7
+
8
+ module Dailymile
9
+
10
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
11
+ BASE_URI = 'https://api.dailymile.com'
12
+ OAUTH_AUTHORIZE_PATH = '/oauth/authorize'
13
+ OAUTH_TOKEN_PATH = '/oauth/token'
14
+
15
+ class DailymileError < StandardError; end
16
+ class NotFound < DailymileError; end
17
+ class Unauthorized < DailymileError; end
18
+ class Forbidden < DailymileError; end
19
+ class RateLimitExceeded < DailymileError; end
20
+ class Unavailable < DailymileError; end
21
+ class UnprocessableEntity < DailymileError
22
+ attr_reader :errors
23
+
24
+ def initialize(errors)
25
+ @errors = errors
26
+ super
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,66 @@
1
+ require 'forwardable'
2
+
3
+ module Dailymile
4
+
5
+ DEFAULT_FORMAT = 'json'
6
+ STREAM_FILTERS = %w(nearby popular)
7
+
8
+ class Client
9
+ extend Forwardable
10
+
11
+ attr_reader :access_token
12
+ def_delegators :access_token, :get, :post, :put, :delete
13
+
14
+ def self.set_client_credentials(client_id, client_secret)
15
+ @@client = OAuth2::Client.new(client_id, client_secret,
16
+ :site => BASE_URI,
17
+ :access_token_path => OAUTH_TOKEN_PATH,
18
+ :authorize_path => OAUTH_AUTHORIZE_PATH
19
+ )
20
+ end
21
+
22
+ def initialize(token = nil)
23
+ @@client ||= OAuth2::Client.new('', '', :site => BASE_URI) # HACK: dummy client
24
+
25
+ @access_token = Token.new(@@client, token)
26
+ end
27
+
28
+ # EXAMPLES:
29
+ # everyone stream: client.entries
30
+ # nearby stream: client.entries :nearby, 37.77916, -122.420049, :page => 2
31
+ # ben's stream: client.entries :ben, :page => 2
32
+ def entries(*args)
33
+ params = extract_options_from_args!(args)
34
+ filter = args.shift
35
+
36
+ entries_path = case filter
37
+ when String, Symbol
38
+ filter = filter.to_s.strip
39
+
40
+ if STREAM_FILTERS.include?(filter)
41
+ if filter == 'nearby'
42
+ lat, lon = args
43
+ "/entries/nearby/#{lat},#{lon}"
44
+ else
45
+ "/entries/#{filter}"
46
+ end
47
+ else
48
+ "/people/#{filter}/entries"
49
+ end
50
+ else
51
+ '/entries'
52
+ end
53
+
54
+ data = get entries_path, params
55
+ data["entries"]
56
+ end
57
+
58
+ private
59
+
60
+ def extract_options_from_args!(args)
61
+ args.last.is_a?(Hash) ? args.pop : {}
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,89 @@
1
+ module Dailymile
2
+
3
+ class Token
4
+
5
+ def initialize(client, token)
6
+ @client = client
7
+ set_access_token token
8
+ end
9
+
10
+ def access_token=(token)
11
+ set_access_token token
12
+ end
13
+
14
+ def get(path, params = {})
15
+ request :get, path, params
16
+ end
17
+
18
+ def post(path, params = {})
19
+ request :post, path, params
20
+ end
21
+
22
+ def put(path, params = {})
23
+ request :put, path, params
24
+ end
25
+
26
+ def delete(path)
27
+ request :delete, path
28
+ end
29
+
30
+ private
31
+
32
+ def request(verb, path, params = {}, headers = {})
33
+ path << ".#{DEFAULT_FORMAT}" unless path =~ /.+\.\w+$/
34
+ path << to_query_string(params) unless params.empty?
35
+
36
+ response = @access_token.request(verb, path, {}, default_headers.merge(headers))
37
+ handle_response(response)
38
+ end
39
+
40
+ def handle_response(response)
41
+ case response.status
42
+ when 200, 201
43
+ JSON.parse(response.body) rescue nil
44
+ when 404
45
+ raise NotFound
46
+ when 401
47
+ raise Unauthorized
48
+ when 403
49
+ raise Forbidden, "Not using HTTP over TLS"
50
+ when 503, 502
51
+ if response.status == 503 && response['Retry-After']
52
+ raise RateLimitExceeded
53
+ else
54
+ raise Unavailable, "#{response.status}: #{response.body}"
55
+ end
56
+ when 422
57
+ errors = JSON.parse(response.body) rescue nil
58
+ raise UnprocessableEntity, errors
59
+ else
60
+ raise DailymileError,"#{response.status}: #{response.body}"
61
+ end
62
+ end
63
+
64
+ def to_query_string(params = {})
65
+ '?' + params.map do |k, v|
66
+ if v.instance_of?(Hash)
67
+ v.map do |sk, sv|
68
+ ["#{k}[#{sk}]", URI.escape(sv.to_s)].join('=')
69
+ end.join('&')
70
+ else
71
+ [k.to_s, URI.escape(v.to_s)].join('=')
72
+ end
73
+ end.join('&')
74
+ end
75
+
76
+ def default_headers
77
+ {
78
+ 'Accept' => 'application/json',
79
+ 'User-Agent' => "dailymile-ruby/#{VERSION}"
80
+ }
81
+ end
82
+
83
+ def set_access_token(token)
84
+ @access_token = OAuth2::AccessToken.new(@client, token)
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,22 @@
1
+ #
2
+ # HACK: monkey-patching oauth2 lib
3
+ #
4
+ module OAuth2
5
+ class AccessToken
6
+ def request(verb, path, params = {}, headers = {})
7
+ params = params.merge('oauth_token' => @token) unless @token.nil?
8
+ @client.request(verb, path, params)
9
+ end
10
+ end
11
+ class Client
12
+ def request(verb, url, params = {}, headers = {})
13
+ if verb == :get
14
+ connection.run_request(verb, url, nil, headers) do |req|
15
+ req.params.update(params)
16
+ end
17
+ else
18
+ connection.run_request(verb, url, params, headers)
19
+ end
20
+ end
21
+ end
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'dailymile'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestDailymileRuby < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dailymile-ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - lostinpatterns
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-11 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: oauth2
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 0
32
+ - 0
33
+ - 13
34
+ version: 0.0.13
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: json_pure
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: This gem wraps the dailymile API that's available at http://www.dailymile.com/api
52
+ email: blweiner@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - VERSION
67
+ - dailymile-ruby.gemspec
68
+ - lib/dailymile.rb
69
+ - lib/dailymile/client.rb
70
+ - lib/dailymile/token.rb
71
+ - lib/oauth2_monkey_patches.rb
72
+ - test/helper.rb
73
+ - test/test_dailymile-ruby.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/dailymile/dailymile-ruby
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: dailymile ruby client
108
+ test_files:
109
+ - test/helper.rb
110
+ - test/test_dailymile-ruby.rb