Class: SquarespaceApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/squarespace_api/connection.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
  'User-Agent' => 'SquarespaceApi'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
# 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
  end

  initialize_app_session if config.access_token
end

Instance Attribute Details

#configObject (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



33
34
35
# File 'lib/squarespace_api/connection.rb', line 33

def delete(path, params: {}, headers: {})
  handle_response(connection.delete(path, params, connection.headers.merge(headers)))
end

#get(path, params: {}, headers: {}) ⇒ Object



25
26
27
# File 'lib/squarespace_api/connection.rb', line 25

def get(path, params: {}, headers: {})
  handle_response(connection.get(path, params, connection.headers.merge(headers)))
end

#initialize_app_sessionObject



58
59
60
61
62
# File 'lib/squarespace_api/connection.rb', line 58

def initialize_app_session
  @connection.headers['Authorization'] = "Bearer #{config.access_token}"
  @connection.url_prefix = api_base_url
  self
end

#initialize_oauth_sessionObject



69
70
71
72
73
# File 'lib/squarespace_api/connection.rb', line 69

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



29
30
31
# File 'lib/squarespace_api/connection.rb', line 29

def post(path, params: {}, headers: {})
  handle_response(connection.post(path, params, connection.headers.merge(headers)))
end

#upload_file(path, file_path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/squarespace_api/connection.rb', line 37

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_sessionObject



53
54
55
56
# File 'lib/squarespace_api/connection.rb', line 53

def with_app_session
  initialize_app_session
  yield
end

#with_idempotency_keyObject



48
49
50
51
# File 'lib/squarespace_api/connection.rb', line 48

def with_idempotency_key
  @connection.headers['Idempotency-Key'] = SecureRandom.hex
  yield
end

#with_oauth_sessionObject



64
65
66
67
# File 'lib/squarespace_api/connection.rb', line 64

def with_oauth_session
  initialize_oauth_session
  yield
end