| 1 |  |  | module MediumSdk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |   class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |     attr_accessor :connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     def initialize(opts = {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |       if opts.key? :client_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |         @connection = MediumSdk::Connection::AuthCode.new opts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |       elsif opts.key? :integration_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         @connection = MediumSdk::Connection::IntegrationToken.new opts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     def get_url(url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |       return @connection.http.get do |req| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         req.url url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def body_key(res, key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |       if res.status >= 400 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         raise "HTTP Error #{res.status} " + res.pretty_inspect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |       return res.body.key?(key) ? res.body[key] : nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     def me(reload = nil) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |       if @_me.nil? || reload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         @_me = body_key get_url('me'), 'data' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |       return @_me | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     def me_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |       me unless @_me | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |       unless @_me.is_a?(Hash) && @_me.key?('id') && @_me['id'].to_s.length>0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         raise 'Authorized User Id is unknown' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |       return @_me['id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     def user_publications(user_id = nil) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |       user_id = me_id if user_id.nil? | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |       res = get_url File.join 'users', user_id.to_s, 'publications' | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |       return body_key(res, 'data') | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def contributing_publications(user_id = nil) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       publications = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |       user_id = me_id if user_id.nil? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |       all_publications = user_publications(user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |       all_publications_ids = all_publications.map { |x| x["id"] } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |       all_publications_ids.each do |pub_id| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         publication_contributors(pub_id).each do |contributor| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |           if contributor["userId"] == user_id && contributor["role"] == "editor" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             publications << all_publications.select {|pub| pub["id"] == contributor["publicationId"] } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |           end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         end  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |       return publications.flatten | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     def post(post) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |       url = '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |       if post.key? :publicationId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         publication_id = post[:publicationId].clone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         post.delete :publicationId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         url = File.join 'publications', publication_id, 'posts' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |       else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         url = File.join 'users', me_id(), 'posts' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |       res = @connection.http.post do |req| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         req.url url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         req.body = post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |       return body_key(res, 'data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     def publication_contributors(publication_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |       res = get_url File.join 'publications', publication_id, 'contributors' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |       return body_key(res, 'data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |   end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | end | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |