Class: Smugsyncv2::Client
- Inherits:
-
Object
- Object
- Smugsyncv2::Client
- Defined in:
- lib/smugsyncv2/client.rb
Constant Summary collapse
- TOKEN_FILE =
'.token_cache'
Instance Method Summary collapse
- #access_token ⇒ Object
- #adapter(url: BASE_URL) ⇒ Object
- #cache_token(token) ⇒ Object
- #connection(**args) ⇒ Object
- #consumer ⇒ Object
- #get_oauth_header(method, url, params) ⇒ Object
- #get_uri(name) ⇒ Object
-
#initialize(key, secret, logger = false) ⇒ Client
constructor
A new instance of Client.
- #load_cached_token ⇒ Object
-
#login ⇒ Object
rubocop:disable Metrics/MethodLength.
- #oauth_opts ⇒ Object
- #request(method: :get, path: nil, params: {}, body: {}, headers: {}) ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(key, secret, logger = false) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 |
# File 'lib/smugsyncv2/client.rb', line 7 def initialize(key, secret, logger = false) @uris = nil @key = key @secret = secret @logger = logger end |
Instance Method Details
#access_token ⇒ Object
36 37 38 |
# File 'lib/smugsyncv2/client.rb', line 36 def access_token @access_token ||= login end |
#adapter(url: BASE_URL) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/smugsyncv2/client.rb', line 59 def adapter(url: BASE_URL) @connection = Faraday.new(url: url) do |conn| conn.request :json conn.response :json conn.adapter Faraday.default_adapter conn.response :logger if @logger end end |
#cache_token(token) ⇒ Object
53 54 55 56 57 |
# File 'lib/smugsyncv2/client.rb', line 53 def cache_token(token) File.open(TOKEN_FILE, 'w') do |file| file.write Marshal.dump(token) end end |
#connection(**args) ⇒ Object
68 69 70 |
# File 'lib/smugsyncv2/client.rb', line 68 def connection(**args) @connection ||= adapter(**args) end |
#consumer ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/smugsyncv2/client.rb', line 40 def consumer if @consumer @consumer else login @consumer end end |
#get_oauth_header(method, url, params) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/smugsyncv2/client.rb', line 72 def get_oauth_header(method, url, params) SimpleOAuth::Header.new( method, url, params, consumer_key: @key, consumer_secret: @secret, token: access_token.token, token_secret: access_token.secret, version: '1.0').to_s end |
#get_uri(name) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/smugsyncv2/client.rb', line 109 def get_uri(name) uri = @uris.send(name).Uri request(path: uri) if @response && @response.Response && @response.Response.send(name) @uris = @response['Response'][name]['Uris'] end @response end |
#load_cached_token ⇒ Object
49 50 51 |
# File 'lib/smugsyncv2/client.rb', line 49 def load_cached_token Marshal.load(File.open(TOKEN_FILE, 'r')) end |
#login ⇒ Object
rubocop:disable Metrics/MethodLength
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/smugsyncv2/client.rb', line 22 def login # rubocop:disable Metrics/MethodLength @consumer = OAuth::Consumer.new(@key, @secret, oauth_opts) return load_cached_token if File.exist?(TOKEN_FILE) @request_token = @consumer.get_request_token = @request_token. + '&Access=Full' puts "Open a web browser and open: #{}" puts 'Enter the validation code: ' verification_code = STDIN.gets.chomp @access_token = @request_token.get_access_token( oauth_verifier: verification_code) cache_token(@access_token) @access_token end |
#oauth_opts ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/smugsyncv2/client.rb', line 14 def oauth_opts { site: OAUTH_ORIGIN, request_token_path: REQUEST_TOKEN_PATH, access_token_path: ACCESS_TOKEN_PATH, authorize_path: AUTHORIZE_PATH } end |
#request(method: :get, path: nil, params: {}, body: {}, headers: {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/smugsyncv2/client.rb', line 83 def request(method: :get, path: nil, params: {}, body: {}, headers: {}) url = path.nil? ? BASE_URL : File.join(API_ORIGIN, path) base_headers = { 'User-Agent' => USER_AGENT, 'Accept' => 'application/json' } headers = base_headers.merge(headers || {}) adapter(url: url) response = @connection.send(method) do |req| oauth_header = get_oauth_header(method, url, params) req.headers.merge!('Authorization' => oauth_header) req.url url req.headers.merge!(headers) req.params.merge!(params) req.body = body end @response = DeepOpenStruct.load(response.body) end |
#user ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/smugsyncv2/client.rb', line 101 def user res = request uri = res.Response.Uris.AuthUser.Uri user = request(path: uri) user = user.Response.User.Uris @uris = user end |