lostinpatterns-dailymile-client 0.1.0 → 0.1.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.rdoc CHANGED
@@ -1,34 +1,20 @@
1
1
  = dailymile-client
2
2
 
3
- Example usage:
3
+ Before getting started:
4
4
 
5
- dailymile = DailymileClient.new(CONSUMER_KEY, CONSUMER_SECRET, :token => '', :secret => '')
6
-
7
- # get a specific entry
8
- dailymile.entry(1)
5
+ You'll want to register your app here:
9
6
 
10
- # get the public feed
11
- dailymile.entries :page => 2
12
-
13
- # get the user's entries
14
- dailymile.entries("ben")
7
+ http://www.dailymile.com/api/consumers/new
15
8
 
16
- # get friends of the given user
17
- dailymile.friends("ben")
9
+ Documentation:
18
10
 
19
- # get the info for a user
20
- dailymile.user "ben"
11
+ You can read the API documentation here:
21
12
 
22
- # post a workout
23
- dailymile.authenticated_user.post_workout("hill repeats. tough stuff!",
24
- :duration => 30.minutes, :type => 'running',
25
- :distance => { :value => 5.5, :units => 'miles' })
26
-
27
- # post a note
28
- dailymile.authenticated_user.post_workout "think today is a rest day!"
29
-
30
- # you and friends feed for the authenticated user
31
- dailymile.authenticated_user.entries
13
+ http://www.dailymile.com/api/documentation
14
+
15
+ You can view an example of using this client here:
16
+
17
+ http://www.dailymile.com/api/documentation/example
32
18
 
33
19
  == Note on Patches/Pull Requests
34
20
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{dailymile-client}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["lostinpatterns"]
9
- s.date = %q{2009-08-01}
9
+ s.date = %q{2009-08-02}
10
10
  s.description = %q{TODO: longer description of your gem}
11
11
  s.email = %q{blweiner@gmail.com}
12
12
  s.extra_rdoc_files = [
@@ -13,8 +13,22 @@ module Dailymile
13
13
  def initialize(consumer_key, consumer_secret, options = {})
14
14
  @consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
15
15
  :site => File.join(API_PROTOCOL, API_HOST))
16
- @access_token = OAuth::AccessToken.new(@consumer, options[:token], options[:secret])
17
16
  @authenticated_user = AuthenticatedUser.new(self)
17
+
18
+ if token = options[:token] and secret = options[:secret]
19
+ @access_token = OAuth::AccessToken.new(@consumer, token, secret)
20
+ end
21
+ end
22
+
23
+ # TODO: add timeouts
24
+ def get_request_token
25
+ @consumer.get_request_token
26
+ end
27
+
28
+ # TODO: add timeouts
29
+ def get_access_token(token, secret, verifier)
30
+ request_token = OAuth::RequestToken.new(@consumer, token, secret)
31
+ request_token.get_access_token(:oauth_verifier => verifier)
18
32
  end
19
33
 
20
34
  def entries(*args)
@@ -39,21 +53,28 @@ module Dailymile
39
53
 
40
54
  def post(path, params = {})
41
55
  set_format! path
42
- handle_response @access_token.post(path, params.to_json, DEFAULT_REQUEST_HEADERS)
56
+ handle_response access_token.post(path, params.to_json, DEFAULT_REQUEST_HEADERS)
43
57
  end
44
58
 
45
59
  def get(path, params = {})
46
60
  set_format! path
47
61
  path << params.to_query_string unless params.empty?
48
- handle_response @access_token.get(path)
62
+ handle_response access_token.get(path)
49
63
  end
50
64
 
51
65
  def delete(path)
52
- handle_response @access_token.delete(path)
66
+ handle_response access_token.delete(path)
53
67
  end
54
68
 
55
69
  private
56
70
 
71
+ def access_token
72
+ if @access_token.nil?
73
+ raise DailymileException, "You aren't authorized! Initialize client with access token and secret or ask for a request token."
74
+ end
75
+ @access_token
76
+ end
77
+
57
78
  def extract_options_from_args!(args)
58
79
  args.last.is_a?(Hash) ? args.pop : {}
59
80
  end
@@ -66,7 +87,7 @@ module Dailymile
66
87
 
67
88
  def handle_response(response)
68
89
  case response
69
- when Net::HTTPOK: JSON.parse(response.body)
90
+ when Net::HTTPOK: JSON.parse(response.body) rescue nil
70
91
  when Net::HTTPNotFound: raise NotFound
71
92
  when Net::HTTPUnauthorized: raise Unauthorized
72
93
  when Net::HTTPBadGateway: raise Unavailable
@@ -7,8 +7,7 @@ require File.dirname(__FILE__) + '/dailymile-client/authenticated_user'
7
7
 
8
8
  module Dailymile
9
9
 
10
- #API_HOST = 'api.dailymile.com'
11
- API_HOST = 'localhost:3000'
10
+ API_HOST = 'api.dailymile.com'
12
11
  API_PROTOCOL = 'http://'
13
12
 
14
13
  class DailymileException < StandardError; end
@@ -26,4 +25,58 @@ module Dailymile
26
25
  end
27
26
  end
28
27
 
28
+ end
29
+
30
+ # http://github.com/mojodna/oauth/commit/37fa79cc988f8eaaf1c6b966f6190b14476d95a1
31
+ # fix for oauth 0.3.5
32
+ class OAuth::Consumer
33
+
34
+ def create_http_request(http_method, path, *arguments)
35
+ http_method = http_method.to_sym
36
+
37
+ if [:post, :put].include?(http_method)
38
+ data = arguments.shift
39
+ data.reject! { |k,v| v.nil? } if data.is_a?(Hash)
40
+ end
41
+
42
+ headers = arguments.first.is_a?(Hash) ? arguments.shift : {}
43
+
44
+ case http_method
45
+ when :post
46
+ request = Net::HTTP::Post.new(path,headers)
47
+ request["Content-Length"] = 0 # Default to 0
48
+ when :put
49
+ request = Net::HTTP::Put.new(path,headers)
50
+ request["Content-Length"] = 0 # Default to 0
51
+ when :get
52
+ request = Net::HTTP::Get.new(path,headers)
53
+ when :delete
54
+ request = Net::HTTP::Delete.new(path,headers)
55
+ when :head
56
+ request = Net::HTTP::Head.new(path,headers)
57
+ else
58
+ raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
59
+ end
60
+
61
+ if data.is_a?(Hash)
62
+ request.set_form_data(data)
63
+ elsif data
64
+ if data.respond_to?(:read)
65
+ request.body_stream = data
66
+ if data.respond_to?(:length)
67
+ request["Content-Length"] = data.length
68
+ elsif data.respond_to?(:stat) && data.stat.respond_to?(:size)
69
+ request["Content-Length"] = data.stat.size
70
+ else
71
+ raise ArgumentError, "Don't know how to send a body_stream that doesn't respond to .length or .stat.size"
72
+ end
73
+ else
74
+ request.body = data.to_s
75
+ request["Content-Length"] = request.body.length
76
+ end
77
+ end
78
+
79
+ request
80
+ end
81
+
29
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lostinpatterns-dailymile-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - lostinpatterns
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-01 00:00:00 -07:00
12
+ date: 2009-08-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15