Class: Posterboard::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Connection

default behavior is to query the API key upon initialization



12
13
14
15
16
17
18
19
20
21
# File 'lib/posterboard/connection.rb', line 12

def initialize(username, password)
  Connection.basic_auth username, password
  # get the API token
  response = Connection.get('/auth/token')
  Connection.default_params :api_token => response['api_token']

  @logger = ::Logger.new(STDOUT)
  @logger.level = ::Logger::DEBUG
  @logger.debug("Created logger")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

try to find posts on a site with a name that matches the method name posts are returned as an array of hashes each with a :title and :body

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/posterboard/connection.rb', line 25

def method_missing(name)
  @logger.debug "Looking for site (#{name})."
  site = sites.find do |site| 
    @logger.debug "Found (#{site["name"]})."
    site['name'] == name.to_s
  end
  raise SiteNotFoundError unless site
  raise MissingSiteIDError, "Is the Posterous service broken?" unless site.key? "id"
  response = Connection.get("/users/me/sites/#{site["id"]}/posts")
  posts = []
  ::JSON.parse(response.body).each do |content|  
    # => TODO: BlankSlate and define these as methods on post, i.e. post.title, or use OpenStruct
    post = OpenStruct.new
    post.title = content['title']
    post.body = content['body_full']
    post.date = Time.parse(content['display_date'])
    posts << post
  end
  posts
end