Class: Craigslist::API::JsonTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/craigslist/api/json_transport.rb

Overview

Request plumbing for the JSON Bulkpost API.

Owns three concerns the resource classes should not repeat: attaching a bearer token (refreshing once on a 401), mapping HTTP status onto this library's error classes, and unwrapping the response envelope.

Constant Summary collapse

FORM_CONTENT_TYPE =

Writes to the Bulkpost API are form-encoded, not JSON, even though the responses are JSON.

"application/x-www-form-urlencoded"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, connection:, token_provider:) ⇒ JsonTransport

Returns a new instance of JsonTransport.



20
21
22
23
24
25
# File 'lib/craigslist/api/json_transport.rb', line 20

def initialize(config:, connection:, token_provider:)
  @config = config
  @connection = connection
  @token_provider = token_provider
  @account_messages = []
end

Instance Attribute Details

#account_messagesArray<Hash> (readonly)

Returns account notices seen on the most recent response.

Returns:

  • (Array<Hash>)

    account notices seen on the most recent response



18
19
20
# File 'lib/craigslist/api/json_transport.rb', line 18

def 
  @account_messages
end

Instance Method Details

#delete(path) ⇒ Envelope

Returns:



43
44
45
# File 'lib/craigslist/api/json_transport.rb', line 43

def delete(path)
  run(:delete, path)
end

#get(path, params: {}) ⇒ Envelope

Returns:



28
29
30
# File 'lib/craigslist/api/json_transport.rb', line 28

def get(path, params: {})
  run(:get, path, params: params)
end

#post(path, form: nil) ⇒ Envelope

Returns:



33
34
35
# File 'lib/craigslist/api/json_transport.rb', line 33

def post(path, form: nil)
  run(:post, path, form: form)
end

#put(path, form: nil) ⇒ Envelope

Returns:



38
39
40
# File 'lib/craigslist/api/json_transport.rb', line 38

def put(path, form: nil)
  run(:put, path, form: form)
end

#upload(path, part:, fields: {}) ⇒ Envelope

Uploads a file as multipart/form-data.

Parameters:

  • path (String)
  • part (Faraday::Multipart::FilePart)
  • fields (Hash) (defaults to: {})

    additional form fields

Returns:



53
54
55
# File 'lib/craigslist/api/json_transport.rb', line 53

def upload(path, part:, fields: {})
  run(:put, path, body: fields.merge("file" => part), content_type: nil)
end