Sha256: aedbd4a7885ea2eb5d72b51813eea4736232adb7bf4f2c8fad440f6828933ac3

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module CureLine
  require "open-uri"
  require "json"

  class User
    attr_reader :user_id

    include ResourceMethods

    # @param user_id [String]
    #
    # @example
    #   # read https://timeline.line.me/user/_dYbbV3vmaJrvqBoV5ZlpCbPN2CWUZdDQayBvjBE
    #   user = CureLine::User.new("_dYbbV3vmaJrvqBoV5ZlpCbPN2CWUZdDQayBvjBE")
    def initialize(user_id)
      @user_id = user_id
    end

    # @return [CureLine::Mash]
    def info
      preloaded_state["userHome"]["homeInfo"]["userInfo"]
    end

    # @return [String]
    def header_url
      resource_url(preloaded_state["userHome"]["homeInfo"]["resourceId"])
    end

    # @return [Array<CureLine::Post>]
    def posts
      preloaded_state["userHome"]["feeds"].map { |feed| CureLine::Post.new(feed) }
    end

    # @return [CureLine::Mash]
    def preloaded_state
      return @preloaded_state if @preloaded_state

      options = {}
      options["User-Agent"] = CureLine.config.user_agent if CureLine.config.user_agent

      url = "https://timeline.line.me/user/#{user_id}"
      html = open(url, options).read

      m = html.match(%r{<script id="init_data" type="application/json">({.+})</script>})
      raise %Q(Not Found <script id="init_data" type="application/json"> in #{url}) unless m

      @preloaded_state = CureLine::Mash.new(JSON.parse(m[1]))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cure_line-0.1.3 lib/cure_line/user.rb