hn_ex v0.1.1 HN View Source

A simple Hacker News API client in Elixir. All API calls have a corresponding unsafe version.

See https://github.com/HackerNews/API for a thorough description of the API and the fields returned from the various calls.

Link to this section Summary

Functions

Fetch a list of ask story ids.

Returns the latest ask stories or raises an error.

Fetch a list of best story ids.

Returns the latest best stories or raises an error.

Perform a GET request.

Perform a GET request.

Fetch an item by id. Returns {:ok, %HN.Item{}}.

Returns the item or raises an error.

Fetch a list of job story ids.

Returns the latest job stories or raises an error.

Fetch the most recent item id.

Returns the most recent item id or raises an error.

Fetch a list of new story ids.

Returns the latest new new stories or raises an error.

Perform request and raise in case of error.

Fetch a list of show story ids.

Returns the latest show stories or raises an error.

Fetch a list of top story ids.

Returns the latest top stories or raises an error.

Fetch recent item and profile changes.

Returns the latest updates or raises an error.

Fetch a user by name. Returns an HN.User.

Returns the user or raises an error.

Link to this section Types

Link to this type

option()

View Source
option() ::
  {:method, Tesla.Env.method()}
  | {:url, Tesla.Env.url()}
  | {:query, Tesla.Env.query()}
  | {:headers, Tesla.Env.headers()}
  | {:body, Tesla.Env.body()}
  | {:opts, Tesla.Env.opts()}

Link to this section Functions

Fetch a list of ask story ids.

Example

  iex> HN.ask_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest ask stories or raises an error.

Fetch a list of best story ids.

Example

  iex> HN.best_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest best stories or raises an error.

Perform a GET request.

See request/1 or request/2 for options definition.

get("/users")
get("/users", query: [scope: "admin"])
get(client, "/users")
get(client, "/users", query: [scope: "admin"])

Perform a GET request.

See request!/1 or request!/2 for options definition.

get!("/users")
get!("/users", query: [scope: "admin"])
get!(client, "/users")
get!(client, "/users", query: [scope: "admin"])

Fetch an item by id. Returns {:ok, %HN.Item{}}.

Example

iex> HN.item(8863) # argument can be an integer or a string
{:ok, %HN.Item{
     by: "dhouston",
     dead: nil,
     deleted: nil,
     descendants: 71,
     id: 8863,
     kids: [9224, 8917, 8952, 8884, 8887, 8869, 8958, 8940, 8908, 9005, 8873,
            9671, 9067, 9055, 8865, 8881, 8872, 8955, 10403, 8903, 8928, 9125, 8998,
            8901, 8902, 8907, 8894, 8870, 8878, 8980, 8934, 8943, 8876],
     parent: nil,
     parts: nil,
     poll: nil,
     score: 104,
     text: nil,
     time: 1175714200,
     title: "My YC app: Dropbox - Throw away your USB drive",
     type: "story",
     url: "http://www.getdropbox.com/u/2/screencast.html"
}}

Returns the item or raises an error.

Fetch a list of job story ids.

Example

  iex> HN.job_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest job stories or raises an error.

Fetch the most recent item id.

Example

  iex> HN.max_item
  {:ok, "22059135"}

Returns the most recent item id or raises an error.

Fetch a list of new story ids.

Example

  iex> HN.new_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest new new stories or raises an error.

Link to this function

request(client \\ %Tesla.Client{}, options)

View Source

Perform a request.

Options

  • :method - the request method, one of [:head, :get, :delete, :trace, :options, :post, :put, :patch]
  • :url - either full url e.g. "http://example.com/some/path" or just "/some/path" if using Tesla.Middleware.BaseUrl
  • :query - a keyword list of query params, e.g. [page: 1, per_page: 100]
  • :headers - a keyworld list of headers, e.g. [{"content-type", "text/plain"}]
  • :body - depends on used middleware:

    • by default it can be a binary
    • if using e.g. JSON encoding middleware it can be a nested map
    • if adapter supports it it can be a Stream with any of the above
  • :opts - custom, per-request middleware or adapter options

Examples

ExampleApi.request(method: :get, url: "/users/path")

# use shortcut methods
ExampleApi.get("/users/1")
ExampleApi.post(client, "/users", %{name: "Jon"})
Link to this function

request!(client \\ %Tesla.Client{}, options)

View Source
request!(Tesla.Env.client(), [option()]) :: Tesla.Env.t() | no_return()

Perform request and raise in case of error.

This is similar to request/2 behaviour from Tesla 0.x

See request/2 for list of available options.

Fetch a list of show story ids.

Example

  iex> HN.show_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest show stories or raises an error.

Fetch a list of top story ids.

Example

  iex> HN.top_stories
  {:ok, [22057737,22055976,22057989,22057576,22054163,22055867,22057173,22054600,22041741]}

Returns the latest top stories or raises an error.

Fetch recent item and profile changes.

Example

  iex> HN.updates
  {:ok,
    %HN.Updates{
      items: [22058286, 22054517, 22055774, 22059007, 22058838, 22056363, 22056764],
      profiles: ["nathan_compton", "chasd00", "yarapavan", "cagenut"]
  }}

Returns the latest updates or raises an error.

Fetch a user by name. Returns an HN.User.

Example

  iex> HN.user("jl")
  {:ok,
    %HN.User{
    about: "This is a test",
    created: 1173923446,
    delay: nil,
    id: "jl",
    karma: 4226,
    submitted: [19464269, 18498213, 16659709, 16659632, 16659556, 14237416,
        5252, 4752, 4586, 4289]
    }}

Returns the user or raises an error.