rest-core 0.4.0 → 0.7.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.
Files changed (72) hide show
  1. data/.travis.yml +2 -7
  2. data/CHANGES.md +13 -2
  3. data/Gemfile +0 -3
  4. data/README.md +30 -34
  5. data/Rakefile +5 -46
  6. data/lib/rest-core.rb +0 -6
  7. data/lib/rest-core/version.rb +1 -1
  8. data/rest-core.gemspec +5 -83
  9. metadata +11 -87
  10. data/example/rails2/Gemfile +0 -21
  11. data/example/rails2/README +0 -4
  12. data/example/rails2/Rakefile +0 -11
  13. data/example/rails2/app/controllers/application_controller.rb +0 -128
  14. data/example/rails2/app/views/application/helper.html.erb +0 -2
  15. data/example/rails2/config/boot.rb +0 -130
  16. data/example/rails2/config/environment.rb +0 -15
  17. data/example/rails2/config/environments/development.rb +0 -17
  18. data/example/rails2/config/environments/production.rb +0 -28
  19. data/example/rails2/config/environments/test.rb +0 -30
  20. data/example/rails2/config/initializers/cookie_verification_secret.rb +0 -7
  21. data/example/rails2/config/initializers/new_rails_defaults.rb +0 -21
  22. data/example/rails2/config/initializers/session_store.rb +0 -15
  23. data/example/rails2/config/preinitializer.rb +0 -23
  24. data/example/rails2/config/rest-core.yaml +0 -16
  25. data/example/rails2/config/routes.rb +0 -43
  26. data/example/rails2/log +0 -0
  27. data/example/rails2/test/functional/application_controller_test.rb +0 -197
  28. data/example/rails2/test/test_helper.rb +0 -18
  29. data/example/rails2/test/unit/rails_util_test.rb +0 -44
  30. data/example/rails3/Gemfile +0 -20
  31. data/example/rails3/README +0 -4
  32. data/example/rails3/Rakefile +0 -7
  33. data/example/rails3/app/controllers/application_controller.rb +0 -128
  34. data/example/rails3/app/views/application/helper.html.erb +0 -2
  35. data/example/rails3/config.ru +0 -4
  36. data/example/rails3/config/application.rb +0 -23
  37. data/example/rails3/config/environment.rb +0 -5
  38. data/example/rails3/config/environments/development.rb +0 -26
  39. data/example/rails3/config/environments/production.rb +0 -49
  40. data/example/rails3/config/environments/test.rb +0 -30
  41. data/example/rails3/config/initializers/secret_token.rb +0 -7
  42. data/example/rails3/config/initializers/session_store.rb +0 -8
  43. data/example/rails3/config/rest-core.yaml +0 -16
  44. data/example/rails3/config/routes.rb +0 -5
  45. data/example/rails3/test/functional/application_controller_test.rb +0 -197
  46. data/example/rails3/test/test_helper.rb +0 -18
  47. data/example/rails3/test/unit/rails_util_test.rb +0 -44
  48. data/example/sinatra/config.ru +0 -16
  49. data/lib/rest-core/client/facebook.rb +0 -251
  50. data/lib/rest-core/client/facebook/rails_util.rb +0 -333
  51. data/lib/rest-core/client/flurry.rb +0 -96
  52. data/lib/rest-core/client/flurry/rails_util.rb +0 -74
  53. data/lib/rest-core/client/github.rb +0 -18
  54. data/lib/rest-core/client/linkedin.rb +0 -59
  55. data/lib/rest-core/client/mixi.rb +0 -47
  56. data/lib/rest-core/client/twitter.rb +0 -101
  57. data/test/client/facebook/config/rest-core.yaml +0 -8
  58. data/test/client/facebook/test_api.rb +0 -97
  59. data/test/client/facebook/test_cache.rb +0 -58
  60. data/test/client/facebook/test_default.rb +0 -23
  61. data/test/client/facebook/test_error.rb +0 -65
  62. data/test/client/facebook/test_handler.rb +0 -84
  63. data/test/client/facebook/test_load_config.rb +0 -39
  64. data/test/client/facebook/test_misc.rb +0 -72
  65. data/test/client/facebook/test_oauth.rb +0 -38
  66. data/test/client/facebook/test_old.rb +0 -114
  67. data/test/client/facebook/test_page.rb +0 -106
  68. data/test/client/facebook/test_parse.rb +0 -128
  69. data/test/client/facebook/test_serialize.rb +0 -43
  70. data/test/client/facebook/test_timeout.rb +0 -22
  71. data/test/client/flurry/test_metrics.rb +0 -83
  72. data/test/client/twitter/test_api.rb +0 -37
@@ -1,96 +0,0 @@
1
-
2
- require 'rest-core'
3
-
4
- require 'time' # for Time.parse
5
-
6
- RestCore::Flurry = RestCore::Builder.client(:api_key, :access_code) do
7
- s = self.class # this is only for ruby 1.8!
8
- use s::Timeout , 10
9
-
10
- use s::DefaultSite , 'http://api.flurry.com/'
11
- use s::DefaultHeaders, {'Accept' => 'application/json'}
12
- use s::DefaultQuery , {}
13
-
14
- use s::CommonLogger , nil
15
- use s::Cache , nil, 600 do
16
- use s::ErrorHandler, lambda{|env| raise env[s::RESPONSE_BODY]['message']}
17
- use s::ErrorDetectorHttp
18
- use s::JsonDecode , true
19
- end
20
- end
21
-
22
- module RestCore::Flurry::Client
23
- # see: http://wiki.flurry.com/index.php?title=AppInfo
24
- # >> f.app_info
25
- # => {"@platform"=>"iPhone", "@name"=>"PicCollage",
26
- # "@createdDate"=>"2011-07-24", "@category"=>"Photography",
27
- # "@version"=>"1.0", "@generatedDate"=>"9/15/11 7:08 AM",
28
- # "version"=>[{"@name"=>"2.1", ...
29
- def app_info query={}
30
- get('appInfo/getApplication', query)
31
- end
32
-
33
- # see: http://wiki.flurry.com/index.php?title=EventMetrics
34
- # >> f.event_matrics(:startDate => '2011-09-14', :endDate => '2011-09-15')
35
- # => {"@type"=>"Summary", "@startDate"=>"2011-09-14",
36
- # "@endDate"=>"2011-09-15", "@version"=>"1.0",
37
- # "@generatedDate"=>"9/15/117:08 AM",
38
- # "event"=>[{"@usersLastWeek"=>"164", "@usersLastMonth"=>"642", ...
39
- def event_metrics query={}
40
- get('eventMetrics/Summary', query)
41
- end
42
-
43
- # see: http://wiki.flurry.com/index.php?title=AppMetrics
44
- # >> f.metrics('ActiveUsers', {}, :weeks => 4)
45
- # => [["2011-09-19", 6516], ["2011-09-18", 43920], ["2011-09-17", 45412],
46
- # ["2011-09-16", 40972], ["2011-09-15", 37587], ["2011-09-14", 34918],
47
- # ["2011-09-13", 35223], ["2011-09-12", 37750], ["2011-09-11", 45057],
48
- # ["2011-09-10", 44077], ["2011-09-09", 36683], ["2011-09-08", 34871],
49
- # ["2011-09-07", 35960], ["2011-09-06", 35829], ["2011-09-05", 37777],
50
- # ["2011-09-04", 40233], ["2011-09-03", 39306], ["2011-09-02", 33467],
51
- # ["2011-09-01", 31558], ["2011-08-31", 32096], ["2011-08-30", 34076],
52
- # ["2011-08-29", 34950], ["2011-08-28", 40456], ["2011-08-27", 41332],
53
- # ["2011-08-26", 37737], ["2011-08-25", 34392], ["2011-08-24", 33560],
54
- # ["2011-08-23", 34722]]
55
- def metrics path, query={}, opts={}
56
- if weeks = opts.delete(:weeks)
57
- query[:startDate] =
58
- (Time.now + 86400 - 86400*7*weeks).strftime('%Y-%m-%d')
59
- end
60
-
61
- query[:endDate] ||= Time.now.strftime('%Y-%m-%d')
62
-
63
- get("appMetrics/#{path}", query, opts)['day'].
64
- map{ |i| [i['@date'], i['@value'].to_i] }.reverse
65
- end
66
-
67
- # >> f.weekly(f.metrics('ActiveUsers', {}, :weeks => 4))
68
- # => [244548, 270227, 248513, 257149]
69
- def weekly array
70
- start = Time.parse(array.first.first, nil).to_i
71
- array.group_by{ |(date, value)|
72
- current = Time.parse(date, nil).to_i
73
- - (current - start) / (86400*7)
74
- # calling .last to discard week numbers created by group_by
75
- }.sort.map(&:last).map{ |week|
76
- week.map{ |(date, num)| num }.inject(&:+) }
77
- end
78
-
79
- # >> f.sum(f.weekly(f.metrics('ActiveUsers', {}, :weeks => 4)))
80
- # => [1020437, 775889, 505662, 257149]
81
- def sum array
82
- reverse = array.reverse
83
- (0...reverse.size).map{ |index|
84
- reverse[1, index].inject(reverse.first, &:+)
85
- }.reverse
86
- end
87
-
88
- def query
89
- {'apiKey' => api_key ,
90
- 'apiAccessCode' => access_code}
91
- end
92
- end
93
-
94
- RestCore::Flurry.send(:include, RestCore::Flurry::Client)
95
- require 'rest-core/client/flurry/rails_util' if
96
- Object.const_defined?(:Rails)
@@ -1,74 +0,0 @@
1
-
2
- require 'rest-core/util/rails_util_util'
3
-
4
- module RestCore::Flurry::DefaultAttributes
5
- def default_log_method ; Rails.logger.method(:debug); end
6
- def default_cache ; Rails.cache ; end
7
- def default_api_key ; nil ; end
8
- def default_access_code; nil ; end
9
- end
10
-
11
- module RestCore::Flurry::RailsUtil
12
- def self.init app=Rails
13
- RestCore::Config.load_for_rails(RestCore::Flurry, 'flurry', app)
14
- end
15
-
16
- module Helper
17
- def rc_flurry
18
- controller.send(:rc_flurry)
19
- end
20
- end
21
-
22
- def self.included controller
23
- # skip if included already, any better way to detect this?
24
- return if controller.respond_to?(:rc_flurry, true)
25
-
26
- controller.helper(RestCore::Flurry::RailsUtil::Helper)
27
- controller.instance_methods.select{ |method|
28
- method.to_s =~ /^rc_flurry/
29
- }.each{ |method| controller.send(:protected, method) }
30
- end
31
-
32
- def rc_flurry_setup options={}
33
- rc_flurry_options_ctl.merge!(
34
- RestCore::RailsUtilUtil.extract_options(
35
- RestCore::Flurry.members, options, :reject))
36
- rc_flurry_options_new.merge!(
37
- RestCore::RailsUtilUtil.extract_options(
38
- RestCore::Flurry.members, options, :select))
39
-
40
- # we'll need to reinitialize rc_flurry with the new options,
41
- # otherwise if you're calling rc_flurry before rc_flurry_setup,
42
- # you'll end up with default options without the ones you've passed
43
- # into rc_flurry_setup.
44
- rc_flurry.send(:initialize, rc_flurry_options_new)
45
-
46
- true # keep going
47
- end
48
-
49
- def rc_flurry
50
- @rc_flurry ||= RestCore::Flurry.new(rc_flurry_options_new)
51
- end
52
-
53
- module_function
54
-
55
- # ==================== begin options utility =======================
56
- def rc_flurry_oget key
57
- if rc_flurry_options_ctl.has_key?(key)
58
- rc_flurry_options_ctl[key]
59
- else
60
- RestCore::Flurry.send("default_#{key}")
61
- end
62
- end
63
-
64
- def rc_flurry_options_ctl
65
- @rc_flurry_options_ctl ||= {}
66
- end
67
-
68
- def rc_flurry_options_new
69
- @rc_flurry_options_new ||= {}
70
- end
71
- # ==================== end options utility =======================
72
- end
73
-
74
- RestCore::Flurry::RailsUtil.init(Rails)
@@ -1,18 +0,0 @@
1
-
2
- require 'rest-core'
3
-
4
- RestCore::Github = RestCore::Builder.client do
5
- s = self.class # this is only for ruby 1.8!
6
- use s::Timeout , 10
7
-
8
- use s::DefaultSite , 'https://api.github.com/'
9
- use s::DefaultHeaders, {'Accept' => 'application/json'}
10
- use s::Oauth2Query , 'access_token', nil
11
-
12
- use s::CommonLogger , nil
13
- use s::Cache , nil, 600 do
14
- use s::ErrorHandler, lambda{|env| raise env[s::RESPONSE_BODY]['message']}
15
- use s::ErrorDetectorHttp
16
- use s::JsonDecode , true
17
- end
18
- end
@@ -1,59 +0,0 @@
1
-
2
- require 'rest-core'
3
-
4
- RestCore::Linkedin = RestCore::Builder.client(:data) do
5
- s = self.class # this is only for ruby 1.8!
6
- use s::Timeout , 10
7
-
8
- use s::DefaultSite , 'https://api.linkedin.com/'
9
- use s::DefaultHeaders, {'Accept' => 'application/json'}
10
- use s::DefaultQuery , {'format' => 'json'}
11
-
12
- use s::Oauth1Header ,
13
- 'uas/oauth/requestToken', 'uas/oauth/accessToken',
14
- 'https://www.linkedin.com/uas/oauth/authorize'
15
-
16
- use s::CommonLogger , nil
17
- use s::Cache , nil, 600 do
18
- use s::ErrorHandler, lambda{|env|
19
- if (body = env[s::RESPONSE_BODY]).kind_of?(Hash)
20
- raise body['message']
21
- else
22
- raise body
23
- end
24
- }
25
- use s::ErrorDetectorHttp
26
- use s::JsonDecode , true
27
- end
28
-
29
- use s::Defaults , :data => lambda{{}}
30
- end
31
-
32
- module RestCore::Linkedin::Client
33
- include RestCore
34
-
35
- def oauth_token
36
- data['oauth_token'] if data.kind_of?(Hash)
37
- end
38
- def oauth_token= token
39
- data['oauth_token'] = token if data.kind_of?(Hash)
40
- end
41
- def oauth_token_secret
42
- data['oauth_token_secret'] if data.kind_of?(Hash)
43
- end
44
- def oauth_token_secret= secret
45
- data['oauth_token_secret'] = secret if data.kind_of?(Hash)
46
- end
47
-
48
- def me queries={}, opts={}
49
- get('v1/people/~', queries, opts)
50
- end
51
-
52
- private
53
- def set_token query
54
- self.data = query
55
- end
56
- end
57
-
58
- RestCore::Linkedin.send(:include, RestCore::ClientOauth1)
59
- RestCore::Linkedin.send(:include, RestCore::Linkedin::Client)
@@ -1,47 +0,0 @@
1
-
2
- RestCore::Mixi = RestCore::Builder.client(
3
- :data, :consumer_key, :consumer_secret, :redirect_uri) do
4
- s = self.class # this is only for ruby 1.8!
5
- use s::Timeout , 10
6
-
7
- use s::DefaultSite , 'http://api.mixi-platform.com/'
8
- use s::DefaultHeaders, {'Accept' => 'application/json'}
9
-
10
- use s::Oauth2Header , nil
11
-
12
- use s::CommonLogger , nil
13
- use s::Cache , nil, 600 do
14
- use s::ErrorHandler , lambda{ |env| p env }
15
- use s::ErrorDetectorHttp
16
- use s::JsonDecode , true
17
- end
18
-
19
- use s::Defaults , :data => lambda{{}}
20
- end
21
-
22
- module RestCore::Mixi::Client
23
- include RestCore
24
-
25
- def access_token
26
- data['access_token'] if data.kind_of?(Hash)
27
- end
28
-
29
- def authorize_url queries={}
30
- url('https://mixi.jp/connect_authorize.pl',
31
- {:client_id => consumer_key,
32
- :response_type => 'code',
33
- :scope => 'r_profile'}.merge(queries))
34
- end
35
-
36
- def authorize! code, payload={}, opts={}
37
- pl = {:client_id => consumer_key ,
38
- :client_secret => consumer_secret,
39
- :redirect_uri => redirect_uri ,
40
- :grant_type => 'authorization_code',
41
- :code => code}.merge(payload)
42
-
43
- self.data = post('https://secure.mixi-platform.com/2/token', pl, {}, opts)
44
- end
45
- end
46
-
47
- RestCore::Mixi.send(:include, RestCore::Mixi::Client)
@@ -1,101 +0,0 @@
1
-
2
- require 'rest-core'
3
-
4
- RestCore::Twitter = RestCore::Builder.client(:data) do
5
- s = self.class # this is only for ruby 1.8!
6
- use s::Timeout , 10
7
-
8
- use s::DefaultSite , 'https://api.twitter.com/'
9
- use s::DefaultHeaders, {'Accept' => 'application/json'}
10
-
11
- use s::Oauth1Header ,
12
- 'oauth/request_token', 'oauth/access_token', 'oauth/authorize'
13
-
14
- use s::CommonLogger , nil
15
- use s::Cache , nil, 600 do
16
- use s::ErrorHandler, lambda{ |env|
17
- raise ::RestCore::Twitter::Error.call(env) }
18
- use s::ErrorDetectorHttp
19
- use s::JsonDecode , true
20
- end
21
-
22
- use s::Defaults , :data => lambda{{}}
23
- end
24
-
25
- class RestCore::Twitter::Error < RestCore::Error
26
- include RestCore
27
- class ServerError < Twitter::Error; end
28
-
29
- class BadRequest < Twitter::Error; end
30
- class Unauthorized < Twitter::Error; end
31
- class Forbidden < Twitter::Error; end
32
- class NotFound < Twitter::Error; end
33
- class NotAcceptable < Twitter::Error; end
34
- class EnhanceYourCalm < Twitter::Error; end
35
-
36
- class InternalServerError < Twitter::Error::ServerError; end
37
- class BadGateway < Twitter::Error::ServerError; end
38
- class ServiceUnavailable < Twitter::Error::ServerError; end
39
-
40
- attr_reader :error, :url
41
- def initialize error, url=''
42
- @error, @url = error, url
43
- super("#{error.inspect} from #{url}")
44
- end
45
-
46
- def self.call env
47
- error, url = env[RESPONSE_BODY], Middleware.request_uri(env)
48
- return new(env[FAIL], url) unless error.kind_of?(Hash)
49
- case env[RESPONSE_STATUS]
50
- when 400; BadRequest
51
- when 401; Unauthorized
52
- when 403; Forbidden
53
- when 404; NotFound
54
- when 406; NotAcceptable
55
- when 420; EnhanceYourCalm
56
- when 500; InternalServerError
57
- when 502; BadGateway
58
- when 503; ServiceUnavailable
59
- else ; self
60
- end.new(error, url)
61
- end
62
- end
63
-
64
- module RestCore::Twitter::Client
65
- include RestCore
66
-
67
- def oauth_token
68
- data['oauth_token'] if data.kind_of?(Hash)
69
- end
70
- def oauth_token= token
71
- data['oauth_token'] = token if data.kind_of?(Hash)
72
- end
73
- def oauth_token_secret
74
- data['oauth_token_secret'] if data.kind_of?(Hash)
75
- end
76
- def oauth_token_secret= secret
77
- data['oauth_token_secret'] = secret if data.kind_of?(Hash)
78
- end
79
-
80
- def tweet status, media=nil, opts={}
81
- if media
82
- post('https://upload.twitter.com/1/statuses/update_with_media.json',
83
- {:status => status, 'media[]' => media},
84
- {}, opts)
85
- else
86
- post('1/statuses/update.json', {:status => status}, {}, opts)
87
- end
88
- end
89
-
90
- def statuses user, queries={}, opts={}
91
- get('1/statuses/user_timeline.json', {:id => user}.merge(queries), opts)
92
- end
93
-
94
- private
95
- def set_token query
96
- self.data = query
97
- end
98
- end
99
-
100
- RestCore::Twitter.send(:include, RestCore::ClientOauth1)
101
- RestCore::Twitter.send(:include, RestCore::Twitter::Client)
@@ -1,8 +0,0 @@
1
-
2
- test:
3
- facebook:
4
- app_id: 41829
5
- secret: <%= 'r41829'.reverse %>
6
- json_decode: false
7
- lang: zh-tw
8
- auto_authorize_scope: 'publish_stream'
@@ -1,97 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- should 'generate correct url' do
11
- TestHelper.normalize_url(
12
- RestCore::Facebook.new(:access_token => 'awesome').
13
- url('path', :query => 'str')).should.eq \
14
- 'https://graph.facebook.com/path?access_token=awesome&query=str'
15
- end
16
-
17
- should 'request to correct server' do
18
- stub_request(:get, 'http://nothing.godfat.org/me').with(
19
- :headers => {'Accept' => 'text/plain',
20
- 'Accept-Language' => 'zh-tw',
21
- 'Accept-Encoding' => 'gzip, deflate', # this is by ruby
22
- }.merge(RUBY_VERSION < '1.9.2' ?
23
- {} :
24
- {'User-Agent' => 'Ruby'})). # this is by ruby
25
- to_return(:body => '{"data": []}')
26
-
27
- RestCore::Facebook.new(:site => 'http://nothing.godfat.org/',
28
- :lang => 'zh-tw',
29
- :accept => 'text/plain').
30
- get('me').should.eq({'data' => []})
31
- end
32
-
33
- should 'pass custom headers' do
34
- stub_request(:get, 'http://example.com/').with(
35
- :headers => {'Accept' => 'application/json',
36
- 'Accept-Language' => 'en-us',
37
- 'Accept-Encoding' => 'gzip, deflate', # this is by ruby
38
- 'X-Forwarded-For' => '127.0.0.1',
39
- }.merge(RUBY_VERSION < '1.9.2' ?
40
- {} :
41
- {'User-Agent' => 'Ruby'})). # this is by ruby
42
- to_return(:body => '{"data": []}')
43
-
44
- RestCore::Facebook.new.get('http://example.com', {},
45
- {:headers => {'X-Forwarded-For' => '127.0.0.1'}} ).
46
- should.eq({'data' => []})
47
- end
48
-
49
- should 'post right' do
50
- stub_request(:post, 'https://graph.facebook.com/feed/me').
51
- with(:body => 'message=hi%20there').to_return(:body => 'ok')
52
-
53
- RestCore::Facebook.new(:json_decode => false).
54
- post('feed/me', :message => 'hi there').should == 'ok'
55
- end
56
-
57
- should 'use secret_access_token' do
58
- stub_request(:get,
59
- 'https://graph.facebook.com/me?access_token=1|2').
60
- to_return(:body => 'ok')
61
-
62
- rg = RestCore::Facebook.new(
63
- :json_decode => false, :access_token => 'wrong',
64
- :app_id => '1', :secret => '2')
65
- rg.get('me', {}, :secret => true).should.eq 'ok'
66
- rg.url('me', {}, :secret => true).should.eq \
67
- 'https://graph.facebook.com/me?access_token=1%7C2'
68
- rg.url('me', {}, :secret => true, :site => '/').should.eq \
69
- '/me?access_token=1%7C2'
70
- end
71
-
72
- should 'suppress auto-decode in an api call' do
73
- stub_request(:get, 'https://graph.facebook.com/woot').
74
- to_return(:body => 'bad json')
75
-
76
- rg = RestCore::Facebook.new(:json_decode => true)
77
- rg.get('woot', {}, :json_decode => false).should.eq 'bad json'
78
- rg.json_decode.should == true
79
- end
80
-
81
- should 'not raise exception when encountering error' do
82
- [500, 401, 402, 403].each{ |status|
83
- stub_request(:delete, 'https://graph.facebook.com/123').to_return(
84
- :body => '[]', :status => status)
85
-
86
- RestCore::Facebook.new.delete('123').should.eq []
87
- }
88
- end
89
-
90
- should 'convert query to string' do
91
- stub(o = Object.new).to_s{ 'i am mock' }
92
- stub_request(:get, "https://graph.facebook.com/search?q=i%20am%20mock").
93
- to_return(:body => 'ok')
94
- RestCore::Facebook.new(:json_decode => false).
95
- get('search', :q => o).should.eq 'ok'
96
- end
97
- end