lib/rumblr/client.rb in jamescallmebrent-rumblr-0.0.1 vs lib/rumblr/client.rb in jamescallmebrent-rumblr-0.0.2

- old
+ new

@@ -19,11 +19,13 @@ user_attributes = nil complete_request(request) do |response_body| user_attributes = parse_user_attributes_from(response_body) user_attributes.merge!(user_credentials) end - User.new(user_attributes) + user = User.new(user_attributes) + user.tumblelogs.each{ |tumblelog| tumblelog.user = user } + user end def read(options={}) tumblelog, posts = nil, [] # initialize the return targets @@ -33,12 +35,17 @@ 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, parser.string = XML::Parser.new, 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| @@ -50,34 +57,32 @@ inner_attrs = cleanup_hash(subclass_attrs) inner_attrs.delete(:text) inner_attrs.delete(:tag) post_attrs.merge!(inner_attrs) # turn attributes into proper model - klass = case post_attrs[:type] - when 'regular' then RegularPost - when 'photo' then PhotoPost - when 'quote' then QuotePost - when 'link' then LinkPost - when 'conversation' then ConversationPost - when 'video' then VideoPost - when 'audio' then AudioPost - else raise 'unknown post type' - end + 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 - # 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) end return tumblelog, posts end + 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 + protected # Starts and completes the given request. Returns or yields the response body. def complete_request(request, host = URI.parse(Rumblr.api_url).host) http = Net::HTTP.new(host) @@ -109,11 +114,11 @@ private def parse_user_attributes_from(response_body) - parser, parser.string = XML::Parser.new, response_body + parser = XML::Parser.string response_body doc = parser.parse tumblelogs = doc.find('//tumblr/tumblelog').inject([]) do |array, element| tumblelog_attrs = cleanup_hash(element.attributes.to_h) array << Tumblelog.new(tumblelog_attrs) array @@ -136,6 +141,6 @@ end end end -end \ No newline at end of file +end