rails-pinterest 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b7c50a6d58454c8af8c007c32231f3c3f874c31304e7465fc0675655cf69a3f
4
- data.tar.gz: e2dc9227c4325527e2d5bdf64ea9892cd254c6428a8a925fb81b7e74d5329d39
3
+ metadata.gz: '097b2ce4d2ec083c16dde219ab02d002612ed44147f0ea1ba7efbca852fd619f'
4
+ data.tar.gz: 4997ffce4d401df22dcd418437d75d0156bafa0b89568906a62b46b348286a71
5
5
  SHA512:
6
- metadata.gz: 0d731a33f948dc774545d087740070623248974571187b5c25d2609cd71b2513ed457e4c820f9cea27e28de69ee8cc61c78dcc3e154328a098b630744381a866
7
- data.tar.gz: 2790992016e2b563fb99146dbda1d737cf99e1115c2ceace8bd0d4b961027922357d3806e5265e63b78efc1961679fd0816a99cec161293995ed3c6f320459fe
6
+ metadata.gz: '009d70127767d9823c65f25f88bb5a2c4aff755edcd3b0a750f2160efb79724053ec20d168cfe0643963b034610b07400ba1dd52c5e1e6242b052e5758fc3721'
7
+ data.tar.gz: ec8e20194ed18c512c3ed412d94e571b382608cfcbe72a87f2b0e9a93233481d2a322b618f22768957fe068107de5631cd9c48a2f00c5545747f50e6ce1e8382
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2023-10-01
3
+ ## [0.1.0] - 2023-11-30
4
4
 
5
5
  - Initial release
6
- - Endpoints for Pins and Boards created
6
+ - Endpoints for Pins and Boards created
7
+
8
+ ## [0.1.1] - 2023-11-30
9
+ - Update the gemfile.lock
10
+ - Push the tagged version to github
11
+ - Re-release to rubygems
12
+
13
+ ## [0.1.2] - 2023-12-2
14
+ - Added oauth refresh token endpoint.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-pinterest (0.1.1)
4
+ rails-pinterest (0.1.2)
5
5
  faraday (>= 1)
6
6
  faraday-multipart (>= 1)
7
7
 
data/README.md CHANGED
@@ -174,6 +174,46 @@ response = client.pins.create_pin(parameters: parameters)
174
174
 
175
175
  ```
176
176
 
177
+ ### Oauth
178
+
179
+ How to refresh your access tokens
180
+
181
+ ```ruby
182
+ # https://developers.pinterest.com/docs/api/v5/#tag/oauth
183
+
184
+ # POST /oauth/tokens to refresh tokens
185
+ parameters = {
186
+ refresh_token: "your-refresh-token-goes-here",
187
+ grant_type: 'refresh_token' # Keep it as 'refresh_token' as per Pinterest API requirements
188
+ scope: 'whatever-new-scopes-desired' #optional
189
+ }
190
+ response = client.oauth.retrieve_oauth_token(parameters: parameters)
191
+ puts response
192
+ # { "response_type": "refresh_token", "access_token": "string", "token_type": "bearer", "expires_in": 0, "scope": "string" }
193
+
194
+ ```
195
+
196
+ Running the Oauth Refresh Token in Console
197
+ ```ruby
198
+ # https://developers.pinterest.com/docs/api/v5/#tag/oauth
199
+
200
+ # 1. Create the client
201
+ client = Pinterest::Client.new(refresh_token: "your-refresh-token", access_token: "can-be-any-fake-or-real-token", client_id: 'client-id-or-app-id' , secret_key: "secret-key")
202
+
203
+ # 2. Set up the params
204
+ parameters = {
205
+ refresh_token: "your-refresh-token-goes-here",
206
+ grant_type: 'refresh_token' # Keep it as 'refresh_token' as per Pinterest API requirements
207
+ scope: 'whatever-new-scopes-desired' #optional
208
+ }
209
+
210
+ # 3. Retrieve refreshed access_token
211
+ response = client.oauth.retrieve_oauth_token(parameters: parameters)
212
+ puts response
213
+ # { "response_type": "refresh_token", "access_token": "string", "token_type": "bearer", "expires_in": 0, "scope": "string" }
214
+
215
+ ```
216
+ **NOTE:** This is likely how you'll be doing it if you manage multiple Pinterest Apps for users in your web/mobile app. Because you'll have multiple access_tokens and refresh_tokens, you can't have this in an .env configuration. You'll likely create new clients every time you refresh with your users' or customers' saved access/refresh_tokens.
177
217
  ## Development
178
218
 
179
219
  After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -6,6 +6,9 @@ module Pinterest
6
6
  api_type
7
7
  api_version
8
8
  access_token
9
+ refresh_token
10
+ client_id
11
+ secret_key
9
12
  uri_base
10
13
  request_timeout
11
14
  extra_headers
@@ -27,5 +30,9 @@ module Pinterest
27
30
  def pins
28
31
  @pins ||= Pinterest::Pins.new(client: self)
29
32
  end
33
+
34
+ def oauth
35
+ @oauth ||= Pinterest::Oauth.new(client: self)
36
+ end
30
37
  end
31
38
  end
@@ -34,6 +34,14 @@ module Pinterest
34
34
  end&.body)
35
35
  end
36
36
 
37
+ def oauth_post(path:, parameters:)
38
+ response = oauth_conn.post(uri(path: path)) do |req|
39
+ req.headers = oauth_headers
40
+ req.body = parameters
41
+ end
42
+ response
43
+ end
44
+
37
45
  private
38
46
 
39
47
  def to_json(string)
@@ -52,6 +60,14 @@ module Pinterest
52
60
  end
53
61
  end
54
62
 
63
+ def oauth_conn(multipart: false)
64
+ Faraday.new do |f|
65
+ f.options[:timeout] = @request_timeout
66
+ f.request :url_encoded
67
+ f.adapter Faraday.default_adapter
68
+ end
69
+ end
70
+
55
71
  def uri(path:)
56
72
  File.join(@uri_base, @api_version, path)
57
73
  end
@@ -67,6 +83,15 @@ module Pinterest
67
83
  }
68
84
  end
69
85
 
86
+ def oauth_headers
87
+ token = Base64.encode64("#{@client_id}:#{@secret_key}").gsub("\n", '')
88
+ puts "Token: #{token}"
89
+ {
90
+ "Authorization" => "Basic #{token}",
91
+ "Content-Type" => "application/x-www-form-urlencoded",
92
+ }
93
+ end
94
+
70
95
  def multipart_parameters(parameters)
71
96
  parameters&.transform_values do |value|
72
97
  next value unless value.respond_to?(:close) # File or IO object.
@@ -0,0 +1,11 @@
1
+ module Pinterest
2
+ class Oauth
3
+ def initialize(client: nil)
4
+ @client = client
5
+ end
6
+
7
+ def retrieve_oauth_token(parameters: {})
8
+ @client.oauth_post(path: "/oauth/token", parameters: parameters)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Pinterest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/pinterest.rb CHANGED
@@ -3,6 +3,7 @@ require "faraday/multipart"
3
3
  require_relative "pinterest/http"
4
4
  require_relative "pinterest/client"
5
5
  require_relative "pinterest/boards"
6
+ require_relative "pinterest/oauth"
6
7
  require_relative "pinterest/pins"
7
8
  require_relative "pinterest/version"
8
9
 
@@ -13,7 +14,7 @@ module Pinterest
13
14
  class Configuration
14
15
  attr_writer :access_token
15
16
  attr_accessor :api_type, :api_version, :uri_base, :request_timeout,
16
- :extra_headers
17
+ :extra_headers, :refresh_token, :client_id, :secret_key
17
18
 
18
19
  DEFAULT_API_VERSION = "v5".freeze
19
20
  DEFAULT_URI_BASE = "https://api.pinterest.com/".freeze
@@ -22,6 +23,9 @@ module Pinterest
22
23
  def initialize
23
24
  @access_token = nil
24
25
  @api_type = nil
26
+ @refresh_token = nil
27
+ @client_id = nil
28
+ @secret_key = nil
25
29
  @api_version = DEFAULT_API_VERSION
26
30
  @uri_base = DEFAULT_URI_BASE
27
31
  @request_timeout = DEFAULT_REQUEST_TIMEOUT
@@ -34,7 +38,7 @@ module Pinterest
34
38
  def access_token
35
39
  return @access_token if @access_token
36
40
 
37
- error_text = "Pinterest access token missing! See https://github.com/alexrudall/ruby-openai#usage"
41
+ error_text = "Pinterest access token missing!"
38
42
  raise ConfigurationError, error_text
39
43
  end
40
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-pinterest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donald Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-01 00:00:00.000000000 Z
11
+ date: 2023-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -60,6 +60,7 @@ files:
60
60
  - lib/pinterest/client.rb
61
61
  - lib/pinterest/compatibility.rb
62
62
  - lib/pinterest/http.rb
63
+ - lib/pinterest/oauth.rb
63
64
  - lib/pinterest/pins.rb
64
65
  - lib/pinterest/version.rb
65
66
  - lib/ruby/pinterest.rb