Class: Craigslist::API::Result

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

Overview

The outcome of a single posting within a bulk submission.

Bulk submissions succeed and fail per item, and the HTTP status says nothing about it — a 200 can carry a document in which every posting failed. So submissions return results rather than raising: a batch with a few rejections is ordinary, not exceptional.

Constant Summary collapse

VALID =

Returned by validate when the posting looks acceptable.

"VALID"
NOT_VALID =

Validation failed. #explanation says why.

"NOT_VALID"
POSTED =

The posting was accepted.

"POSTED"
FAILED =

Unexpected error at post time.

"FAILED"
INSUFFICIENT_BLOCKS =

The account has no remaining blocks for this area/category and is not invoiced. More must be purchased before the posting is processed.

"INSUFFICIENT_BLOCKS"
CREDIT_LIMIT_REACHED =

The invoiced account hit its credit limit.

"CREDIT_LIMIT_REACHED"
CREDIT_CARD_ERROR =

Billing the card named by cl:purchaseWithCreditCardID failed.

"CREDIT_CARD_ERROR"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, status: nil, explanation: nil, posting_id: nil, manage_url: nil, view_url: nil, preview_html: nil, warnings: []) ⇒ Result

Returns a new instance of Result.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/craigslist/api/result.rb', line 59

def initialize(key:, status: nil, explanation: nil, posting_id: nil,
  manage_url: nil, view_url: nil, preview_html: nil, warnings: [])
  @key = key
  @status = status
  @explanation = explanation
  @posting_id = posting_id
  @manage_url = manage_url
  @view_url = view_url
  @preview_html = preview_html
  @warnings = Array(warnings).freeze
  freeze
end

Instance Attribute Details

#explanationString? (readonly)

Returns human-readable detail, populated on failure.

Returns:

  • (String, nil)

    human-readable detail, populated on failure



41
42
43
# File 'lib/craigslist/api/result.rb', line 41

def explanation
  @explanation
end

#keyString (readonly)

Returns the key you supplied for this posting.

Returns:

  • (String)

    the key you supplied for this posting



35
36
37
# File 'lib/craigslist/api/result.rb', line 35

def key
  @key
end

#manage_urlString? (readonly)

Returns URL for editing or deleting the posting.

Returns:

  • (String, nil)

    URL for editing or deleting the posting



47
48
49
# File 'lib/craigslist/api/result.rb', line 47

def manage_url
  @manage_url
end

#posting_idString? (readonly)

Returns craigslist's id for the new posting, on success.

Returns:

  • (String, nil)

    craigslist's id for the new posting, on success



44
45
46
# File 'lib/craigslist/api/result.rb', line 44

def posting_id
  @posting_id
end

#preview_htmlString? (readonly)

Returns HTML preview of the posting as submitted.

Returns:

  • (String, nil)

    HTML preview of the posting as submitted



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

def preview_html
  @preview_html
end

#statusString? (readonly)

Returns one of the status constants above.

Returns:

  • (String, nil)

    one of the status constants above



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

def status
  @status
end

#view_urlString? (readonly)

Returns public URL of the posting.

Returns:

  • (String, nil)

    public URL of the posting



50
51
52
# File 'lib/craigslist/api/result.rb', line 50

def view_url
  @view_url
end

#warningsArray<String> (readonly)

Returns non-fatal warnings, typically XML parsing complaints. Worth logging even when the posting succeeded.

Returns:

  • (Array<String>)

    non-fatal warnings, typically XML parsing complaints. Worth logging even when the posting succeeded.



57
58
59
# File 'lib/craigslist/api/result.rb', line 57

def warnings
  @warnings
end

Instance Method Details

#credit_card_error?Boolean

Returns the credit card could not be billed.

Returns:

  • (Boolean)

    the credit card could not be billed



108
109
110
# File 'lib/craigslist/api/result.rb', line 108

def credit_card_error?
  status == CREDIT_CARD_ERROR
end

#credit_limit_reached?Boolean

Returns the invoiced account hit its credit limit.

Returns:

  • (Boolean)

    the invoiced account hit its credit limit



103
104
105
# File 'lib/craigslist/api/result.rb', line 103

def credit_limit_reached?
  status == CREDIT_LIMIT_REACHED
end

#failure?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/craigslist/api/result.rb', line 88

def failure?
  !success?
end

#inspectObject



117
118
119
120
# File 'lib/craigslist/api/result.rb', line 117

def inspect
  "#<#{self.class.name} key=#{key.inspect} status=#{status.inspect} " \
    "posting_id=#{posting_id.inspect}>"
end

#insufficient_blocks?Boolean

Returns the account is out of posting blocks.

Returns:

  • (Boolean)

    the account is out of posting blocks



98
99
100
# File 'lib/craigslist/api/result.rb', line 98

def insufficient_blocks?
  status == INSUFFICIENT_BLOCKS
end

#not_valid?Boolean

Returns rejected during validation.

Returns:

  • (Boolean)

    rejected during validation



93
94
95
# File 'lib/craigslist/api/result.rb', line 93

def not_valid?
  status == NOT_VALID
end

#posted?Boolean

Returns accepted for posting (post mode only).

Returns:

  • (Boolean)

    accepted for posting (post mode only)



78
79
80
# File 'lib/craigslist/api/result.rb', line 78

def posted?
  status == POSTED
end

#success?Boolean

Returns either valid or posted.

Returns:

  • (Boolean)

    either valid or posted



83
84
85
# File 'lib/craigslist/api/result.rb', line 83

def success?
  valid? || posted?
end

#valid?Boolean

Returns validated successfully (validate mode only).

Returns:

  • (Boolean)

    validated successfully (validate mode only)



73
74
75
# File 'lib/craigslist/api/result.rb', line 73

def valid?
  status == VALID
end

#warnings?Boolean

Returns whether any non-fatal warnings came back.

Returns:

  • (Boolean)

    whether any non-fatal warnings came back



113
114
115
# File 'lib/craigslist/api/result.rb', line 113

def warnings?
  !warnings.empty?
end