Class: Rumblr::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rumblr/client.rb

Constant Summary collapse

DEFAULT_HEADER =

The default set of headers for each request.

{
    'User-Agent'  => 'Rumblr',
    'Accept'      => 'application/xml'
}

Instance Method Summary collapse

Instance Method Details

#authenticate(email, password) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rumblr/client.rb', line 12

def authenticate(email,password)
  raise ArgumentError unless (email && password)
  user_credentials = { :email => email, :password => password }
  uri = URI::HTTP.build({:path => '/api/authenticate'})
  request = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  request.set_form_data(user_credentials)
  
  user_attributes = nil
  complete_request(request) do |response_body|
    user_attributes = parse_user_attributes_from(response_body)
    user_attributes.merge!(user_credentials)
  end
  user = User.new(user_attributes)
  user.tumblelogs.each{ |tumblelog| tumblelog.user = user }
  user
end

#read(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rumblr/client.rb', line 29

def read(options={})
  tumblelog, posts = nil, [] # initialize the return targets
  
  uri = URI::HTTP.build({:path => '/api/read'})
  request = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  request.set_form_data(options)
  
  raise(ArgumentError) unless (options && options[:url] && !options[:url].empty?)
  
  request_url_host = URI.parse(options[:url]).host
  complete_request(request,host=request_url_host) do |response_body|
    parser = XML::Parser.string(response_body)
    doc = parser.parse
    # parse and map tumblelog
    tumblelog_element = doc.find_first('//tumblr/tumblelog')
    tumblelog_attrs = cleanup_hash(tumblelog_element.attributes.to_h)
    tumblelog_attrs.merge!(:url => options[:url])
    tumblelog = Tumblelog.new(tumblelog_attrs)
    # parse and map posts
    posts = doc.find('//tumblr/posts/post').inject([]) do |array, element|
      post_attrs = cleanup_hash(element.attributes.to_h)
      # inner elements =>  sublcass-specific attribute hash
      subclass_attrs = element.children.inject({'tags' => []}) do |hash, child|
        hash['tags'] << child.content if (child.name[/tag/])
        hash[child.name] = child.content
        hash
      end
      # merge hashes, clean out cruft
      inner_attrs = cleanup_hash(subclass_attrs,post_attrs[:type])
      inner_attrs.delete(:text)
      inner_attrs.delete(:tag)
      post_attrs.merge!(inner_attrs)
      # turn attributes into proper model
      klass = Rumblr.const_get(Post::TYPES[post_attrs[:type]])
      raise 'unknown post type' unless klass
      post = klass.new(post_attrs)
      post.tumblelog = tumblelog
      array << post
      array
    end
  end
  return tumblelog, posts
end

#write(post, creds) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
# File 'lib/rumblr/client.rb', line 73

def write(post, creds)
  raise(ArgumentError) unless post && post.is_a?(Post)
  raise(ArgumentError) unless creds
  uri = URI::HTTP.build(:path => "/api/write")
  request = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  request.set_form_data(creds.merge(:type => Post::TYPES.invert[post.class]).merge(post.attribute_hash))
  request_url_host = URI.parse(uri.request_uri).host
  complete_request(request)
end