Class: Kinja::Client
- Inherits:
-
Object
- Object
- Kinja::Client
- Defined in:
- lib/kinja.rb
Constant Summary collapse
- API_ROOT =
"http://kinja.com/api"- LOGIN_PATH =
"/profile/account/burner/login"- TOKEN_PATH =
"/profile/token"- CREATE_POST_PATH =
"/core/post/add"- POST_PATH =
"/core/post"- AUTHOR_PATH =
"/profile/user/views/asAuthor"
Instance Attribute Summary collapse
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #create_post(opts = {}) ⇒ Object
- #get_api_token(response) ⇒ Object
- #get_author(user) ⇒ Object
- #get_default_blog_id(user) ⇒ Object
- #get_post(link_or_id) ⇒ Object
- #get_post_id(link) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
- #session_token(response) ⇒ Object
- #update_post(link_or_id, opts) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 |
# File 'lib/kinja.rb', line 15 def initialize(opts={}) @username = opts[:user] @pass = opts[:password] end |
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
6 7 8 |
# File 'lib/kinja.rb', line 6 def user @user end |
Instance Method Details
#create_post(opts = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kinja.rb', line 20 def create_post(opts={}) get_api_token(login) opts[:status] = opts[:status] || "DRAFT" opts[:replies] = opts[:replies] || true opts[:defaultBlogId] = opts[:defaultBlogId] || get_default_blog_id(@user) HTTParty.post "#{API_ROOT}#{CREATE_POST_PATH}?token=#{@api_token}", body: { headline: opts[:headline], original: opts[:body], defaultBlogId: opts[:defaultBlogId], status: opts[:status], allowReplies: opts[:replies] }.to_json, headers: { 'Content-Type' => 'application/json' } end |
#get_api_token(response) ⇒ Object
74 75 76 77 78 |
# File 'lib/kinja.rb', line 74 def get_api_token(response) @api_token = HTTParty.get("#{API_ROOT}#{TOKEN_PATH}", cookies: {KinjaSession: session_token(response)} )['data']['token'] end |
#get_author(user) ⇒ Object
60 61 62 |
# File 'lib/kinja.rb', line 60 def (user) HTTParty.get "#{API_ROOT}#{AUTHOR_PATH}?ids=#{user["id"]}" end |
#get_default_blog_id(user) ⇒ Object
64 65 66 |
# File 'lib/kinja.rb', line 64 def get_default_blog_id(user) (user)["data"][0]["defaultBlogId"] end |
#get_post(link_or_id) ⇒ Object
47 48 49 50 |
# File 'lib/kinja.rb', line 47 def get_post(link_or_id) id = get_post_id link_or_id HTTParty.get "#{API_ROOT}#{POST_PATH}/#{id}" end |
#get_post_id(link) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/kinja.rb', line 52 def get_post_id(link) return link if link.match(/^\d+$/) new_link_re = /-(\d+)\/?/ old_link_re = /\.com\/(\d+)\// return link.match(new_link_re)[1] if link.match(new_link_re) return link.match(old_link_re)[1] if link.match(old_link_re) end |
#login ⇒ Object
68 69 70 71 72 |
# File 'lib/kinja.rb', line 68 def login response = HTTParty.get "#{API_ROOT}#{LOGIN_PATH}?screenName=#{@username}&token=#{@pass}" @user = response["data"] response end |
#session_token(response) ⇒ Object
80 81 82 83 |
# File 'lib/kinja.rb', line 80 def session_token(response) re = /KinjaSession=([\w-]+);/ response.headers["set-cookie"].match(re)[1] end |
#update_post(link_or_id, opts) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/kinja.rb', line 37 def update_post(link_or_id, opts) get_api_token(login) id = get_post_id link_or_id opts[:defaultBlogId] = opts[:defaultBlogId] || get_default_blog_id(@user) HTTParty.post "#{API_ROOT}#{POST_PATH}/#{id}/update?token=#{@api_token}", body: opts.to_json, headers: { 'Content-Type' => 'application/json' } end |