Class: SquarespaceApi::Connection
- Inherits:
-
Object
- Object
- SquarespaceApi::Connection
- Defined in:
- lib/squarespace_api/connection.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json', 'User-Agent' => 'SquarespaceApi' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #delete(path, params: {}, headers: {}) ⇒ Object
- #get(path, params: {}, headers: {}) ⇒ Object
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #initialize_app_session ⇒ Object
- #initialize_oauth_session ⇒ Object
- #post(path, params: {}, headers: {}) ⇒ Object
- #upload_file(path, file_path) ⇒ Object
- #with_app_session ⇒ Object
- #with_idempotency_key ⇒ Object
- #with_oauth_session ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/squarespace_api/connection.rb', line 15 def initialize(config) @config = config @connection = Faraday.new do |connection| connection.headers = DEFAULT_HEADERS connection.response :json connection.adapter(Faraday.default_adapter) end initialize_app_session if config.access_token end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/squarespace_api/connection.rb', line 13 def config @config end |
Instance Method Details
#delete(path, params: {}, headers: {}) ⇒ Object
34 35 36 |
# File 'lib/squarespace_api/connection.rb', line 34 def delete(path, params: {}, headers: {}) handle_response(connection.delete(path, params, connection.headers.merge(headers))) end |
#get(path, params: {}, headers: {}) ⇒ Object
26 27 28 |
# File 'lib/squarespace_api/connection.rb', line 26 def get(path, params: {}, headers: {}) handle_response(connection.get(path, params, connection.headers.merge(headers))) end |
#initialize_app_session ⇒ Object
59 60 61 62 63 |
# File 'lib/squarespace_api/connection.rb', line 59 def initialize_app_session @connection.headers['Authorization'] = "Bearer #{config.access_token}" @connection.url_prefix = api_base_url self end |
#initialize_oauth_session ⇒ Object
70 71 72 73 74 |
# File 'lib/squarespace_api/connection.rb', line 70 def initialize_oauth_session @connection.headers['Authorization'] = "Basic #{config.encoded_oauth_token}" @connection.url_prefix = config.oauth_base_url self end |
#post(path, params: {}, headers: {}) ⇒ Object
30 31 32 |
# File 'lib/squarespace_api/connection.rb', line 30 def post(path, params: {}, headers: {}) handle_response(connection.post(path, params, connection.headers.merge(headers))) end |
#upload_file(path, file_path) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/squarespace_api/connection.rb', line 38 def upload_file(path, file_path) temp_connection = Faraday.new(url: api_base_url) do |f| f.request :multipart f.response :json f.headers = DEFAULT_HEADERS.merge( 'Authorization' => "Bearer #{config.access_token}", 'Content-Type' => 'multipart/form-data', ) end.post(path, { file: Faraday::FilePart.new(file_path, 'text/x-ruby') }) end |
#with_app_session ⇒ Object
54 55 56 57 |
# File 'lib/squarespace_api/connection.rb', line 54 def with_app_session initialize_app_session yield end |
#with_idempotency_key ⇒ Object
49 50 51 52 |
# File 'lib/squarespace_api/connection.rb', line 49 def with_idempotency_key @connection.headers['Idempotency-Key'] = SecureRandom.hex yield end |
#with_oauth_session ⇒ Object
65 66 67 68 |
# File 'lib/squarespace_api/connection.rb', line 65 def with_oauth_session initialize_oauth_session yield end |