glass-rails 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f0c58157a2668abbda0417296fcf8ac4f57a3a4
4
- data.tar.gz: b7859dc9b8804a5c7c2f95a0fe27eb8cf236d852
3
+ metadata.gz: 04e2a6397d54f83f18ca0038cf0512ac36d5516c
4
+ data.tar.gz: 9294c8db7bf53009e9063a7907fce9522f434268
5
5
  SHA512:
6
- metadata.gz: 6c51db88dae76a80f11a2ef3fcce707e8944f27ea615111872d939449ac09aa276175f21fc45514acc8bbc51ba13fc0aad28d42c961a67a74565e8917f7b4825
7
- data.tar.gz: 92de4b4ca3572f44bc297ee6cb8a4f6063b2aa530d116b4a6b34af255b0cd490db6381bfeb3e656ec40dba835207f014897b859b02882ab8fe7633fb8c485cc5
6
+ metadata.gz: 16c3cce5db9c51886b515ecf47b2d2603c9c821d42f83f3e486258c5f921b5553b77e0ad94a979e9efd03040dad0a5490ee13804c146bde04070793befc99605
7
+ data.tar.gz: 6c02d1425142c574d5d18c88fb477d4a56cedbe682cda18f19f5cf0cc84aabd5d8072e7459d2d8295a7a2be6f35ff104cd0317f1948c5d23560816af9cd1bb25
data/.gitignore CHANGED
@@ -5,6 +5,8 @@
5
5
  .config
6
6
  .yardoc
7
7
  .rspec
8
+ .ruby-version
9
+ .ruby-gemset
8
10
  Gemfile.lock
9
11
  InstalledFiles
10
12
  _yardoc
data/README.md CHANGED
@@ -34,6 +34,23 @@ tables.
34
34
  Additionally, the install generator will create a set of html.erb templates for you to use (optionally) when creating timeline items to post to a user. The templates will be created under the directory
35
35
  `app/views/glass` though you may want to organize them more logically, depending on your use case.
36
36
 
37
+ ### API Key Configuration
38
+
39
+ The glass install generator will automatically generate a yaml file for you in the correct format for storing your client id and client secret, for each respective environment. You can optionally bypass loading your API Keys from
40
+ a YAML file by utilizing the generated initializer, which allows you to set custom configurations for your application.
41
+
42
+ If you want to load your keys, for example, from your ENV variables, you can do so like this:
43
+
44
+ ```ruby
45
+ Glass.setup do |config|
46
+
47
+ ## blah blah some config stuff..
48
+
49
+
50
+ config.client_id = ENV["GOOGLE_CLIENT_ID"]
51
+ config.client_secret = ENV["GOOGLE_CLIENT_SECRET"]
52
+ end
53
+ ```
37
54
  ### Glass Models - (Introduction)
38
55
 
39
56
  This gem also provides a glass model generator, which generates a timeline-item
@@ -196,12 +213,12 @@ mirror api for insertion into the timeline.
196
213
  Then all you have to do is use the following command to actually insert the content:
197
214
 
198
215
  ```ruby
199
- gt.insert()
216
+ gt.mirror_insert()
200
217
  ```
201
218
 
202
219
  Additionally, if you would like to merge in attributes into the hash before it is
203
220
  posted to the mirror-api, then you can optionally pass in a hash of attributes
204
- to the insert() method call, and the hash will be merged into the body_object that
221
+ to the mirror_insert() method call, and the hash will be merged into the body_object that
205
222
  is posted to the mirror api.
206
223
 
207
224
  This last point is relevant, for example, in the case of reading aloud text.
@@ -209,7 +226,7 @@ To do so, you need to specify a speakableText: "some text to be read aloud" attr
209
226
  in the body object when it gets posted. You can therefore do something like this:
210
227
 
211
228
  ```ruby
212
- gt.insert(speakableText: "some text to be read aloud")
229
+ gt.mirror_insert(speakableText: "some text to be read aloud")
213
230
  ```
214
231
  ## License
215
232
 
@@ -9,13 +9,27 @@ Rails.application.routes.default_url_options[:host] = 'localhost:3000' if Rails.
9
9
  ## Your configuration details here:
10
10
 
11
11
  Glass.setup do |config|
12
- ## modify this to change the name of your glass app
12
+
13
+ # modify this to change the name of your glass app
13
14
  ## config.application_name = "SomeGlassApp"
14
15
 
15
- ## modify this to change the application version of your glass app
16
+
17
+
18
+ # modify this to change the application version of your glass app
16
19
  ## config.application_version = "0.0.1"
17
20
 
18
21
 
22
+
23
+ # if you don't want to use a yaml file to load your api
24
+ # keys, then you can use these config vars to force load
25
+ # your keys from enviroment variables, or whatever method
26
+ # you might choose.
27
+ #
28
+ ## config.client_id = ENV["GOOGLE_CLIENT_ID"]
29
+ ## config.client_secret = ENV["GOOGLE_CLIENT_SECRET"]
30
+
31
+
32
+
19
33
  ## manually override your glass views path here.
20
34
  config.glass_template_path = "app/views/glass"
21
35
  end
data/lib/glass.rb CHANGED
@@ -15,10 +15,18 @@ module Glass
15
15
  mattr_accessor :glass_template_path
16
16
  @@glass_template_path = "app/views/glass"
17
17
 
18
+ mattr_accessor :_api_keys
19
+ @@_api_keys = nil
18
20
 
21
+ mattr_accessor :client_id
22
+
23
+ mattr_accessor :client_secret
24
+
19
25
  ## devise trick
20
26
  def self.setup
21
27
  yield self
28
+ require "glass/api_keys"
29
+ Glass::ApiKeys.generate!
22
30
  end
31
+ end
23
32
 
24
- end
@@ -1,29 +1,68 @@
1
1
  require "yaml"
2
+ require "pry"
2
3
  module Glass
3
4
  ## just a small class to organize the api key logic info
4
5
  ## from the yml file in config.
5
6
  class ApiKeys
6
7
  class APIKeyConfigurationError < StandardError; end;
7
- attr_accessor :client_id, :client_secret, :google_api_keys
8
+ class YAMLDoesNotHaveTheRightKeysForExtraction < StandardError; end;
9
+ attr_accessor :client_id, :client_secret, :google_api_keys, :yaml_config, :yaml_file_contents, :keys
8
10
  def initialize
9
- self.google_api_keys = load_yaml_file
11
+ self.google_api_keys = {}
12
+ preload_yaml_file_keys
13
+ load_google_keys_from_whereever
10
14
  load_keys
11
- self
15
+ return self.keys
16
+ end
17
+ def self.generate!
18
+ keys_instance = self.new
19
+ keys_instance.keys
20
+ ::Glass.client_secret = keys_instance.keys.client_secret
21
+ ::Glass.client_id = keys_instance.keys.client_id
22
+ ::Glass._api_keys = keys_instance.keys
23
+ end
24
+ private
25
+ def yaml_file_path
26
+ "#{Dir.pwd}/config/google-api-keys.yml"
12
27
  end
13
- private
14
28
  def load_keys
15
- if google_api_keys["client_id"].nil? or google_api_keys["client_secret"].nil?
16
- raise APIKeyConfigurationError
17
- else
18
- set_client_keys
29
+ google_api_keys_are_empty? ? (raise APIKeyConfigurationError) : set_client_keys
30
+ end
31
+ def yaml_config_hash
32
+ return self.yaml_file_contents if self.yaml_file_contents
33
+ self.yaml_file_contents = {}
34
+ self.yaml_file_contents = ::YAML.load(File.read(yaml_file_path)) if File.exists?(yaml_file_path)
35
+ self.yaml_file_contents
36
+ end
37
+ def preload_yaml_file_keys
38
+ self.yaml_config = {}
39
+ if yaml_config_hash.has_key?(::Rails.env)
40
+ self.yaml_config = yaml_config_hash[::Rails.env]
19
41
  end
20
42
  end
21
- def load_yaml_file
22
- ::YAML.load(File.read("#{::Rails.root}/config/google-api-keys.yml"))[::Rails.env]
43
+
44
+ def load_google_keys_from_whereever
45
+ [:client_id, :client_secret].each do |key|
46
+ google_api_keys[key] = ::Glass.send(key)
47
+ unless google_api_keys[key]
48
+ load_yaml_as_auxiliary_method(key)
49
+ end
50
+ end
51
+ end
52
+
53
+ def load_yaml_as_auxiliary_method(key)
54
+ if yaml_config.has_key?(key.to_s)
55
+ google_api_keys[key] = yaml_config[key.to_s]
56
+ else
57
+ raise YAMLDoesNotHaveTheRightKeysForExtraction
58
+ end
23
59
  end
24
60
  def set_client_keys
25
- self.client_id = google_api_keys["client_id"]
26
- self.client_secret = google_api_keys["client_secret"]
61
+ keys_struct = Struct.new(:client_id, :client_secret)
62
+ self.keys = keys_struct.new(google_api_keys[:client_id], google_api_keys[:client_secret])
63
+ end
64
+ def google_api_keys_are_empty?
65
+ google_api_keys[:client_id].nil? or google_api_keys[:client_secret].nil?
27
66
  end
28
67
  end
29
- end
68
+ end
data/lib/glass/client.rb CHANGED
@@ -1,4 +1,3 @@
1
- require "glass/api_keys"
2
1
  require 'active_support/core_ext/hash/indifferent_access'
3
2
  require "google/api_client"
4
3
 
@@ -16,36 +15,18 @@ module Glass
16
15
  end
17
16
 
18
17
  def initialize(opts)
19
- self.api_keys = opts[:api_keys] || ::Glass::ApiKeys.new
20
- initialize_google_api_client
21
- self.mirror_api = google_client.discovered_api("mirror", "v1")
18
+ setup_google_api_keys
19
+ initialize_google_client
22
20
  self.google_account = opts[:google_account]
23
-
24
- ### this isn't functional yet but this is an idea for
25
- ### an api for those who wish to opt out of passing in a
26
- ### google account, by passing in a hash of options
27
- ###
28
- ###
29
- ### the tricky aspect of this is how to handle the update
30
- ### of the token information if the token is expired.
31
-
32
- self.access_token = opts[:access_token] || google_account.try(:token)
33
- self.refresh_token = opts[:refresh_token] || google_account.try(:refresh_token)
34
- self.has_expired_token = opts[:has_expired_token] || google_account.has_expired_token?
35
-
36
21
  setup_with_our_access_tokens
37
22
  setup_with_user_access_token
38
23
  self
39
24
  end
40
- def initialize_google_api_client
41
- application_name = ::Glass.application_name
42
- application_version = ::Glass.application_version
43
- self.google_client = ::Google::APIClient.new(application_name: application_name, application_version: application_version)
44
- end
45
25
 
46
26
  def get_timeline_item(id)
47
27
  response_hash(self.google_client.execute(get_timeline_item_parameters(id)).response)
48
28
  end
29
+
49
30
  def get_timeline_item_parameters(id)
50
31
  { api_method: self.mirror_api.timeline.get,
51
32
  parameters: {
@@ -59,7 +40,6 @@ module Glass
59
40
  self
60
41
  end
61
42
 
62
-
63
43
  def json_content(options, api_method="insert")
64
44
  if c = options[:content]
65
45
  data = c.is_a?(String) ? {text: c} : c
@@ -82,6 +62,7 @@ module Glass
82
62
  inserting_content = { api_method: mirror_api.timeline.send(action),
83
63
  body_object: body_object}
84
64
  end
65
+
85
66
  def get(id)
86
67
  self.google_client.execute(get_timeline_item_parameters(id))
87
68
  end
@@ -90,7 +71,6 @@ module Glass
90
71
  google_client.execute(rest_action(options, "insert"))
91
72
  end
92
73
 
93
-
94
74
  def patch(options={})
95
75
  glass_item_id = options.delete(:glass_item_id)
96
76
  patch_action = rest_action(options, "patch").merge(parameters: {id: glass_item_id})
@@ -106,15 +86,12 @@ module Glass
106
86
  google_client.execute update_content
107
87
  end
108
88
 
109
-
110
-
111
89
  ## deprecated: please use cached_list instead
112
90
  def timeline_list(opts={as_hash: true})
113
91
  puts "DEPRECATION WARNING: timeline_list is now deprecated, please use cached_list instead"
114
92
  cached_list
115
93
  end
116
94
 
117
-
118
95
  def cached_list(opts={as_hash: true})
119
96
  retval = @timeline_list.nil? ? self.list(opts) : @timeline_list
120
97
  opts[:as_hash] ? retval.map(&:to_hash).map(&:with_indifferent_access) : retval
@@ -151,16 +128,12 @@ module Glass
151
128
  timeline_list(opts)
152
129
  end
153
130
 
154
-
155
-
156
-
157
131
  def delete(options={})
158
132
  deleting_content = { api_method: mirror_api.timeline.delete,
159
133
  parameters: options }
160
134
  google_client.execute(deleting_content)
161
135
  end
162
136
 
163
-
164
137
  def response_hash(google_response)
165
138
  JSON.parse(google_response.body).with_indifferent_access
166
139
  end
@@ -194,8 +167,6 @@ module Glass
194
167
  ea_data_hash
195
168
  end
196
169
 
197
-
198
- private
199
170
  def format_hash_properly(data_hash)
200
171
  data_hash.inject({}) do |acc, (key, value)|
201
172
  new_key = key.to_s.camelize(:lower)
@@ -203,11 +174,25 @@ module Glass
203
174
  acc
204
175
  end.with_indifferent_access
205
176
  end
177
+
206
178
  def to_google_time(time)
207
179
  Time.now.to_i + time
208
180
  end
181
+
209
182
  def format_date(time)
210
183
  time.to_time.utc.iso8601.gsub("Z", ".000Z") # fucking google has a weird format
211
184
  end
185
+
186
+ def initialize_google_client
187
+ application_name = ::Glass.application_name
188
+ application_version = ::Glass.application_versio
189
+ self.google_client = ::Google::APIClient.new(application_name: application_name,
190
+ application_version: application_version)
191
+ self.mirror_api = google_client.discovered_api("mirror", "v1")
192
+ end
193
+
194
+ def setup_google_api_keys
195
+ self.api_keys = ::Glass._api_keys
196
+ end
212
197
  end
213
198
  end
@@ -1,5 +1,5 @@
1
1
  module Glass
2
2
  module Rails
3
- VERSION="1.0.0"
3
+ VERSION="1.0.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glass-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kunal Modi