Sha256: 35c37cd012decc8605e567091db1a30e870e1c5d22714d45bc85708765bb37e3

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'forwardable'
require 'hackernews_ruby/request'

module HackernewsRuby
  class Client
    extend Forwardable

    include Request
    attr_accessor :api_url, :api_version


    def initialize(options={})
      @api_url = "https://hacker-news.firebaseio.com"
      @api_version = "v0"
      @api_url = options[:api_url].nil? ? api_url : options[:api_url]
      @api_version = options[:api_version].nil? ? @api_version : options[:api_version]

      reload_config
    end

    def connection
      params = {}
      @connection = Faraday.new(url: @api_url, params: params,
                                headers: default_headers,
                                ssl: { verify: true } ) do |faraday|
                                  faraday.use FaradayMiddleware::Mashify
                                  faraday.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
                                  faraday.use FaradayMiddleware::FollowRedirects
                                  faraday.adapter Faraday.default_adapter
                                end
    end

    def get_item(id, params={})
      url = "/#{HackernewsRuby.api_version}/item/#{id}.json"
      get(url, params)
    end

    def get_user(id, params={})
      url = "/#{HackernewsRuby.api_version}/user/#{id}.json"
      get(url, params)
    end

    def top_stories(params={})
      url ="/#{HackernewsRuby.api_version}/topstories.json?"
      get(url, params)
    end


    private

    def default_headers
      {
        accept: 'application/json',
        content_type: 'application/json',
        user_agent: "Ruby Gem vy HackerNews_Ruby #{HackernewsRuby::VERSION}"
      }
    end

    def reload_config
      HackernewsRuby.api_url = @api_url
      HackernewsRuby.api_version = @api_version
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hackernews_ruby-0.0.4 lib/hackernews_ruby/client.rb
hackernews_ruby-0.0.3 lib/hackernews_ruby/client.rb