cronofy 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da5d96380934904435706ec98e7aa923abd05cc7
4
- data.tar.gz: 73720046c2544252bdec8d7f7d5d5af8d7b93f2a
3
+ metadata.gz: 1be096b17b4eb2b3422337a2d168b7f27559493c
4
+ data.tar.gz: e696a6175e17d0465912d7810342cfd1352b7191
5
5
  SHA512:
6
- metadata.gz: 884b2c8194a158011ee7f4c03218e1f28f75bab6c904d6e36b39ef7734a58082e6601a8ccbc6a02359c5cdbbc1e0d46a3c865ebb33934018b3e0d24a75fcd87c
7
- data.tar.gz: 96feb5c06ccad53deb93df2fc819fb4182857615776e7713484e45e230626c2f885258448941c912ed21296fed61ec69225808a4be741b3677dc7c92d63605fb
6
+ metadata.gz: 5ea577cd0076d2aa66cdf8c7ecd6791aa1d80ef216a51535d51a46b351b586ef7fcb4456ce8cb4be78c010f8bcf82f6b12239f82d2948131324c6ae746696f9b
7
+ data.tar.gz: 0071814777c83e679d6b704595b756c6aa709c4540430fff18a2b29caefcbbeca8309390931259fd9b5c5159fb1e82c34920e2f9c65aebe11ee6490919223d74
data/README.md CHANGED
@@ -24,9 +24,9 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- You have to register on cronofy website and create an application there. You will get a client id and client secret which you will have to pass to initializer. You can also pass a token that you will get later, or leave it blank in case if you don't have it now:
27
+ You have to register on cronofy website and create an application there. You will get a client id and client secret which you will have to pass to initializer. You can also pass a token and refresh token that you will get later, or leave it blank in case if you don't have it now:
28
28
  ```ruby
29
- cronofy = Cronofy.new('CLIENT_ID', 'CLIENT_SECRET', 'TOKEN')
29
+ cronofy = Cronofy::Client.new('CLIENT_ID', 'CLIENT_SECRET', 'TOKEN', 'REFRESH_TOKEN')
30
30
  ```
31
31
 
32
32
  Generate a link for a user to grant access for his calendars:
@@ -81,7 +81,7 @@ event_data = {
81
81
  description: "Meeting room"
82
82
  }
83
83
  }
84
- cronofy.create_or_update_event(calendar_id, event_data)
84
+ cronofy.upsert(calendar_id, event_data)
85
85
  ```
86
86
 
87
87
  To delete an event from user's calendar:
@@ -91,4 +91,6 @@ cronofy.delete_event(calendar_id, event_id)
91
91
 
92
92
  ## Links
93
93
 
94
+ * [API Docs](http://www.cronofy.com/developers/api)
94
95
  * [API mailing list](https://groups.google.com/d/forum/cronofy-api)
96
+
@@ -1,118 +1,26 @@
1
1
  require "cronofy/version"
2
+ require "cronofy/errors"
2
3
  require "cronofy/auth"
4
+ require "cronofy/client"
3
5
  require "cronofy/response_parser"
4
- require "cronofy/errors"
5
6
  require 'json'
6
7
 
7
8
  module Cronofy
8
- class Cronofy
9
-
10
- def initialize(client_id, client_secret, token=nil, refresh_token=nil)
11
- @auth = Auth.new(client_id, client_secret, token, refresh_token)
12
- end
13
-
14
- def access_token!
15
- raise CredentialsMissingError.new unless @auth.access_token
16
- @auth.access_token
17
- end
18
-
19
- # Public : Lists the calendars or the user across all of the calendar accounts
20
- # see http://www.cronofy.com/developers/api#calendars
21
- #
22
- # Returns Hash of calendars
23
- def list_calendars
24
- response = do_request { access_token!.get("/v1/calendars") }
25
- ResponseParser.new(response).parse_json
26
- end
27
-
28
- # Public : Creates or updates an existing event that matches the event_id, in the calendar
29
- # see: http://www.cronofy.com/developers/api#upsert-event
30
- # aliased as upsert_event
31
- #
32
- # calendar_id - String Cronofy ID for the the calendar to contain the event
33
- # event - Hash describing the event with symbolized keys.
34
- # :event_id String client identifier for event NOT Cronofy's
35
- # :summary String
36
- # :start Time
37
- # :end Time
38
- #
39
- # Returns nothing
40
- def create_or_update_event(calendar_id, event)
41
- body = event.dup
42
- body[:start] = event[:start].utc.iso8601
43
- body[:end] = event[:end].utc.iso8601
44
-
45
- headers = {
46
- 'Content-Type' => 'application/json'
47
- }
48
-
49
- do_request { access_token!.post("/v1/calendars/#{calendar_id}/events", { body: JSON.generate(body), headers: headers }) }
50
- end
51
- alias_method :upsert_event, :create_or_update_event
52
9
 
53
- # Public : Deletes an event from the specified calendar
54
- # see http://www.cronofy.com/developers/api#delete-event
55
- #
56
- # calendar_id - String Cronofy ID for the calendar containing the event
57
- # event_id - String client ID for the event
58
- #
59
- # Returns nothing
60
- def delete_event(calendar_id, event_id)
61
- params = { event_id: event_id }
62
-
63
- do_request { access_token!.delete("/v1/calendars/#{calendar_id}/events", { params: params }) }
64
- end
65
-
66
- # Public : Generate the authorization URL to send the user to in order to generate
67
- # and authorization code in order for an access_token to be issued
68
- # see http://www.cronofy.com/developers/api#authorization
69
- #
70
- # redirect_uri - String URI to return the user to once authorization process completed
71
- # scope - Array of scopes describing access required to the users calendars (default: all scopes)
72
- #
73
- # Returns String
74
- def user_auth_link(redirect_uri, scope=nil)
75
- @auth.user_auth_link(redirect_uri, scope)
76
- end
77
-
78
- # Public : Returns the access_token for a given code and redirect_uri pair
79
- # see http://www.cronofy.com/developers/api#token-issue
80
- #
81
- # code - String code returned to redirect_uri after authorization
82
- # redirect_uri - String URI returned to
83
- #
84
- # Returns Cronofy::Credentials
85
- def get_token_from_code(code, redirect_uri)
86
- @auth.get_token_from_code(code, redirect_uri)
87
- end
88
-
89
- # Public : Refreshes the access_token and periodically the refresh_token for authorization
90
- # see http://www.cronofy.com/developers/api#token-refresh
91
- #
92
- # Returns Cronofy::Credentials
93
- def refresh_access_token
94
- @auth.refresh!
95
- end
96
-
97
- private
10
+ def self.api_url
11
+ @api_url ||= (ENV['CRONOFY_API_URL'] || "https://api.cronofy.com")
12
+ end
98
13
 
99
- ERROR_MAP = {
100
- 401 => AuthenticationFailureError,
101
- 403 => AuthorizationFailureError,
102
- 404 => NotFoundError,
103
- 422 => InvalidRequestError,
104
- 429 => TooManyRequestsError
105
- }
14
+ def self.api_url=(value)
15
+ @api_url = value
16
+ end
106
17
 
107
- def do_request(&block)
108
- begin
109
- block.call
110
- rescue OAuth2::Error => e
111
- error_class = ERROR_MAP.fetch(e.response.status, UnknownError)
112
- raise error_class.new(e.response.headers['status'], e.response)
113
- end
114
- end
18
+ def self.app_url
19
+ @app_url ||= (ENV['CRONOFY_APP_URL'] || "https://app.cronofy.com")
20
+ end
115
21
 
22
+ def self.app_url=(value)
23
+ @app_url = value
116
24
  end
117
25
 
118
26
  end
@@ -2,9 +2,6 @@ require "oauth2"
2
2
 
3
3
  module Cronofy
4
4
  class Auth
5
- APP_URL = 'https://app.cronofy.com'
6
- API_URL = 'https://api.cronofy.com'
7
-
8
5
  class Credentials
9
6
 
10
7
  attr_reader :access_token,
@@ -32,8 +29,8 @@ module Cronofy
32
29
  attr_reader :access_token
33
30
 
34
31
  def initialize(client_id, client_secret, token=nil, refresh_token=nil)
35
- @auth_client = OAuth2::Client.new(client_id, client_secret, site: APP_URL)
36
- @api_client = OAuth2::Client.new(client_id, client_secret, site: API_URL)
32
+ @auth_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.app_url)
33
+ @api_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.api_url)
37
34
 
38
35
  set_access_token(token, refresh_token) if token
39
36
  end
@@ -0,0 +1,119 @@
1
+ module Cronofy
2
+
3
+ class Client
4
+
5
+ def initialize(client_id, client_secret, token=nil, refresh_token=nil)
6
+ @auth = Auth.new(client_id, client_secret, token, refresh_token)
7
+ end
8
+
9
+ def access_token!
10
+ raise CredentialsMissingError.new unless @auth.access_token
11
+ @auth.access_token
12
+ end
13
+
14
+ # Public : Lists the calendars or the user across all of the calendar accounts
15
+ # see http://www.cronofy.com/developers/api#calendars
16
+ #
17
+ # Returns Hash of calendars
18
+ def list_calendars
19
+ response = do_request { access_token!.get("/v1/calendars") }
20
+ ResponseParser.new(response).parse_json
21
+ end
22
+
23
+ # Public : Creates or updates an existing event that matches the event_id, in the calendar
24
+ # see: http://www.cronofy.com/developers/api#upsert-event
25
+ # aliased as upsert_event
26
+ #
27
+ # calendar_id - String Cronofy ID for the the calendar to contain the event
28
+ # event - Hash describing the event with symbolized keys.
29
+ # :event_id String client identifier for event NOT Cronofy's
30
+ # :summary String
31
+ # :start Time
32
+ # :end Time
33
+ #
34
+ # Returns nothing
35
+ def create_or_update_event(calendar_id, event)
36
+ body = event.dup
37
+ body[:start] = event[:start].utc.iso8601
38
+ body[:end] = event[:end].utc.iso8601
39
+
40
+ headers = {
41
+ 'Content-Type' => 'application/json'
42
+ }
43
+
44
+ do_request { access_token!.post("/v1/calendars/#{calendar_id}/events", { body: JSON.generate(body), headers: headers }) }
45
+ end
46
+ alias_method :upsert_event, :create_or_update_event
47
+
48
+ # Public : Deletes an event from the specified calendar
49
+ # see http://www.cronofy.com/developers/api#delete-event
50
+ #
51
+ # calendar_id - String Cronofy ID for the calendar containing the event
52
+ # event_id - String client ID for the event
53
+ #
54
+ # Returns nothing
55
+ def delete_event(calendar_id, event_id)
56
+ params = { event_id: event_id }
57
+
58
+ do_request { access_token!.delete("/v1/calendars/#{calendar_id}/events", { params: params }) }
59
+ end
60
+
61
+ # Public : Generate the authorization URL to send the user to in order to generate
62
+ # and authorization code in order for an access_token to be issued
63
+ # see http://www.cronofy.com/developers/api#authorization
64
+ #
65
+ # redirect_uri - String URI to return the user to once authorization process completed
66
+ # scope - Array of scopes describing access required to the users calendars (default: all scopes)
67
+ #
68
+ # Returns String
69
+ def user_auth_link(redirect_uri, scope=nil)
70
+ @auth.user_auth_link(redirect_uri, scope)
71
+ end
72
+
73
+ # Public : Returns the access_token for a given code and redirect_uri pair
74
+ # see http://www.cronofy.com/developers/api#token-issue
75
+ #
76
+ # code - String code returned to redirect_uri after authorization
77
+ # redirect_uri - String URI returned to
78
+ #
79
+ # Returns Cronofy::Credentials
80
+ def get_token_from_code(code, redirect_uri)
81
+ @auth.get_token_from_code(code, redirect_uri)
82
+ end
83
+
84
+ # Public : Refreshes the access_token and periodically the refresh_token for authorization
85
+ # see http://www.cronofy.com/developers/api#token-refresh
86
+ #
87
+ # Returns Cronofy::Credentials
88
+ def refresh_access_token
89
+ @auth.refresh!
90
+ end
91
+
92
+ private
93
+
94
+ ERROR_MAP = {
95
+ 401 => ::Cronofy::AuthenticationFailureError,
96
+ 403 => ::Cronofy::AuthorizationFailureError,
97
+ 404 => ::Cronofy::NotFoundError,
98
+ 422 => ::Cronofy::InvalidRequestError,
99
+ 429 => ::Cronofy::TooManyRequestsError
100
+ }
101
+
102
+ def do_request(&block)
103
+ begin
104
+ block.call
105
+ rescue OAuth2::Error => e
106
+ error_class = ERROR_MAP.fetch(e.response.status, UnknownError)
107
+ raise error_class.new(e.response.headers['status'], e.response)
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ # Alias for backwards compatibility
114
+ # depcrectated will be removed
115
+ class Cronofy < Client
116
+
117
+ end
118
+
119
+ end
@@ -1,3 +1,3 @@
1
1
  module Cronofy
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cronofy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergii Paryzhskyi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-24 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,7 @@ files:
82
82
  - cronofy.gemspec
83
83
  - lib/cronofy.rb
84
84
  - lib/cronofy/auth.rb
85
+ - lib/cronofy/client.rb
85
86
  - lib/cronofy/errors.rb
86
87
  - lib/cronofy/response_parser.rb
87
88
  - lib/cronofy/version.rb