Class: Posterous::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/posterous.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, site_id = nil, hostname = "") ⇒ Client

Authenticated initialization

Raises:



68
69
70
71
72
73
74
# File 'lib/posterous.rb', line 68

def initialize user, pass, site_id = nil, hostname = ""
  raise AuthError, 'Either Username or Password is blank and/or not a string.' if \
    !user.is_a?(String) || !pass.is_a?(String) || user == "" || pass == ""
  self.class.basic_auth user, pass
  @site_id = site_id ? site_id.to_s : site_id
  @source = @body = @title = @source_url = @date = @media = @tags = @autopost = @private_post = nil
end

Instance Attribute Details

#autopostObject (readonly)

Returns the value of attribute autopost.



65
66
67
# File 'lib/posterous.rb', line 65

def autopost
  @autopost
end

#bodyObject

Returns the value of attribute body.



64
65
66
# File 'lib/posterous.rb', line 64

def body
  @body
end

#dateObject

Returns the value of attribute date.



64
65
66
# File 'lib/posterous.rb', line 64

def date
  @date
end

#private_postObject (readonly)

Returns the value of attribute private_post.



65
66
67
# File 'lib/posterous.rb', line 65

def private_post
  @private_post
end

#site_idObject

Returns the value of attribute site_id.



65
66
67
# File 'lib/posterous.rb', line 65

def site_id
  @site_id
end

#sourceObject

Returns the value of attribute source.



64
65
66
# File 'lib/posterous.rb', line 64

def source
  @source
end

#source_urlObject

Returns the value of attribute source_url.



64
65
66
# File 'lib/posterous.rb', line 64

def source_url
  @source_url
end

#tagsObject

Returns the value of attribute tags.



65
66
67
# File 'lib/posterous.rb', line 65

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



64
65
66
# File 'lib/posterous.rb', line 64

def title
  @title
end

Instance Method Details

#account_infoObject



138
139
140
# File 'lib/posterous.rb', line 138

def 
  self.class.post(AUTH_PATH, :query => {})["rsp"]
end

#add_postObject



142
143
144
# File 'lib/posterous.rb', line 142

def add_post
  self.class.post(POST_PATH, :query => build_query)
end

#build_queryObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/posterous.rb', line 122

def build_query
  options = { :site_id    => @site_id,
              :autopost   => @autopost,
              :private    => @private_post,
              :date       => @date,
              :tags       => @tags }

  query   = { :title      => @title,
              :body       => @body,
              :source     => @source,
              :sourceLink => @source_url }

  options.delete_if { |k,v| !v }
  query.merge!(options)
end

#has_site?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/posterous.rb', line 91

def has_site?
  res = 
  return false unless res.is_a?(Hash)
  
  case res["site"]
  when Hash
    return true unless @site_id
    return @site_id == res["site"]["id"]
  when Array
    res["site"].each do |site|
      return true if @site_id && @site_id == site["id"]
    end      
  end
  false
end

#primary_siteObject

Raises:



107
108
109
110
111
112
113
114
115
# File 'lib/posterous.rb', line 107

def primary_site
  res = 
  raise SiteError, "Couldn't find a primary site. Check login and password is valid." \
    unless res.is_a?(Hash) && res["stat"] == "ok" && res["site"]
  [res["site"]].flatten.each do |site|
    return site["id"] if site["primary"] == "true"
  end
  nil
end

#set_to(on) ⇒ Object



117
118
119
120
# File 'lib/posterous.rb', line 117

def set_to on
  @private_post = 1 if on == :private
  @autopost     = 1 if on == :autopost
end

#valid_user?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/posterous.rb', line 85

def valid_user?
  res = 
  return false unless res.is_a?(Hash)
  res["stat"] == "ok"
end